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