@cleverbrush/schema 0.0.0-beta-20260410073748

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 (85) hide show
  1. package/README.md +1409 -0
  2. package/dist/builders/AnySchemaBuilder.d.ts +116 -0
  3. package/dist/builders/AnySchemaBuilder.js +2 -0
  4. package/dist/builders/AnySchemaBuilder.js.map +1 -0
  5. package/dist/builders/ArraySchemaBuilder.d.ts +214 -0
  6. package/dist/builders/ArraySchemaBuilder.js +2 -0
  7. package/dist/builders/ArraySchemaBuilder.js.map +1 -0
  8. package/dist/builders/BooleanSchemaBuilder.d.ts +146 -0
  9. package/dist/builders/BooleanSchemaBuilder.js +2 -0
  10. package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
  11. package/dist/builders/DateSchemaBuilder.d.ts +277 -0
  12. package/dist/builders/DateSchemaBuilder.js +2 -0
  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 +246 -0
  18. package/dist/builders/FunctionSchemaBuilder.js +2 -0
  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 +273 -0
  23. package/dist/builders/NumberSchemaBuilder.js +2 -0
  24. package/dist/builders/NumberSchemaBuilder.js.map +1 -0
  25. package/dist/builders/ObjectSchemaBuilder.d.ts +517 -0
  26. package/dist/builders/ObjectSchemaBuilder.js +2 -0
  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 +947 -0
  33. package/dist/builders/StringSchemaBuilder.d.ts +291 -0
  34. package/dist/builders/StringSchemaBuilder.js +2 -0
  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 +227 -0
  40. package/dist/builders/UnionSchemaBuilder.js +2 -0
  41. package/dist/builders/UnionSchemaBuilder.js.map +1 -0
  42. package/dist/chunk-CTP4RHDG.js +2 -0
  43. package/dist/chunk-CTP4RHDG.js.map +1 -0
  44. package/dist/chunk-FWUEUW2N.js +2 -0
  45. package/dist/chunk-FWUEUW2N.js.map +1 -0
  46. package/dist/chunk-IJ6FO37G.js +2 -0
  47. package/dist/chunk-IJ6FO37G.js.map +1 -0
  48. package/dist/chunk-KESLT5EE.js +2 -0
  49. package/dist/chunk-KESLT5EE.js.map +1 -0
  50. package/dist/chunk-KFTO7TMB.js +2 -0
  51. package/dist/chunk-KFTO7TMB.js.map +1 -0
  52. package/dist/chunk-MKAYFPAR.js +2 -0
  53. package/dist/chunk-MKAYFPAR.js.map +1 -0
  54. package/dist/chunk-NXPH3ZUW.js +2 -0
  55. package/dist/chunk-NXPH3ZUW.js.map +1 -0
  56. package/dist/chunk-OOPF7RXS.js +2 -0
  57. package/dist/chunk-OOPF7RXS.js.map +1 -0
  58. package/dist/chunk-S5TC6NSU.js +2 -0
  59. package/dist/chunk-S5TC6NSU.js.map +1 -0
  60. package/dist/chunk-YWBNVHR6.js +2 -0
  61. package/dist/chunk-YWBNVHR6.js.map +1 -0
  62. package/dist/chunk-Z72HXYKX.js +2 -0
  63. package/dist/chunk-Z72HXYKX.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 -0
  82. package/dist/index.js +2 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/utils/transaction.d.ts +69 -0
  85. package/package.json +104 -0
@@ -0,0 +1,517 @@
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>;
10
+ type ObjectSchemaBuilderProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = ReturnType<ObjectSchemaBuilder<T, TRequired>['introspect']>;
11
+ type ObjectSchemaBuilderCreateProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = Partial<ObjectSchemaBuilderProps<T, TRequired>>;
12
+ type Id<T> = T extends infer U ? {
13
+ [K in keyof U]: U[K];
14
+ } : never;
15
+ export type RespectPropsOptionality<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
16
+ [K in RequiredProps<T>]: InferType<T[K]>;
17
+ } & {
18
+ [K in NotRequiredProps<T>]?: InferType<T[K]>;
19
+ };
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>>> = {
26
+ [K in keyof T]: ReturnType<T[K]['required']>;
27
+ };
28
+ type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
29
+ [K in keyof T]: ReturnType<T[K]['optional']>;
30
+ };
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> = {
40
+ [K in keyof T]: K extends TProp ? ReturnType<T[K]['optional']> : T[K];
41
+ };
42
+ type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T> = {
43
+ [K in keyof T]: K extends TProp ? ReturnType<T[K]['required']> : T[K];
44
+ };
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>> = {
46
+ [K in keyof T]: K extends TProp ? TSchema : T[K];
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
+ };
68
+ /**
69
+ * Object schema builder class. Similar to the `object` type
70
+ * in JS. Allows to define a schema for `object` value.
71
+ * Should be used to validate objects with specific properties.
72
+ * Properties should be defined as their own schema builders.
73
+ * You can use any `SchemaBuilder` e.g. `string()`, `number()`,
74
+ * `boolean()`, `array()`, `object()`, etc. to define properties.
75
+ * Which means that you can define nested objects and arrays of
76
+ * any complexity.
77
+ *
78
+ * **NOTE** this class is exported only to give opportunity to extend it
79
+ * by inheriting. It is not recommended to create an instance of this class
80
+ * directly. Use {@link object | object()} function instead.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const schema = object({
85
+ * name: string(),
86
+ * age: number()
87
+ * });
88
+ *
89
+ * const result = schema.validate({
90
+ * name: 'John',
91
+ * age: 30
92
+ * });
93
+ *
94
+ * // result.valid === true
95
+ * // result.object === { name: 'John', age: 30 }
96
+ * ```
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const schema = object({
101
+ * name: string(),
102
+ * age: number().optional()
103
+ * });
104
+ *
105
+ * const result = schema.validate({
106
+ * name: 'John'
107
+ * });
108
+ * // result.valid === true
109
+ * // result.object === { name: 'John' }
110
+ * ```
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const schema = object({
115
+ * name: string(),
116
+ * age: number();
117
+ * });
118
+ * const result = schema.validate({
119
+ * name: 'John'
120
+ * });
121
+ *
122
+ * // result.valid === false
123
+ * // result.errors is deprecated — use result.getErrorsFor() instead
124
+ * // result.getErrorsFor((p) => p.age).errors // ["is expected to have property 'age'"]
125
+ * ```
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const schema = object({
130
+ * name: string(),
131
+ * address: object({
132
+ * city: string(),
133
+ * country: string()
134
+ * })
135
+ * });
136
+ * const result = schema.validate({
137
+ * name: 'John',
138
+ * address: {
139
+ * city: 'New York',
140
+ * country: 'USA'
141
+ * }
142
+ * });
143
+ * // result.valid === true
144
+ * // result.object === {
145
+ * // name: 'John',
146
+ * // address: {
147
+ * // city: 'New York',
148
+ * // country: 'USA'
149
+ * // }
150
+ * // }
151
+ * ```
152
+ * @see {@link object}
153
+ */
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> {
155
+ #private;
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, {}>;
162
+ protected createFromProps<T extends Record<string, SchemaBuilder>, R extends boolean = true>(props: ObjectSchemaBuilderCreateProps<T, R>): this;
163
+ protected constructor(props: ObjectSchemaBuilderCreateProps);
164
+ introspect(): {
165
+ /**
166
+ * Properties defined in schema
167
+ */
168
+ properties: TProperties;
169
+ /**
170
+ * If set to `true`, schema validation will not
171
+ * return errors if object contains fields which
172
+ * are not defined in the schema `properties`.
173
+ * Set to `false` by default
174
+ */
175
+ acceptUnknownProps: boolean;
176
+ type: string;
177
+ isRequired: boolean;
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;
191
+ };
192
+ /**
193
+ * @hidden
194
+ */
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;
204
+ /**
205
+ * @hidden
206
+ */
207
+ clearDefault(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
208
+ /**
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.
235
+ * @param context Optional `ValidationContext` settings.
236
+ */
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>>;
262
+ /**
263
+ * Fields not defined in `properties` will not be validated
264
+ * and will be passed through the validation.
265
+ */
266
+ acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
267
+ /**
268
+ * Fields not defined in `properties` will be considered
269
+ * as schema violation. This is the default behavior.
270
+ */
271
+ notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
272
+ /**
273
+ * @inheritdoc
274
+ */
275
+ hasType<T>(_notUsed?: T): ObjectSchemaBuilder<TProperties, TRequired, TNullable, T, THasDefault, TExtensions> & TExtensions;
276
+ /**
277
+ * @inheritdoc
278
+ */
279
+ clearHasType(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
280
+ /**
281
+ * Adds a new property to the object schema. The new property
282
+ * will be validated according to the provided schema.
283
+ * @param propName name of the new property
284
+ * @param schema schema builder of the new property
285
+ */
286
+ addProp<TType extends SchemaBuilder<any, any, any, any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
287
+ [k in TName]: TType;
288
+ }, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
289
+ /**
290
+ * @hidden
291
+ * @deprecated this is for internal use, do not use if you are
292
+ * not sure you need it.
293
+ *
294
+ * TODO: This is used to avoid `&` in resulting types. For example,
295
+ * when you have a schema like `object({prop1: string()})` and then use `addProp({prop2: string()})` method,
296
+ * the resulting type without `optimize` will be something like `{prop1: string} & {prop2: string}`. Which
297
+ * is not we would like to have. Instead we want to have `{prop1: string, prop2: string}`. This is what
298
+ * `optimize` method does. However it is not always possible to do this optimization without losing
299
+ * JSDoc comments, which is sucks. For example, I had to disable optimization for UnionSchemas, because
300
+ * comments were lost. Hopefully it will be fixed in the future by Typescript team or somebody will
301
+ * find a workaround/fix and create a pull request.
302
+ */
303
+ optimize(): SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions>;
304
+ /**
305
+ * Adds new properties to the object schema. The same as `.addProp()` but
306
+ * allows to add multiple properties with one call. The new properties
307
+ * will be validated according to the provided schemas.
308
+ * @param props a key/schema object map.
309
+ */
310
+ addProps<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
311
+ /**
312
+ * Adds all properties from the `schema` object schema to the current schema.
313
+ * @param schema an instance of `ObjectSchemaBuilder`
314
+ */
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;
316
+ /**
317
+ * Omits properties listed in `properties` from the schema.
318
+ * Consider `Omit<Type, 'prop1'|'prop2'...>` as a good illustration
319
+ * from the TS world.
320
+ * @param properties - array of property names (strings) to remove from the schema.
321
+ */
322
+ omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
323
+ /**
324
+ * Removes `propName` from the list of properties.
325
+ * @param propName property name to remove. Schema should contain
326
+ * this property. An error will be thrown otherwise.
327
+ */
328
+ omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
329
+ /**
330
+ * Removes all properties of `schema` from the current schema.
331
+ * `Omit<TSchema, keyof TAnotherSchema>` as a good illustration
332
+ * from the TS world.
333
+ * @param schema schema builder to take properties from.
334
+ */
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;
336
+ /**
337
+ * Adds all properties from `schema` to the current schema.
338
+ * `TSchema & TAnotherSchema` is a good example of the similar concept
339
+ * in the TS type system.
340
+ * @param schema an object schema to take properties from
341
+ */
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;
343
+ /**
344
+ * Marks all properties in the current schema as optional.
345
+ * It is the same as call `.optional('propname')` where `propname` is the name
346
+ * of every property in the schema.
347
+ */
348
+ partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
349
+ /**
350
+ * Marks all properties from `properties` as optional in the schema.
351
+ * @param properties list of property names (string) to make optional
352
+ */
353
+ partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
354
+ /**
355
+ * Marks property `propName` as optional in the schema.
356
+ * @param propName the name of the property (string).
357
+ */
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;
418
+ /**
419
+ * Returns a new schema containing only properties listed in
420
+ * `properties` array.
421
+ * @param properties array of property names (strings)
422
+ */
423
+ pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
424
+ /**
425
+ * Returns new schema based on the current schema. This new schema
426
+ * will consists only from properties which names are taken from the
427
+ * `schema` object schema.
428
+ * @param schema schema to take property names list from
429
+ */
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;
431
+ /**
432
+ * Returns a new schema consisting of only one property
433
+ * (taken from the `property` property name). If the property
434
+ * does not exists in the current schema, an error will be thrown.
435
+ * @param property the name of the property (string).
436
+ */
437
+ pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
438
+ /**
439
+ * Modify schema for `propName` and return a new schema.
440
+ * Could be useful if you want to leave all schema intact, but
441
+ * change a type of one property.
442
+ * @param propName name of the property (string)
443
+ * @param callback callback function returning a new schema fo the `propName`. As a first parameter
444
+ * you will receive an old schema for `propName`.
445
+ * @returns
446
+ */
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;
448
+ /**
449
+ * An alias for `.partial(prop: string)`
450
+ * @param prop name of the property
451
+ */
452
+ makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
453
+ /**
454
+ * Marks `prop` as required property.
455
+ * If `prop` does not exists in the current schema,
456
+ * an error will be thrown.
457
+ * @param prop name of the property
458
+ */
459
+ makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
460
+ /**
461
+ * `Partial<T>` would be a good example of the
462
+ * same operation in the TS world.
463
+ */
464
+ makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
465
+ /**
466
+ * `Required<T>` would be a good example of the
467
+ * same operation in the TS world.
468
+ */
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>;
474
+ }
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];
514
+ };
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];
517
+ };
@@ -0,0 +1,2 @@
1
+ import{a,b}from"../chunk-NXPH3ZUW.js";import"../chunk-CTP4RHDG.js";export{a as ObjectSchemaBuilder,b as object};
2
+ //# sourceMappingURL=ObjectSchemaBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,68 @@
1
+ import { ObjectSchemaBuilder } from './ObjectSchemaBuilder.js';
2
+ import { type InferType, type NestedValidationResult, type PropertyDescriptorInner, type PropertyDescriptorTree } from './SchemaBuilder.js';
3
+ /**
4
+ * Mutable container for nested validation results associated with a specific
5
+ * property descriptor. Implements {@link NestedValidationResult} and tracks
6
+ * the seen value, accumulated error messages, and child results for
7
+ * nested object properties.
8
+ *
9
+ * Used internally by `ObjectSchemaBuilder` during validation to build up
10
+ * a tree of per-property validation results.
11
+ */
12
+ export declare class PropertyValidationResult<TSchema extends ObjectSchemaBuilder<any, any, any, any, any> = ObjectSchemaBuilder<any, any, any, any, any>, TRootSchema extends ObjectSchemaBuilder<any, any, any, any, any> = ObjectSchemaBuilder<any, any, any, any, any>, TParentPropertyDescriptor = any> implements NestedValidationResult<TSchema, TRootSchema, TParentPropertyDescriptor> {
13
+ #private;
14
+ /**
15
+ * The value that was seen at the property location described by the descriptor.
16
+ * Retrieves the value from the root object using the property descriptor's `getValue` method.
17
+ * Returns `undefined` if the property is not found.
18
+ */
19
+ get seenValue(): InferType<TSchema> | undefined;
20
+ /**
21
+ * The list of validation error messages accumulated for this property.
22
+ */
23
+ get errors(): ReadonlyArray<string>;
24
+ /**
25
+ * Whether validation passed for this property and all of its children.
26
+ * Returns `true` only when there are no errors and no child errors.
27
+ */
28
+ get isValid(): boolean;
29
+ /**
30
+ * Returns the list of child `NestedValidationResult` instances
31
+ * representing validation results for nested properties.
32
+ */
33
+ getChildErrors(): ReadonlyArray<NestedValidationResult<any, any, any>>;
34
+ /**
35
+ * The inner property descriptor providing `getValue`, `setValue`, and `getSchema`
36
+ * operations for the property this error relates to.
37
+ */
38
+ get descriptor(): PropertyDescriptorInner<TRootSchema, TSchema, TParentPropertyDescriptor>;
39
+ /**
40
+ * Creates a new `PropertyValidationResult`.
41
+ *
42
+ * @param descriptor - the property descriptor tree node this error is associated with;
43
+ * must be a valid descriptor (checked via `ObjectSchemaBuilder.isValidPropertyDescriptor`)
44
+ * @param rootObjectValue - the root object being validated, used to resolve property values
45
+ * @param errors - optional initial list of error message strings
46
+ * @throws if `descriptor` is not a valid property descriptor
47
+ */
48
+ constructor(descriptor: PropertyDescriptorTree<TSchema, TRootSchema, any, TParentPropertyDescriptor>, rootObjectValue: InferType<TRootSchema> | undefined, errors?: string[]);
49
+ /**
50
+ * Appends a validation error message to this property's error list.
51
+ * @param error - the error message string to add
52
+ */
53
+ addError(error: string): void;
54
+ /**
55
+ * Appends a child `NestedValidationResult` for a nested property.
56
+ * @param childError - the child validation result to add
57
+ */
58
+ addChildError(childError: NestedValidationResult<any, any, any>): void;
59
+ /**
60
+ * Returns a JSON-serializable representation of this validation result.
61
+ * This ensures `JSON.stringify` includes `isValid` and `errors`,
62
+ * which are otherwise non-enumerable prototype getters.
63
+ */
64
+ toJSON(): {
65
+ isValid: boolean;
66
+ errors: ReadonlyArray<string>;
67
+ };
68
+ }