@cleverbrush/schema 1.1.10 → 2.0.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.
Files changed (87) hide show
  1. package/README.md +1314 -101
  2. package/dist/builders/AnySchemaBuilder.d.ts +55 -15
  3. package/dist/builders/AnySchemaBuilder.js +2 -112
  4. package/dist/builders/AnySchemaBuilder.js.map +1 -0
  5. package/dist/builders/ArraySchemaBuilder.d.ts +124 -23
  6. package/dist/builders/ArraySchemaBuilder.js +2 -284
  7. package/dist/builders/ArraySchemaBuilder.js.map +1 -0
  8. package/dist/builders/BooleanSchemaBuilder.d.ts +95 -20
  9. package/dist/builders/BooleanSchemaBuilder.js +2 -150
  10. package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
  11. package/dist/builders/DateSchemaBuilder.d.ts +135 -37
  12. package/dist/builders/DateSchemaBuilder.js +2 -433
  13. package/dist/builders/DateSchemaBuilder.js.map +1 -0
  14. package/dist/builders/ExternSchemaBuilder.d.ts +200 -0
  15. package/dist/builders/ExternSchemaBuilder.js +2 -0
  16. package/dist/builders/ExternSchemaBuilder.js.map +1 -0
  17. package/dist/builders/FunctionSchemaBuilder.d.ts +58 -15
  18. package/dist/builders/FunctionSchemaBuilder.js +2 -113
  19. package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
  20. package/dist/builders/LazySchemaBuilder.d.ts +167 -0
  21. package/dist/builders/NullSchemaBuilder.d.ts +162 -0
  22. package/dist/builders/NumberSchemaBuilder.d.ts +143 -31
  23. package/dist/builders/NumberSchemaBuilder.js +2 -386
  24. package/dist/builders/NumberSchemaBuilder.js.map +1 -0
  25. package/dist/builders/ObjectSchemaBuilder.d.ts +266 -61
  26. package/dist/builders/ObjectSchemaBuilder.js +2 -589
  27. package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
  28. package/dist/builders/PropertyValidationResult.d.ts +68 -0
  29. package/dist/builders/RecordSchemaBuilder.d.ts +341 -0
  30. package/dist/builders/RecordSchemaBuilder.js +2 -0
  31. package/dist/builders/RecordSchemaBuilder.js.map +1 -0
  32. package/dist/builders/SchemaBuilder.d.ts +818 -30
  33. package/dist/builders/StringSchemaBuilder.d.ts +152 -37
  34. package/dist/builders/StringSchemaBuilder.js +2 -414
  35. package/dist/builders/StringSchemaBuilder.js.map +1 -0
  36. package/dist/builders/TupleSchemaBuilder.d.ts +248 -0
  37. package/dist/builders/TupleSchemaBuilder.js +2 -0
  38. package/dist/builders/TupleSchemaBuilder.js.map +1 -0
  39. package/dist/builders/UnionSchemaBuilder.d.ts +133 -39
  40. package/dist/builders/UnionSchemaBuilder.js +2 -216
  41. package/dist/builders/UnionSchemaBuilder.js.map +1 -0
  42. package/dist/chunk-BFCMDDTX.js +2 -0
  43. package/dist/chunk-BFCMDDTX.js.map +1 -0
  44. package/dist/chunk-CTP4RHDG.js +2 -0
  45. package/dist/chunk-CTP4RHDG.js.map +1 -0
  46. package/dist/chunk-EUQ5AE3H.js +2 -0
  47. package/dist/chunk-EUQ5AE3H.js.map +1 -0
  48. package/dist/chunk-FWUEUW2N.js +2 -0
  49. package/dist/chunk-FWUEUW2N.js.map +1 -0
  50. package/dist/chunk-IJ6FO37G.js +2 -0
  51. package/dist/chunk-IJ6FO37G.js.map +1 -0
  52. package/dist/chunk-KESLT5EE.js +2 -0
  53. package/dist/chunk-KESLT5EE.js.map +1 -0
  54. package/dist/chunk-KFTO7TMB.js +2 -0
  55. package/dist/chunk-KFTO7TMB.js.map +1 -0
  56. package/dist/chunk-MKAYFPAR.js +2 -0
  57. package/dist/chunk-MKAYFPAR.js.map +1 -0
  58. package/dist/chunk-NXPH3ZUW.js +2 -0
  59. package/dist/chunk-NXPH3ZUW.js.map +1 -0
  60. package/dist/chunk-S5TC6NSU.js +2 -0
  61. package/dist/chunk-S5TC6NSU.js.map +1 -0
  62. package/dist/chunk-YWBNVHR6.js +2 -0
  63. package/dist/chunk-YWBNVHR6.js.map +1 -0
  64. package/dist/chunk-Z7QZ7IXT.js +2 -0
  65. package/dist/chunk-Z7QZ7IXT.js.map +1 -0
  66. package/dist/chunk-ZAN4ZHCJ.js +2 -0
  67. package/dist/chunk-ZAN4ZHCJ.js.map +1 -0
  68. package/dist/chunk-ZUPYV5TI.js +2 -0
  69. package/dist/chunk-ZUPYV5TI.js.map +1 -0
  70. package/dist/core.d.ts +23 -0
  71. package/dist/core.js +2 -0
  72. package/dist/core.js.map +1 -0
  73. package/dist/extension.d.ts +413 -0
  74. package/dist/extensions/array.d.ts +112 -0
  75. package/dist/extensions/enum.d.ts +190 -0
  76. package/dist/extensions/index.d.ts +108 -0
  77. package/dist/extensions/nullable.d.ts +26 -0
  78. package/dist/extensions/number.d.ts +228 -0
  79. package/dist/extensions/string.d.ts +332 -0
  80. package/dist/extensions/util.d.ts +45 -0
  81. package/dist/index.d.ts +7 -20
  82. package/dist/index.js +2 -19
  83. package/dist/index.js.map +1 -0
  84. package/dist/utils/transaction.d.ts +27 -4
  85. package/package.json +74 -6
  86. package/dist/builders/SchemaBuilder.js +0 -275
  87. package/dist/utils/transaction.js +0 -178
@@ -1,29 +1,70 @@
1
- import { InferType, SchemaBuilder, ValidationContext, ValidationResult } from './SchemaBuilder.js';
1
+ import { PropertyValidationResult } from './PropertyValidationResult.js';
2
+ import { type BRAND, type InferType, type NestedValidationResult, type PreValidationResult, type PropertyDescriptor, type PropertyDescriptorTree, SchemaBuilder, SYMBOL_HAS_PROPERTIES, type ValidationContext, type ValidationError, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
3
+ /**
4
+ * A callback function to select properties from the schema.
5
+ * Normally it's provided by the user to select property descriptors
6
+ * from the schema for the further usage. e.g. to select source and destination
7
+ * properties for object mappings
8
+ */
9
+ export type SchemaPropertySelector<TSchema extends ObjectSchemaBuilder<any, any, any, any, any, any>, TPropertySchema extends SchemaBuilder<any, any, any, any, any>, TAssignableTo = any, TParentPropertyDescriptor = undefined> = (l: PropertyDescriptorTree<TSchema, TSchema, TAssignableTo>) => PropertyDescriptor<TSchema, TPropertySchema, TParentPropertyDescriptor>;
2
10
  type ObjectSchemaBuilderProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = ReturnType<ObjectSchemaBuilder<T, TRequired>['introspect']>;
3
11
  type ObjectSchemaBuilderCreateProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = Partial<ObjectSchemaBuilderProps<T, TRequired>>;
4
12
  type Id<T> = T extends infer U ? {
5
13
  [K in keyof U]: U[K];
6
14
  } : never;
7
- export type RespectPropsOptionality<T extends Record<string, SchemaBuilder<any, any>>> = {
15
+ export type RespectPropsOptionality<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
8
16
  [K in RequiredProps<T>]: InferType<T[K]>;
9
17
  } & {
10
18
  [K in NotRequiredProps<T>]?: InferType<T[K]>;
11
19
  };
12
- type MakeChildrenRequired<T extends Record<string, SchemaBuilder<any, any>>> = {
20
+ type RespectPropsOptionalityForInput<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
21
+ [K in RequiredInputProps<T>]: InferType<T[K]>;
22
+ } & {
23
+ [K in NotRequiredInputProps<T>]?: InferType<T[K]>;
24
+ };
25
+ type MakeChildrenRequired<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
13
26
  [K in keyof T]: ReturnType<T[K]['required']>;
14
27
  };
15
- type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any>>> = {
28
+ type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
16
29
  [K in keyof T]: ReturnType<T[K]['optional']>;
17
30
  };
18
- type MakeChildOptional<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T> = {
31
+ /**
32
+ * Recursively maps each property to its optional form, descending into
33
+ * nested `ObjectSchemaBuilder` schemas. All other schema types (arrays,
34
+ * unions, primitives) are only made optional at the top level.
35
+ */
36
+ type DeepMakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
37
+ [K in keyof T]: T[K] extends ObjectSchemaBuilder<infer P extends Record<string, SchemaBuilder<any, any, any, any, any>>, any, any, any, any, any> ? ReturnType<ReturnType<T[K]['deepPartial']>['optional']> : ReturnType<T[K]['optional']>;
38
+ };
39
+ type MakeChildOptional<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T> = {
19
40
  [K in keyof T]: K extends TProp ? ReturnType<T[K]['optional']> : T[K];
20
41
  };
21
- type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T> = {
42
+ type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T> = {
22
43
  [K in keyof T]: K extends TProp ? ReturnType<T[K]['required']> : T[K];
23
44
  };
24
- type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T, TSchema extends SchemaBuilder<any, any>> = {
45
+ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T, TSchema extends SchemaBuilder<any, any, any, any, any>> = {
25
46
  [K in keyof T]: K extends TProp ? TSchema : T[K];
26
47
  };
48
+ export type ObjectSchemaValidationResult<T, TRootSchema extends ObjectSchemaBuilder<any, any, any, any, any, any>, TSchema extends ObjectSchemaBuilder<any, any, any, any, any, any> = TRootSchema> = Omit<ValidationResult<T>, 'errors'> & {
49
+ /**
50
+ * A flat list of validation errors.
51
+ *
52
+ * @deprecated Use {@link ObjectSchemaValidationResult.getErrorsFor | getErrorsFor()} instead for
53
+ * per-property error inspection with type-safe property selectors. The `errors` array on
54
+ * `ObjectSchemaBuilder` validation results will be removed in a future major version.
55
+ */
56
+ errors?: ValidationError[];
57
+ /**
58
+ * Returns a nested validation error for the property selected by the `selector` function.
59
+ * This is the **recommended** way to inspect validation errors — it provides type-safe,
60
+ * per-property error details including `isValid`, `errors`, and `seenValue`.
61
+ *
62
+ * Prefer this over the deprecated `errors` array.
63
+ *
64
+ * @param selector a callback function to select property from the schema.
65
+ */
66
+ getErrorsFor<TPropertySchema, TParentPropertyDescriptor>(selector?: (properties: PropertyDescriptorTree<TSchema, TRootSchema>) => PropertyDescriptor<TRootSchema, TPropertySchema, TParentPropertyDescriptor>): TPropertySchema extends ObjectSchemaBuilder<any, any, any, any, any, any> ? PropertyValidationResult<TPropertySchema, TRootSchema, TParentPropertyDescriptor> : NestedValidationResult<TPropertySchema, TRootSchema, TParentPropertyDescriptor>;
67
+ };
27
68
  /**
28
69
  * Object schema builder class. Similar to the `object` type
29
70
  * in JS. Allows to define a schema for `object` value.
@@ -45,7 +86,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
45
86
  * age: number()
46
87
  * });
47
88
  *
48
- * const result = await schema.validate({
89
+ * const result = schema.validate({
49
90
  * name: 'John',
50
91
  * age: 30
51
92
  * });
@@ -61,7 +102,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
61
102
  * age: number().optional()
62
103
  * });
63
104
  *
64
- * const result = await schema.validate({
105
+ * const result = schema.validate({
65
106
  * name: 'John'
66
107
  * });
67
108
  * // result.valid === true
@@ -74,12 +115,13 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
74
115
  * name: string(),
75
116
  * age: number();
76
117
  * });
77
- * const result = await schema.validate({
118
+ * const result = schema.validate({
78
119
  * name: 'John'
79
120
  * });
80
121
  *
81
122
  * // result.valid === false
82
- * // result.errors[0].message === "is expected to have property 'age'"
123
+ * // result.errors is deprecated use result.getErrorsFor() instead
124
+ * // result.getErrorsFor((p) => p.age).errors // ["is expected to have property 'age'"]
83
125
  * ```
84
126
  *
85
127
  * @example
@@ -91,7 +133,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
91
133
  * country: string()
92
134
  * })
93
135
  * });
94
- * const result = await schema.validate({
136
+ * const result = schema.validate({
95
137
  * name: 'John',
96
138
  * address: {
97
139
  * city: 'New York',
@@ -109,11 +151,16 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
109
151
  * ```
110
152
  * @see {@link object}
111
153
  */
112
- export declare class ObjectSchemaBuilder<TProperties extends Record<string, SchemaBuilder<any, any>> = {}, TRequired extends boolean = true, TExplicitType = undefined> extends SchemaBuilder<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType, TRequired> {
154
+ export declare class ObjectSchemaBuilder<TProperties extends Record<string, SchemaBuilder<any, any, any, any, any>> = {}, TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions> {
113
155
  #private;
114
- static create<P extends Record<string, SchemaBuilder>, R extends boolean>(props: ObjectSchemaBuilderCreateProps<P, R>): ObjectSchemaBuilder<{}, true, undefined>;
156
+ /** Marks this builder as having sub-properties for descriptor tree recursion. */
157
+ readonly [SYMBOL_HAS_PROPERTIES] = true;
158
+ /**
159
+ * @hidden
160
+ */
161
+ static create<P extends Record<string, SchemaBuilder>, R extends boolean>(props: ObjectSchemaBuilderCreateProps<P, R>): ObjectSchemaBuilder<{}, true, false, undefined, false, {}>;
115
162
  protected createFromProps<T extends Record<string, SchemaBuilder>, R extends boolean = true>(props: ObjectSchemaBuilderCreateProps<T, R>): this;
116
- private constructor();
163
+ protected constructor(props: ObjectSchemaBuilderCreateProps);
117
164
  introspect(): {
118
165
  /**
119
166
  * Properties defined in schema
@@ -128,49 +175,117 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
128
175
  acceptUnknownProps: boolean;
129
176
  type: string;
130
177
  isRequired: boolean;
131
- preprocessors: readonly import("./SchemaBuilder.js").Preprocessor<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
132
- validators: readonly import("./SchemaBuilder.js").Validator<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
178
+ isNullable: boolean;
179
+ isReadonly: boolean;
180
+ preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
181
+ validators: readonly import("./SchemaBuilder.js").ValidatorEntry<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
182
+ requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
183
+ extensions: {
184
+ [x: string]: unknown;
185
+ };
186
+ hasDefault: boolean;
187
+ defaultValue: (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | (() => undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | undefined;
188
+ description: string | undefined;
189
+ hasCatch: boolean;
190
+ catchValue: (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | (() => undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | undefined;
133
191
  };
134
192
  /**
135
193
  * @hidden
136
194
  */
137
- required(): ObjectSchemaBuilder<TProperties, true, TExplicitType>;
195
+ required(errorMessage?: ValidationErrorMessageProvider): ObjectSchemaBuilder<TProperties, true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
196
+ /**
197
+ * @hidden
198
+ */
199
+ optional(): ObjectSchemaBuilder<TProperties, false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
200
+ /**
201
+ * @hidden
202
+ */
203
+ default(value: (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | (() => undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType)): ObjectSchemaBuilder<TProperties, true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
138
204
  /**
139
205
  * @hidden
140
206
  */
141
- optional(): ObjectSchemaBuilder<TProperties, false, TExplicitType>;
207
+ clearDefault(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
142
208
  /**
143
- * Performs validion of object schema over the `object`.
209
+ * @hidden
210
+ */
211
+ brand<TBrand extends string | symbol>(_name?: TBrand): ObjectSchemaBuilder<TProperties, TRequired, TNullable, (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) & {
212
+ readonly [K in BRAND]: TBrand;
213
+ }, THasDefault, TExtensions> & TExtensions;
214
+ /**
215
+ * Marks the inferred type as `Readonly<T>` — all top-level properties
216
+ * become `readonly` at the type level. Validation behaviour is unchanged.
217
+ *
218
+ * @see {@link SchemaBuilder.readonly}
219
+ */
220
+ readonly(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, Readonly<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>, THasDefault, TExtensions> & TExtensions;
221
+ protected preValidateSync(object: any, context?: ValidationContext<this>): PreValidationResult<InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>>, {
222
+ validatedObject: any;
223
+ }>;
224
+ protected preValidateAsync(object: any, context?: ValidationContext<this>): Promise<PreValidationResult<InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>>, {
225
+ validatedObject: any;
226
+ }>>;
227
+ /**
228
+ * Performs synchronous validation of object schema over the `object`.
229
+ * Throws if any preprocessor, validator, or error message provider returns a Promise.
230
+ *
231
+ * The returned result includes a `getErrorsFor()` method for type-safe,
232
+ * per-property error inspection.
233
+ *
234
+ * @param object The object to validate against this schema.
144
235
  * @param context Optional `ValidationContext` settings.
145
236
  */
146
- validate(object: undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType, context?: ValidationContext): Promise<ValidationResult<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>>;
237
+ validate(object: undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionalityForInput<TProperties>> : TExplicitType, TRequired>> : TExplicitType, context?: ValidationContext<this>): ObjectSchemaValidationResult<undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>> : TExplicitType, this>;
238
+ /**
239
+ * @param object The object to validate against this schema.
240
+ * @param context Optional `ValidationContext` settings.
241
+ */
242
+ validateAsync(object: undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionalityForInput<TProperties>> : TExplicitType, TRequired>> : TExplicitType, context?: ValidationContext<this>): Promise<ObjectSchemaValidationResult<undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>> : TExplicitType, this>>;
243
+ /**
244
+ * Performs synchronous validation of object schema over the `object`.
245
+ * Throws if any preprocessor, validator, or error message provider returns a Promise.
246
+ *
247
+ * The returned result includes a `getErrorsFor()` method for type-safe,
248
+ * per-property error inspection.
249
+ *
250
+ * @param object The object to validate against this schema.
251
+ * @param context Optional `ValidationContext` settings.
252
+ */
253
+ protected _validate(object: undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionalityForInput<TProperties>> : TExplicitType, TRequired>> : TExplicitType, context?: ValidationContext<this>): ObjectSchemaValidationResult<undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>> : TExplicitType, this>;
254
+ /**
255
+ * Performs async validation of object schema over the `object`.
256
+ * Supports async preprocessors, validators, and error message providers.
257
+ *
258
+ * @param object The object to validate against this schema.
259
+ * @param context Optional `ValidationContext` settings.
260
+ */
261
+ protected _validateAsync(object: undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionalityForInput<TProperties>> : TExplicitType, TRequired>> : TExplicitType, context?: ValidationContext<this>): Promise<ObjectSchemaValidationResult<undefined extends TExplicitType ? InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>> : TExplicitType, this>>;
147
262
  /**
148
263
  * Fields not defined in `properties` will not be validated
149
264
  * and will be passed through the validation.
150
265
  */
151
- acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>;
266
+ acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
152
267
  /**
153
268
  * Fields not defined in `properties` will be considered
154
269
  * as schema violation. This is the default behavior.
155
270
  */
156
- notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>;
271
+ notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
157
272
  /**
158
- * @hidden
273
+ * @inheritdoc
159
274
  */
160
- hasType<T>(notUsed?: T): ObjectSchemaBuilder<TProperties, TRequired, T>;
275
+ hasType<T>(_notUsed?: T): ObjectSchemaBuilder<TProperties, TRequired, TNullable, T, THasDefault, TExtensions> & TExtensions;
161
276
  /**
162
- * @hidden
277
+ * @inheritdoc
163
278
  */
164
- clearHasType(): ObjectSchemaBuilder<TProperties, TRequired>;
279
+ clearHasType(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
165
280
  /**
166
281
  * Adds a new property to the object schema. The new property
167
282
  * will be validated according to the provided schema.
168
283
  * @param propName name of the new property
169
284
  * @param schema schema builder of the new property
170
285
  */
171
- addProp<TType extends SchemaBuilder<any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
286
+ addProp<TType extends SchemaBuilder<any, any, any, any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
172
287
  [k in TName]: TType;
173
- }, TRequired, TExplicitType>;
288
+ }, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
174
289
  /**
175
290
  * @hidden
176
291
  * @deprecated this is for internal use, do not use if you are
@@ -185,82 +300,141 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
185
300
  * comments were lost. Hopefully it will be fixed in the future by Typescript team or somebody will
186
301
  * find a workaround/fix and create a pull request.
187
302
  */
188
- optimize(): SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType>;
303
+ optimize(): SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions>;
189
304
  /**
190
305
  * Adds new properties to the object schema. The same as `.addProp()` but
191
306
  * allows to add multiple properties with one call. The new properties
192
307
  * will be validated according to the provided schemas.
193
308
  * @param props a key/schema object map.
194
309
  */
195
- addProps<TProps extends Record<string, SchemaBuilder<any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps, true, undefined>;
310
+ addProps<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
196
311
  /**
197
312
  * Adds all properties from the `schema` object schema to the current schema.
198
313
  * @param schema an instance of `ObjectSchemaBuilder`
199
314
  */
200
- addProps<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProp, infer TReq, infer TExpType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProp> & TProp, TRequired, TExplicitType> : never;
315
+ addProps<K extends ObjectSchemaBuilder<any, any, any, any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProp, infer _, any, infer __> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProp> & TProp, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions : never;
201
316
  /**
202
317
  * Omits properties listed in `properties` from the schema.
203
318
  * Consider `Omit<Type, 'prop1'|'prop2'...>` as a good illustration
204
319
  * from the TS world.
205
320
  * @param properties - array of property names (strings) to remove from the schema.
206
321
  */
207
- omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TExplicitType>;
322
+ omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
208
323
  /**
209
324
  * Removes `propName` from the list of properties.
210
325
  * @param propName property name to remove. Schema should contain
211
326
  * this property. An error will be thrown otherwise.
212
327
  */
213
- omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TExplicitType>;
328
+ omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
214
329
  /**
215
330
  * Removes all properties of `schema` from the current schema.
216
331
  * `Omit<TSchema, keyof TAnotherSchema>` as a good illustration
217
332
  * from the TS world.
218
333
  * @param schema schema builder to take properties from.
219
334
  */
220
- omit<T>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TRequired, infer TExplicitType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps>, TRequired, TExplicitType> : never;
335
+ omit<T>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TRequired, any, infer TExplicitType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions : never;
221
336
  /**
222
337
  * Adds all properties from `schema` to the current schema.
223
338
  * `TSchema & TAnotherSchema` is a good example of the similar concept
224
339
  * in the TS type system.
225
340
  * @param schema an object schema to take properties from
226
341
  */
227
- intersect<T extends ObjectSchemaBuilder<any, any, any>>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TReq, infer TExplType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps> & TProps, TRequired, TExplType> : never;
342
+ intersect<T extends ObjectSchemaBuilder<any, any, any, any, any, any>>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer _, any, infer TExplType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps> & TProps, TRequired, TNullable, TExplType, THasDefault, TExtensions> & TExtensions : never;
228
343
  /**
229
344
  * Marks all properties in the current schema as optional.
230
345
  * It is the same as call `.optional('propname')` where `propname` is the name
231
346
  * of every property in the schema.
232
347
  */
233
- partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType>;
348
+ partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
234
349
  /**
235
350
  * Marks all properties from `properties` as optional in the schema.
236
351
  * @param properties list of property names (string) to make optional
237
352
  */
238
- partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TExplicitType>;
353
+ partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
239
354
  /**
240
355
  * Marks property `propName` as optional in the schema.
241
356
  * @param propName the name of the property (string).
242
357
  */
243
- partial<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty> & Pick<MakeChildrenOptional<TProperties>, TProperty>, TRequired, TExplicitType>;
358
+ partial<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty> & Pick<MakeChildrenOptional<TProperties>, TProperty>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
359
+ /**
360
+ * Recursively marks all properties — and all properties of nested
361
+ * `object()` schemas — as optional. Useful for PATCH API bodies
362
+ * and partial form state where every field at every level is optional.
363
+ *
364
+ * Only nested `ObjectSchemaBuilder` schemas are recursed into.
365
+ * Other schema types (arrays, unions, primitives, lazy) are made
366
+ * optional at the top level but their internals are not modified.
367
+ *
368
+ * @example
369
+ * ```ts
370
+ * const Address = object({
371
+ * street: string(),
372
+ * city: string()
373
+ * });
374
+ *
375
+ * const User = object({
376
+ * name: string(),
377
+ * address: Address
378
+ * });
379
+ *
380
+ * const PatchUser = User.deepPartial();
381
+ * // PatchUser infers as:
382
+ * // { name?: string; address?: { street?: string; city?: string } }
383
+ *
384
+ * PatchUser.validate({ address: { city: 'Paris' } }); // valid
385
+ * PatchUser.validate({}); // valid
386
+ * ```
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * // Three-level nesting
391
+ * const schema = object({
392
+ * a: object({
393
+ * b: object({ c: string() })
394
+ * })
395
+ * }).deepPartial();
396
+ *
397
+ * schema.validate({}); // valid
398
+ * schema.validate({ a: {} }); // valid
399
+ * schema.validate({ a: { b: {} } }); // valid
400
+ * ```
401
+ *
402
+ * @example
403
+ * ```ts
404
+ * // PATCH API body
405
+ * const CreateBody = object({
406
+ * profile: object({ displayName: string(), bio: string() }),
407
+ * settings: object({ theme: string(), language: string() })
408
+ * });
409
+ *
410
+ * const PatchBody = CreateBody.deepPartial();
411
+ * // All fields are optional at every level —
412
+ * // send only what you want to update.
413
+ * ```
414
+ *
415
+ * @see {@link partial} for shallow-only property optionality.
416
+ */
417
+ deepPartial(): ObjectSchemaBuilder<DeepMakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
244
418
  /**
245
419
  * Returns a new schema containing only properties listed in
246
420
  * `properties` array.
247
421
  * @param properties array of property names (strings)
248
422
  */
249
- pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined>;
423
+ pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
250
424
  /**
251
425
  * Returns new schema based on the current schema. This new schema
252
426
  * will consists only from properties which names are taken from the
253
427
  * `schema` object schema.
254
428
  * @param schema schema to take property names list from
255
429
  */
256
- pick<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProps, infer T1, infer T2> ? ObjectSchemaBuilder<Omit<TProperties, keyof Omit<TProperties, keyof TProps>>, TRequired, undefined> : never;
430
+ pick<K extends ObjectSchemaBuilder<any, any, any, any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProps, infer _, any, infer __> ? ObjectSchemaBuilder<Omit<TProperties, keyof Omit<TProperties, keyof TProps>>, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions : never;
257
431
  /**
258
432
  * Returns a new schema consisting of only one property
259
433
  * (taken from the `property` property name). If the property
260
434
  * does not exists in the current schema, an error will be thrown.
261
435
  * @param property the name of the property (string).
262
436
  */
263
- pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined>;
437
+ pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
264
438
  /**
265
439
  * Modify schema for `propName` and return a new schema.
266
440
  * Could be useful if you want to leave all schema intact, but
@@ -270,43 +444,74 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
270
444
  * you will receive an old schema for `propName`.
271
445
  * @returns
272
446
  */
273
- modifyPropSchema<K extends keyof TProperties, R extends SchemaBuilder<any, any>>(propName: K, callback: (builder: TProperties[K]) => R): ObjectSchemaBuilder<ModifyPropSchema<TProperties, K, R>, TRequired, TExplicitType>;
447
+ modifyPropSchema<K extends keyof TProperties, R extends SchemaBuilder<any, any, any, any, any>>(propName: K, callback: (builder: TProperties[K]) => R): ObjectSchemaBuilder<ModifyPropSchema<TProperties, K, R>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
274
448
  /**
275
449
  * An alias for `.partial(prop: string)`
276
450
  * @param prop name of the property
277
451
  */
278
- makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TExplicitType>;
452
+ makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
279
453
  /**
280
454
  * Marks `prop` as required property.
281
455
  * If `prop` does not exists in the current schema,
282
456
  * an error will be thrown.
283
457
  * @param prop name of the property
284
458
  */
285
- makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TExplicitType>;
459
+ makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
286
460
  /**
287
461
  * `Partial<T>` would be a good example of the
288
462
  * same operation in the TS world.
289
463
  */
290
- makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType>;
464
+ makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
291
465
  /**
292
466
  * `Required<T>` would be a good example of the
293
467
  * same operation in the TS world.
294
468
  */
295
- makeAllPropsRequired(): ObjectSchemaBuilder<MakeChildrenRequired<TProperties>, TRequired, TExplicitType>;
469
+ makeAllPropsRequired(): ObjectSchemaBuilder<MakeChildrenRequired<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
470
+ static getPropertiesFor<TProperties extends Record<string, SchemaBuilder<any, any, any, any, any>> = {}, TRequired extends boolean = true, TExplicitType = undefined, TSchema extends ObjectSchemaBuilder<any, any, any, any> = ObjectSchemaBuilder<TProperties, TRequired, false, TExplicitType>>(schema: TSchema): PropertyDescriptorTree<TSchema, TSchema>;
471
+ static isValidPropertyDescriptor(descriptor: PropertyDescriptor<any, any, any>): boolean;
472
+ nullable(): ObjectSchemaBuilder<TProperties, TRequired, true, TExplicitType, THasDefault, TExtensions>;
473
+ notNullable(): ObjectSchemaBuilder<TProperties, TRequired, false, TExplicitType, THasDefault, TExtensions>;
296
474
  }
297
- /**
298
- * Defines a schema for empty object `{}`
299
- */
300
- export declare function object(): ObjectSchemaBuilder<{}, true>;
301
- /**
302
- * Defines an object schema, properties definitions are takens from `props`.
303
- * @param props key/schema object map for schema's properties.
304
- */
305
- export declare function object<TProps extends Record<string, SchemaBuilder<any, any>>>(props: TProps): ObjectSchemaBuilder<TProps, true>;
306
- type RequiredProps<T extends Record<string, SchemaBuilder<any, any>>> = keyof {
307
- [k in keyof T as T[k] extends SchemaBuilder<infer TRes, infer TReq> ? TReq extends true ? k : never : never]: T[k];
475
+ export interface Object {
476
+ /**
477
+ * Defines a schema for empty object `{}`
478
+ */
479
+ (): ObjectSchemaBuilder<{}, true>;
480
+ /**
481
+ * Defines an object schema, properties definitions are takens from `props`.
482
+ * @param props key/schema object map for schema's properties.
483
+ */
484
+ <TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ObjectSchemaBuilder<TProps, true>;
485
+ /**
486
+ * Defines an object schema, properties definitions are takens from `props`.
487
+ * @param props key/schema object map for schema's properties.
488
+ */
489
+ <TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props?: TProps): ObjectSchemaBuilder<TProps, true>;
490
+ /**
491
+ * Returns a tree of property descriptors for the given `schema`.
492
+ * The structure of the tree is the same as the structure of the `schema`.
493
+ * Which gives you an opportunity to access property descriptors for each
494
+ * property in the schema in a useful and type-safe way.
495
+ * @param schema
496
+ */
497
+ getPropertiesFor<TProperties extends Record<string, SchemaBuilder<any, any, any, any, any>> = {}, TRequired extends boolean = true, TExplicitType = undefined, TSchema extends ObjectSchemaBuilder<any, any, any, any> = ObjectSchemaBuilder<TProperties, TRequired, false, TExplicitType>>(schema: TSchema): PropertyDescriptorTree<TSchema, TSchema>;
498
+ /**
499
+ * Verifies if the given `descriptor` is a valid property descriptor.
500
+ * @param descriptor a property descriptor to check
501
+ */
502
+ isValidPropertyDescriptor(descriptor: PropertyDescriptor<any, any, any>): boolean;
503
+ }
504
+ declare const object: Object;
505
+ export { object };
506
+ type RequiredProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
507
+ [k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, any> ? TReq extends true ? k : never : never]: T[k];
508
+ };
509
+ type NotRequiredProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
510
+ [k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, any> ? TReq extends true ? never : k : never]: T[k];
511
+ };
512
+ type RequiredInputProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
513
+ [k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, infer THasDef> ? TReq extends true ? THasDef extends true ? never : k : never : never]: T[k];
308
514
  };
309
- type NotRequiredProps<T extends Record<string, SchemaBuilder<any, any>>> = keyof {
310
- [k in keyof T as T[k] extends SchemaBuilder<infer TRes, infer TReq> ? TReq extends true ? never : k : never]: T[k];
515
+ type NotRequiredInputProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
516
+ [k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, infer THasDef> ? TReq extends true ? THasDef extends true ? k : never : k : never]: T[k];
311
517
  };
312
- export {};