UNPKG

single-source-of-truth

Version:
58 lines 2.35 kB
import * as z from 'zod/v4'; export const TruthRelation = z.core.$constructor('TruthRelation', (inst, def) => { z.ZodType.init(inst, def); inst.unwrap = () => def.getter(); inst.name = (name) => inst.clone({ ...inst.def, ' name': name }); inst.reference = (field, relatedField) => inst.clone({ ...inst.def, ' references': [...(inst.def.references ?? []), [field, relatedField]], }); }); export function relation(getter) { return new TruthRelation({ type: 'custom', getter: getter, }); } // export var TruthRelationUtil; (function (TruthRelationUtil) { function isAnyRelation(value) { return (value instanceof TruthRelation || (value instanceof z.ZodNullable && value.unwrap() instanceof TruthRelation) || (value instanceof z.ZodArray && value.element instanceof TruthRelation)); } TruthRelationUtil.isAnyRelation = isAnyRelation; function normaliseRelation(relation) { if (relation instanceof TruthRelation) return relation.def.getter(); if (relation instanceof z.ZodNullable) return z.nullable(relation.unwrap().unwrap()); if (relation instanceof z.ZodArray) return z.array(relation.element.unwrap()); throw new Error('Invalid relation'); } TruthRelationUtil.normaliseRelation = normaliseRelation; function extractModelFromRelation(relation) { if (relation instanceof TruthRelation) return relation.def.getter(); if (relation instanceof z.ZodNullable) return relation.unwrap().unwrap(); if (relation instanceof z.ZodArray) return relation.element.unwrap(); throw new Error('Invalid relation'); } TruthRelationUtil.extractModelFromRelation = extractModelFromRelation; function applyRelationWrapping(blueprint, schema) { if (blueprint instanceof TruthRelation) return schema; if (blueprint instanceof z.ZodNullable) return z.nullable(schema); if (blueprint instanceof z.ZodArray) return z.array(schema); throw new Error('Invalid relation'); } TruthRelationUtil.applyRelationWrapping = applyRelationWrapping; })(TruthRelationUtil || (TruthRelationUtil = {})); //# sourceMappingURL=relation.js.map