@zenstackhq/zod 3.5.5 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ import { AttributeApplication, Expression, FieldHasDefault, FieldIsArray, FieldIsComputed, FieldIsDelegateDiscriminator, FieldIsRelation, GetEnum, GetEnums, GetModelFieldType, GetModelFields, GetModels, GetTypeDefFieldType, GetTypeDefFields, GetTypeDefs, ModelFieldIsOptional, SchemaDef, TypeDefFieldIsArray, TypeDefFieldIsOptional } from "@zenstackhq/schema";
2
+ import Decimal from "decimal.js";
3
+ import z$1, { z } from "zod";
4
+
5
+ //#region src/types.d.ts
6
+ /**
7
+ * Scalar-only shape returned by the no-options `makeModelSchema` overload.
8
+ * Relation fields are excluded by default — use `include` or `select` to opt in.
9
+ */
10
+ type GetModelFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? never : Field]: ZodOptionalAndNullableIf<ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>> };
11
+ /**
12
+ * Full shape including both scalar and relation fields — used internally for
13
+ * type lookups (e.g. resolving relation field Zod types in include/select).
14
+ */
15
+ type GetAllModelFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = GetModelFieldsShape<Schema, Model> & { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? Field : never]: ZodNullableIf<z$1.ZodOptional<ZodArrayIf<z$1.ZodObject<GetModelFieldsShape<Schema, GetModelFieldType<Schema, Model, Field> extends GetModels<Schema> ? GetModelFieldType<Schema, Model, Field> : never>, z$1.core.$strict>, FieldIsArray<Schema, Model, Field>>>, ModelFieldIsOptional<Schema, Model, Field>> };
16
+ type GetModelCreateFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? never : FieldIsComputed<Schema, Model, Field> extends true ? never : FieldIsDelegateDiscriminator<Schema, Model, Field> extends true ? never : Field]: ZodOptionalIf<ZodOptionalAndNullableIf<ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>>, FieldHasDefault<Schema, Model, Field>> };
17
+ type GetModelUpdateFieldsShape<Schema extends SchemaDef, Model extends GetModels<Schema>> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? never : FieldIsComputed<Schema, Model, Field> extends true ? never : FieldIsDelegateDiscriminator<Schema, Model, Field> extends true ? never : Field]: z$1.ZodOptional<ZodOptionalAndNullableIf<ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, FieldIsArray<Schema, Model, Field>>, ModelFieldIsOptional<Schema, Model, Field>>> };
18
+ type GetTypeDefFieldsShape<Schema extends SchemaDef, Type extends GetTypeDefs<Schema>> = { [Field in GetTypeDefFields<Schema, Type>]: ZodOptionalAndNullableIf<ZodArrayIf<MapTypeDefFieldToZod<Schema, Type, Field>, TypeDefFieldIsArray<Schema, Type, Field>>, TypeDefFieldIsOptional<Schema, Type, Field>> };
19
+ type FieldTypeZodMap = {
20
+ String: z$1.ZodString;
21
+ Int: z$1.ZodNumber;
22
+ BigInt: z$1.ZodBigInt;
23
+ Float: z$1.ZodNumber;
24
+ Decimal: z$1.ZodType<Decimal>;
25
+ Boolean: z$1.ZodBoolean;
26
+ DateTime: z$1.ZodType<Date>;
27
+ Bytes: z$1.ZodType<Uint8Array>;
28
+ Json: JsonZodType;
29
+ };
30
+ type MapModelFieldToZod<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, FieldType = GetModelFieldType<Schema, Model, Field>> = MapFieldTypeToZod<Schema, FieldType>;
31
+ type MapTypeDefFieldToZod<Schema extends SchemaDef, Type extends GetTypeDefs<Schema>, Field extends GetTypeDefFields<Schema, Type>, FieldType = GetTypeDefFieldType<Schema, Type, Field>> = MapFieldTypeToZod<Schema, FieldType>;
32
+ type MapFieldTypeToZod<Schema extends SchemaDef, FieldType> = FieldType extends keyof FieldTypeZodMap ? FieldTypeZodMap[FieldType] : FieldType extends GetEnums<Schema> ? EnumZodType<Schema, FieldType> : FieldType extends GetTypeDefs<Schema> ? z$1.ZodObject<GetTypeDefFieldsShape<Schema, FieldType>, z$1.core.$strict> : z$1.ZodUnknown;
33
+ type JsonZodType = z$1.ZodObject<Record<string, z$1.ZodType>, z$1.core.$loose> | z$1.ZodArray<z$1.ZodType> | z$1.ZodString | z$1.ZodNumber | z$1.ZodBoolean | z$1.ZodNull;
34
+ type EnumZodType<Schema extends SchemaDef, EnumName extends GetEnums<Schema>> = z$1.ZodEnum<{ [Key in keyof GetEnum<Schema, EnumName>]: GetEnum<Schema, EnumName>[Key] }>;
35
+ type ZodOptionalAndNullableIf<T extends z$1.ZodType, Condition extends boolean> = Condition extends true ? z$1.ZodOptional<z$1.ZodNullable<T>> : T;
36
+ type ZodOptionalIf<T extends z$1.ZodType, Condition extends boolean> = Condition extends true ? z$1.ZodOptional<T> : T;
37
+ type ZodNullableIf<T extends z$1.ZodType, Condition extends boolean> = Condition extends true ? z$1.ZodNullable<T> : T;
38
+ type ZodArrayIf<T extends z$1.ZodType, Condition extends boolean> = Condition extends true ? z$1.ZodArray<T> : T;
39
+ /**
40
+ * The non-relation scalar fields of a model (excludes relation fields and
41
+ * foreign-key fields that back a relation).
42
+ */
43
+ type ScalarModelFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? never : Field]: Field };
44
+ /**
45
+ * The relation fields of a model.
46
+ */
47
+ type RelationModelFields<Schema extends SchemaDef, Model extends GetModels<Schema>> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? Field : never]: Field };
48
+ /**
49
+ * For a relation field, resolve the related model name.
50
+ */
51
+ type RelatedModel<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetModelFieldType<Schema, Model, Field> extends GetModels<Schema> ? GetModelFieldType<Schema, Model, Field> : never;
52
+ /**
53
+ * Controls which fields are made optional in the generated schema.
54
+ *
55
+ * - `"all"` — every field in the schema becomes optional.
56
+ * - `"defaults"` — only fields that have a default value (`@default`) or are
57
+ * auto-managed (`@updatedAt`) are made optional; all other
58
+ * fields retain their original optionality.
59
+ */
60
+ type ModelSchemaOptionality = 'all' | 'defaults';
61
+ /**
62
+ * ORM-style query options accepted by `makeModelSchema`.
63
+ *
64
+ * Exactly mirrors the `select` / `include` / `omit` vocabulary:
65
+ * - `select` — pick specific fields (scalars and/or relations). Mutually
66
+ * exclusive with `include` and `omit`.
67
+ * - `include` — start with all scalar fields, then add the named relation
68
+ * fields. Can be combined with `omit`.
69
+ * - `omit` — remove named scalar fields from the default scalar set.
70
+ * Can be combined with `include`, mutually exclusive with
71
+ * `select`.
72
+ * - `optionality` — when `"all"`, every field becomes optional. When
73
+ * `"defaults"`, only fields with a `@default` value or
74
+ * `@updatedAt` are made optional.
75
+ */
76
+ type ModelSchemaOptions<Schema extends SchemaDef, Model extends GetModels<Schema>> = {
77
+ /**
78
+ * Pick only the listed fields. Values must be `true` (include with
79
+ * default shape) or a nested options object (for relation fields).
80
+ * Only `true` is accepted — ORM convention.
81
+ */
82
+ select: { [Field in GetModelFields<Schema, Model>]?: FieldIsRelation<Schema, Model, Field> extends true ? true | ModelSchemaOptions<Schema, RelatedModel<Schema, Model, Field>> : true };
83
+ include?: never;
84
+ omit?: never;
85
+ /**
86
+ * Controls which fields are made optional.
87
+ * - `"all"` — every field becomes optional.
88
+ * - `"defaults"` — only fields with `@default` or `@updatedAt` become optional.
89
+ */
90
+ optionality?: ModelSchemaOptionality;
91
+ } | {
92
+ select?: never;
93
+ /**
94
+ * Add the listed relation fields on top of the scalar fields.
95
+ * Values must be `true` (default shape) or a nested options object.
96
+ * Only `true` is accepted — ORM convention.
97
+ */
98
+ include?: { [Field in keyof RelationModelFields<Schema, Model>]?: Field extends GetModelFields<Schema, Model> ? true | ModelSchemaOptions<Schema, RelatedModel<Schema, Model, Field>> : never };
99
+ /**
100
+ * Remove the listed scalar fields from the output.
101
+ * Only `true` is accepted — ORM convention.
102
+ */
103
+ omit?: { [Field in keyof ScalarModelFields<Schema, Model>]?: true };
104
+ /**
105
+ * Controls which fields are made optional.
106
+ * - `"all"` — every field becomes optional.
107
+ * - `"defaults"` — only fields with `@default` or `@updatedAt` become optional.
108
+ */
109
+ optionality?: ModelSchemaOptionality;
110
+ };
111
+ /**
112
+ * Narrows `Field` so it can safely index `GetModelFieldsShape`. The mapped
113
+ * type uses a `as`-remapping clause, so TypeScript widens the key set and
114
+ * `Field extends GetModelFields<…>` alone is not enough for indexing.
115
+ */
116
+ type FieldInShape<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = Field & keyof GetAllModelFieldsShape<Schema, Model>;
117
+ /**
118
+ * Zod shape produced when a relation field is included via `include: { field:
119
+ * true }` or `select: { field: true }` — identical to how the existing
120
+ * `makeModelSchema` (no-options) represents relation fields: optional, carries
121
+ * array-ness and nullability from the field definition.
122
+ */
123
+ type RelationFieldZodDefault<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>> = GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>];
124
+ /**
125
+ * Zod shape for a relation field included with nested options. We recurse
126
+ * into `GetModelSchemaShapeWithOptions` for the related model, then re-apply
127
+ * the same optional/array/nullable wrappers as the default relation field.
128
+ */
129
+ type RelationFieldZodWithOptions<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, Options> = RelatedModel<Schema, Model, Field> extends GetModels<Schema> ? ZodNullableIf<z$1.ZodOptional<ZodArrayIf<z$1.ZodObject<GetModelSchemaShapeWithOptions<Schema, RelatedModel<Schema, Model, Field>, Options>, z$1.core.$strict>, FieldIsArray<Schema, Model, Field>>>, ModelFieldIsOptional<Schema, Model, Field>> : never;
130
+ /**
131
+ * Resolve the Zod type for a single field given a select-entry value (`true`
132
+ * or a nested options object).
133
+ */
134
+ type SelectEntryToZod<Schema extends SchemaDef, Model extends GetModels<Schema>, Field extends GetModelFields<Schema, Model>, Value> = Value extends boolean ? GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>] : Value extends object ? RelationFieldZodWithOptions<Schema, Model, Field, Value> : never;
135
+ /**
136
+ * Build the Zod shape for the `select` branch: only the listed fields,
137
+ * recursing into relations when given nested options.
138
+ */
139
+ type BuildSelectShape<Schema extends SchemaDef, Model extends GetModels<Schema>, S extends Record<string, unknown>> = { [Field in keyof S & GetModelFields<Schema, Model>]: SelectEntryToZod<Schema, Model, Field, S[Field]> };
140
+ /**
141
+ * Build the Zod shape for the `include` + `omit` branch:
142
+ * - All scalar fields, minus any that appear in `omit` with value `true`.
143
+ * - Plus the relation fields listed in `include`.
144
+ */
145
+ type BuildIncludeOmitShape<Schema extends SchemaDef, Model extends GetModels<Schema>, I extends Record<string, unknown> | undefined, O extends Record<string, unknown> | undefined> = { [Field in GetModelFields<Schema, Model> as FieldIsRelation<Schema, Model, Field> extends true ? never : O extends object ? Field extends keyof O ? O[Field] extends true ? never : Field : Field : Field]: GetAllModelFieldsShape<Schema, Model>[FieldInShape<Schema, Model, Field>] } & (I extends object ? { [Field in keyof I & GetModelFields<Schema, Model>]: I[Field] extends object ? RelationFieldZodWithOptions<Schema, Model, Field, I[Field]> : RelationFieldZodDefault<Schema, Model, Field> } : {});
146
+ /**
147
+ * Wraps every field in a shape with `z.ZodOptional` when `Optionality` is `"all"`.
148
+ * When `Optionality` is `"defaults"`, only fields that carry a `@default` or
149
+ * `@updatedAt` attribute (as detected by `FieldHasDefault`) are wrapped.
150
+ * When `Optionality` is anything else the shape is returned as-is.
151
+ */
152
+ type ApplyOptionality<Shape extends Record<string, z$1.ZodType>, Optionality, Schema extends SchemaDef = never, Model extends GetModels<Schema> = never> = Optionality extends 'all' ? { [K in keyof Shape]: z$1.ZodOptional<Shape[K]> } : Optionality extends 'defaults' ? { [K in keyof Shape]: K extends GetModelFields<Schema, Model> ? FieldHasDefault<Schema, Model, K> extends true ? z$1.ZodOptional<Shape[K]> : Shape[K] : Shape[K] } : Shape;
153
+ /**
154
+ * The top-level conditional that maps options → Zod shape.
155
+ *
156
+ * - No options / undefined → existing `GetModelFieldsShape` (no change).
157
+ * - `{ select: S }` → `BuildSelectShape` (+ optionality wrapper).
158
+ * - `{ include?, omit? }` → `BuildIncludeOmitShape` (+ optionality wrapper).
159
+ *
160
+ * `optionality: "defaults"` is inferred statically using `FieldHasDefault`,
161
+ * which inspects the `default` and `updatedAt` fields on `FieldDef` to
162
+ * determine which fields should become optional.
163
+ */
164
+ type GetModelSchemaShapeWithOptions<Schema extends SchemaDef, Model extends GetModels<Schema>, Options> = Options extends {
165
+ select: infer S extends Record<string, unknown>;
166
+ optionality?: infer Opt;
167
+ } ? ApplyOptionality<BuildSelectShape<Schema, Model, S>, Opt, Schema, Model> : Options extends {
168
+ include?: infer I extends Record<string, unknown> | undefined;
169
+ omit?: infer O extends Record<string, unknown> | undefined;
170
+ optionality?: infer Opt;
171
+ } ? ApplyOptionality<BuildIncludeOmitShape<Schema, Model, I, O>, Opt, Schema, Model> : GetModelFieldsShape<Schema, Model>;
172
+ //#endregion
173
+ //#region src/factory.d.ts
174
+ declare function createSchemaFactory<Schema extends SchemaDef>(schema: Schema): SchemaFactory<Schema>;
175
+ declare class SchemaFactory<Schema extends SchemaDef> {
176
+ private readonly schema;
177
+ constructor(_schema: Schema);
178
+ makeModelSchema<Model extends GetModels<Schema>>(model: Model): z$1.ZodObject<GetModelFieldsShape<Schema, Model>, z$1.core.$strict>;
179
+ makeModelSchema<Model extends GetModels<Schema>, Options extends ModelSchemaOptions<Schema, Model>>(model: Model, options: Options): z$1.ZodObject<GetModelSchemaShapeWithOptions<Schema, Model, Options>, z$1.core.$strict>;
180
+ /**
181
+ * @deprecated Use `makeModelSchema(model, { optionality: 'defaults' })` instead.
182
+ */
183
+ makeModelCreateSchema<Model extends GetModels<Schema>>(model: Model): z$1.ZodObject<GetModelCreateFieldsShape<Schema, Model>, z$1.core.$strict>;
184
+ /**
185
+ * @deprecated Use `makeModelSchema(model, { optionality: 'all' })` instead.
186
+ */
187
+ makeModelUpdateSchema<Model extends GetModels<Schema>>(model: Model): z$1.ZodObject<GetModelUpdateFieldsShape<Schema, Model>, z$1.core.$strict>;
188
+ /**
189
+ * Applies the `optionality` option to a fields map.
190
+ *
191
+ * - `"all"` — wraps every field in `z.ZodOptional`.
192
+ * - `"defaults"` — only wraps fields that have a `@default` attribute or
193
+ * are `@updatedAt` in `z.ZodOptional`. Fields that are
194
+ * already optional (nullable optional) retain their shape;
195
+ * we just add the outer optional layer.
196
+ * - `undefined` — returns the fields map unchanged.
197
+ */
198
+ private applyOptionality;
199
+ /**
200
+ * Internal loose options shape used at runtime (we've already validated the
201
+ * type-level constraints via the overload signatures).
202
+ */
203
+ private buildFieldsWithOptions;
204
+ /**
205
+ * Returns the set of scalar field names that will be present in the
206
+ * resulting schema after applying `options`. Used by `addCustomValidation`
207
+ * to skip `@@validate` rules that reference an absent field.
208
+ *
209
+ * Only scalar fields matter here because `@@validate` conditions are
210
+ * restricted by the ZModel compiler to scalar fields of the same model.
211
+ */
212
+ private buildPresentFields;
213
+ /**
214
+ * Build the inner Zod schema for a relation field, optionally with nested
215
+ * query options. Does NOT apply cardinality/optional wrappers — the caller
216
+ * does that.
217
+ */
218
+ private makeRelationFieldSchema;
219
+ private makeScalarFieldSchema;
220
+ private makeJsonSchema;
221
+ /**
222
+ * Wraps a schema with `.optional()` and copies any `description` from its
223
+ * metadata onto the resulting `ZodOptional`, so that callers inspecting
224
+ * `.meta()?.description` on shape fields still find the value after
225
+ * optionality has been applied.
226
+ */
227
+ private wrapOptionalPreservingMeta;
228
+ private applyCardinality;
229
+ makeTypeSchema<Type extends GetTypeDefs<Schema>>(type: Type): z$1.ZodObject<GetTypeDefFieldsShape<Schema, Type>, z$1.core.$strict>;
230
+ makeEnumSchema<Enum extends GetEnums<Schema>>(_enum: Enum): z$1.ZodEnum<{ [Key in keyof GetEnum<Schema, Enum>]: GetEnum<Schema, Enum>[Key] }>;
231
+ private getMetaDescription;
232
+ private applyDescription;
233
+ }
234
+ declare namespace utils_d_exports {
235
+ export { addBigIntValidation, addCustomValidation, addDecimalValidation, addListValidation, addNumberValidation, addStringValidation, collectFieldRefs };
236
+ }
237
+ declare function addStringValidation(schema: z.ZodString, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema;
238
+ declare function addNumberValidation(schema: z.ZodNumber, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema;
239
+ declare function addBigIntValidation(schema: z.ZodBigInt, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema;
240
+ declare function addDecimalValidation(schema: z.ZodType<Decimal> | z.ZodString, attributes: readonly AttributeApplication[] | undefined, addExtraValidation: boolean): z.ZodSchema;
241
+ declare function addListValidation(schema: z.ZodArray<any>, attributes: readonly AttributeApplication[] | undefined): z.ZodSchema;
242
+ /**
243
+ * Recursively collects all field names referenced by `kind: 'field'` nodes
244
+ * inside an expression tree.
245
+ */
246
+ declare function collectFieldRefs(expr: Expression): Set<string>;
247
+ /**
248
+ * Applies `@@validate` rules from `attributes` to `schema` as Zod refinements.
249
+ *
250
+ * When `presentFields` is provided, only rules whose every field reference is
251
+ * present in the set are applied. Rules that reference a field absent from the
252
+ * set are silently skipped — they cannot be evaluated correctly against a
253
+ * partial payload (e.g. when `select` or `omit` has been used).
254
+ *
255
+ * Omit `presentFields` (or pass `undefined`) to apply all rules
256
+ * unconditionally, which is the correct behaviour for full-model schemas.
257
+ *
258
+ * Note: `@@validate` conditions are restricted by the ZModel compiler to
259
+ * scalar fields of the same model only — relation fields are a compile error.
260
+ * A flat field-name set is therefore sufficient.
261
+ */
262
+ declare function addCustomValidation(schema: z.ZodSchema, attributes: readonly AttributeApplication[] | undefined, presentFields?: ReadonlySet<string>): z.ZodSchema;
263
+ //#endregion
264
+ export { type GetModelSchemaShapeWithOptions, type ModelSchemaOptions, utils_d_exports as ZodUtils, createSchemaFactory };
265
+ //# sourceMappingURL=index.d.mts.map