import { AttributeApplication, Expression, FieldHasDefault, FieldIsArray, FieldIsComputed, FieldIsDelegateDiscriminator, FieldIsRelation, GetEnum, GetEnums, GetModelFieldType, GetModelFields, GetModels, GetTypeDefFieldType, GetTypeDefFields, GetTypeDefs, ModelFieldIsOptional, SchemaDef, TypeDefFieldIsArray, TypeDefFieldIsOptional } from "@zenstackhq/schema"; import Decimal from "decimal.js"; import z$1, { z } from "zod"; //#region src/types.d.ts /** * Scalar-only shape returned by the no-options `makeModelSchema` overload. * Relation fields are excluded by default — use `include` or `select` to opt in. */ type GetModelFieldsShape> = { [Field in GetModelFields as FieldIsRelation extends true ? never : Field]: ZodOptionalAndNullableIf, FieldIsArray>, ModelFieldIsOptional> }; /** * Full shape including both scalar and relation fields — used internally for * type lookups (e.g. resolving relation field Zod types in include/select). */ type GetAllModelFieldsShape> = GetModelFieldsShape & { [Field in GetModelFields as FieldIsRelation extends true ? Field : never]: ZodNullableIf extends GetModels ? GetModelFieldType : never>, z$1.core.$strict>, FieldIsArray>>, ModelFieldIsOptional> }; type GetModelCreateFieldsShape> = { [Field in GetModelFields as FieldIsRelation extends true ? never : FieldIsComputed extends true ? never : FieldIsDelegateDiscriminator extends true ? never : Field]: ZodOptionalIf, FieldIsArray>, ModelFieldIsOptional>, FieldHasDefault> }; type GetModelUpdateFieldsShape> = { [Field in GetModelFields as FieldIsRelation extends true ? never : FieldIsComputed extends true ? never : FieldIsDelegateDiscriminator extends true ? never : Field]: z$1.ZodOptional, FieldIsArray>, ModelFieldIsOptional>> }; type GetTypeDefFieldsShape> = { [Field in GetTypeDefFields]: ZodOptionalAndNullableIf, TypeDefFieldIsArray>, TypeDefFieldIsOptional> }; type FieldTypeZodMap = { String: z$1.ZodString; Int: z$1.ZodNumber; BigInt: z$1.ZodBigInt; Float: z$1.ZodNumber; Decimal: z$1.ZodType; Boolean: z$1.ZodBoolean; DateTime: z$1.ZodDate; Bytes: z$1.ZodType; Json: JsonZodType; }; type MapModelFieldToZod, Field extends GetModelFields, FieldType = GetModelFieldType> = MapFieldTypeToZod; type MapTypeDefFieldToZod, Field extends GetTypeDefFields, FieldType = GetTypeDefFieldType> = MapFieldTypeToZod; type MapFieldTypeToZod = FieldType extends keyof FieldTypeZodMap ? FieldTypeZodMap[FieldType] : FieldType extends GetEnums ? EnumZodType : FieldType extends GetTypeDefs ? z$1.ZodObject, z$1.core.$strict> : z$1.ZodUnknown; type JsonValue = string | number | boolean | JsonObject | JsonArray | null; type JsonObject = { [key: string]: JsonValue; }; type JsonArray = Array; type JsonZodType = z$1.ZodType; type EnumZodType> = z$1.ZodEnum<{ [Key in keyof GetEnum]: GetEnum[Key] }>; type ZodOptionalAndNullableIf = Condition extends true ? z$1.ZodOptional> : T; type ZodOptionalIf = Condition extends true ? z$1.ZodOptional : T; type ZodNullableIf = Condition extends true ? z$1.ZodNullable : T; type ZodArrayIf = Condition extends true ? z$1.ZodArray : T; /** * The non-relation scalar fields of a model (excludes relation fields and * foreign-key fields that back a relation). */ type ScalarModelFields> = { [Field in GetModelFields as FieldIsRelation extends true ? never : Field]: Field }; /** * The relation fields of a model. */ type RelationModelFields> = { [Field in GetModelFields as FieldIsRelation extends true ? Field : never]: Field }; /** * For a relation field, resolve the related model name. */ type RelatedModel, Field extends GetModelFields> = GetModelFieldType extends GetModels ? GetModelFieldType : never; /** * Controls which fields are made optional in the generated schema. * * - `"all"` — every field in the schema becomes optional. * - `"defaults"` — only fields that have a default value (`@default`) or are * auto-managed (`@updatedAt`) are made optional; all other * fields retain their original optionality. */ type ModelSchemaOptionality = 'all' | 'defaults'; /** * ORM-style query options accepted by `makeModelSchema`. * * Exactly mirrors the `select` / `include` / `omit` vocabulary: * - `select` — pick specific fields (scalars and/or relations). Mutually * exclusive with `include` and `omit`. * - `include` — start with all scalar fields, then add the named relation * fields. Can be combined with `omit`. * - `omit` — remove named scalar fields from the default scalar set. * Can be combined with `include`, mutually exclusive with * `select`. * - `optionality` — when `"all"`, every field becomes optional. When * `"defaults"`, only fields with a `@default` value or * `@updatedAt` are made optional. */ type ModelSchemaOptions> = { /** * Pick only the listed fields. Values must be `true` (include with * default shape) or a nested options object (for relation fields). * Only `true` is accepted — ORM convention. */ select: { [Field in GetModelFields]?: FieldIsRelation extends true ? true | ModelSchemaOptions> : true }; include?: never; omit?: never; /** * Controls which fields are made optional. * - `"all"` — every field becomes optional. * - `"defaults"` — only fields with `@default` or `@updatedAt` become optional. */ optionality?: ModelSchemaOptionality; } | { select?: never; /** * Add the listed relation fields on top of the scalar fields. * Values must be `true` (default shape) or a nested options object. * Only `true` is accepted — ORM convention. */ include?: { [Field in keyof RelationModelFields]?: Field extends GetModelFields ? true | ModelSchemaOptions> : never }; /** * Remove the listed scalar fields from the output. * Only `true` is accepted — ORM convention. */ omit?: { [Field in keyof ScalarModelFields]?: true }; /** * Controls which fields are made optional. * - `"all"` — every field becomes optional. * - `"defaults"` — only fields with `@default` or `@updatedAt` become optional. */ optionality?: ModelSchemaOptionality; }; /** * Narrows `Field` so it can safely index `GetModelFieldsShape`. The mapped * type uses a `as`-remapping clause, so TypeScript widens the key set and * `Field extends GetModelFields<…>` alone is not enough for indexing. */ type FieldInShape, Field extends GetModelFields> = Field & keyof GetAllModelFieldsShape; /** * Zod shape produced when a relation field is included via `include: { field: * true }` or `select: { field: true }` — identical to how the existing * `makeModelSchema` (no-options) represents relation fields: optional, carries * array-ness and nullability from the field definition. */ type RelationFieldZodDefault, Field extends GetModelFields> = GetAllModelFieldsShape[FieldInShape]; /** * Zod shape for a relation field included with nested options. We recurse * into `GetModelSchemaShapeWithOptions` for the related model, then re-apply * the same optional/array/nullable wrappers as the default relation field. */ type RelationFieldZodWithOptions, Field extends GetModelFields, Options> = RelatedModel extends GetModels ? ZodNullableIf, Options>, z$1.core.$strict>, FieldIsArray>>, ModelFieldIsOptional> : never; /** * Resolve the Zod type for a single field given a select-entry value (`true` * or a nested options object). */ type SelectEntryToZod, Field extends GetModelFields, Value> = Value extends boolean ? GetAllModelFieldsShape[FieldInShape] : Value extends object ? RelationFieldZodWithOptions : never; /** * Build the Zod shape for the `select` branch: only the listed fields, * recursing into relations when given nested options. */ type BuildSelectShape, S extends Record> = { [Field in keyof S & GetModelFields]: SelectEntryToZod }; /** * Build the Zod shape for the `include` + `omit` branch: * - All scalar fields, minus any that appear in `omit` with value `true`. * - Plus the relation fields listed in `include`. */ type BuildIncludeOmitShape, I extends Record | undefined, O extends Record | undefined> = { [Field in GetModelFields as FieldIsRelation extends true ? never : O extends object ? Field extends keyof O ? O[Field] extends true ? never : Field : Field : Field]: GetAllModelFieldsShape[FieldInShape] } & (I extends object ? { [Field in keyof I & GetModelFields]: I[Field] extends object ? RelationFieldZodWithOptions : RelationFieldZodDefault } : {}); /** * Wraps every field in a shape with `z.ZodOptional` when `Optionality` is `"all"`. * When `Optionality` is `"defaults"`, only fields that carry a `@default` or * `@updatedAt` attribute (as detected by `FieldHasDefault`) are wrapped. * When `Optionality` is anything else the shape is returned as-is. */ type ApplyOptionality, Optionality, Schema extends SchemaDef = never, Model extends GetModels = never> = Optionality extends 'all' ? { [K in keyof Shape]: z$1.ZodOptional } : Optionality extends 'defaults' ? { [K in keyof Shape]: K extends GetModelFields ? FieldHasDefault extends true ? z$1.ZodOptional : Shape[K] : Shape[K] } : Shape; /** * The top-level conditional that maps options → Zod shape. * * - No options / undefined → existing `GetModelFieldsShape` (no change). * - `{ select: S }` → `BuildSelectShape` (+ optionality wrapper). * - `{ include?, omit? }` → `BuildIncludeOmitShape` (+ optionality wrapper). * * `optionality: "defaults"` is inferred statically using `FieldHasDefault`, * which inspects the `default` and `updatedAt` fields on `FieldDef` to * determine which fields should become optional. */ type GetModelSchemaShapeWithOptions, Options> = Options extends { select: infer S extends Record; optionality?: infer Opt; } ? ApplyOptionality, Opt, Schema, Model> : Options extends { include?: infer I extends Record | undefined; omit?: infer O extends Record | undefined; optionality?: infer Opt; } ? ApplyOptionality, Opt, Schema, Model> : GetModelFieldsShape; //#endregion //#region src/factory.d.ts declare function createSchemaFactory(schema: Schema): SchemaFactory; declare class SchemaFactory { private readonly schema; constructor(_schema: Schema); makeModelSchema>(model: Model): z$1.ZodObject, z$1.core.$strict>; makeModelSchema, Options extends ModelSchemaOptions>(model: Model, options: Options): z$1.ZodObject, z$1.core.$strict>; /** * @deprecated Use `makeModelSchema(model, { optionality: 'defaults' })` instead. */ makeModelCreateSchema>(model: Model): z$1.ZodObject, z$1.core.$strict>; /** * @deprecated Use `makeModelSchema(model, { optionality: 'all' })` instead. */ makeModelUpdateSchema>(model: Model): z$1.ZodObject, z$1.core.$strict>; /** * Applies the `optionality` option to a fields map. * * - `"all"` — wraps every field in `z.ZodOptional`. * - `"defaults"` — only wraps fields that have a `@default` attribute or * are `@updatedAt` in `z.ZodOptional`. Fields that are * already optional (nullable optional) retain their shape; * we just add the outer optional layer. * - `undefined` — returns the fields map unchanged. */ private applyOptionality; /** * Internal loose options shape used at runtime (we've already validated the * type-level constraints via the overload signatures). */ private buildFieldsWithOptions; /** * Returns the set of scalar field names that will be present in the * resulting schema after applying `options`. Used by `addCustomValidation` * to skip `@@validate` rules that reference an absent field. * * Only scalar fields matter here because `@@validate` conditions are * restricted by the ZModel compiler to scalar fields of the same model. */ private buildPresentFields; /** * Build the inner Zod schema for a relation field, optionally with nested * query options. Does NOT apply cardinality/optional wrappers — the caller * does that. */ private makeRelationFieldSchema; private makeScalarFieldSchema; private makeJsonSchema; /** * Wraps a schema with `.optional()` and copies any `description` from its * metadata onto the resulting `ZodOptional`, so that callers inspecting * `.meta()?.description` on shape fields still find the value after * optionality has been applied. */ private wrapOptionalPreservingMeta; private applyCardinality; makeTypeSchema>(type: Type): z$1.ZodObject, z$1.core.$strict>; makeEnumSchema>(_enum: Enum): z$1.ZodEnum<{ [Key in keyof GetEnum]: GetEnum[Key] }>; private getMetaDescription; private applyDescription; } declare namespace utils_d_exports { export { addBigIntValidation, addCustomValidation, addDecimalValidation, addListValidation, addNumberValidation, addStringValidation, collectFieldRefs }; } declare function addStringValidation(schema: z.ZodString, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema; declare function addNumberValidation(schema: z.ZodNumber, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema; declare function addBigIntValidation(schema: z.ZodBigInt, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema; declare function addDecimalValidation(schema: z.ZodType | z.ZodString, attributes: readonly AttributeApplication[] | undefined, addExtraValidation: boolean): z.ZodSchema; declare function addListValidation(schema: z.ZodArray, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema; /** * Recursively collects all field names referenced by `kind: 'field'` nodes * inside an expression tree. */ declare function collectFieldRefs(expr: Expression): Set; /** * Applies `@@validate` rules from `attributes` to `schema` as Zod refinements. * * When `presentFields` is provided, only rules whose every field reference is * present in the set are applied. Rules that reference a field absent from the * set are silently skipped — they cannot be evaluated correctly against a * partial payload (e.g. when `select` or `omit` has been used). * * Omit `presentFields` (or pass `undefined`) to apply all rules * unconditionally, which is the correct behaviour for full-model schemas. * * Note: `@@validate` conditions are restricted by the ZModel compiler to * scalar fields of the same model only — relation fields are a compile error. * A flat field-name set is therefore sufficient. */ declare function addCustomValidation(schema: z.ZodSchema, attributes: readonly AttributeApplication[] | undefined, presentFields?: ReadonlySet): z.ZodSchema; //#endregion export { type GetModelSchemaShapeWithOptions, type JsonValue, type ModelSchemaOptions, utils_d_exports as ZodUtils, createSchemaFactory }; //# sourceMappingURL=index.d.mts.map