UNPKG

single-source-of-truth

Version:
32 lines 1.29 kB
import * as z from 'zod/v4'; import { parseModelSchema } from '../resolvers/model.js'; import { TruthRelationUtil } from './relation.js'; import { TruthShapeUtil } from './shape.js'; export const TruthModel = z.core.$constructor('TruthModel', (inst, def) => { z.ZodObject.init(inst, def); inst.with = (include, override) => { const relation = inst._zod.def.relations[include]; const model = TruthRelationUtil.extractModelFromRelation(relation); const schema = override ? override(model) : model; return inst.clone({ ...inst._zod.def, includes: [...inst._zod.def.includes, include], shape: { ...inst._zod.def.shape, [include]: TruthRelationUtil.applyRelationWrapping(relation, schema), }, }); }; inst.toStandard = () => parseModelSchema(def.name ?? '', inst); }); export function model(shape) { return new TruthModel({ type: 'object', name: null, shape: TruthShapeUtil.extractZodShape(shape), attributes: TruthShapeUtil.normaliseAttributesShape(TruthShapeUtil.extractAttributesShape(shape)), includes: [], relations: TruthShapeUtil.extractRelationsShape(shape), }); } //# sourceMappingURL=model.js.map