@cleverbrush/schema 1.1.11 → 3.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.
- package/README.md +1721 -104
- package/dist/builders/AnySchemaBuilder.d.ts +54 -14
- package/dist/builders/AnySchemaBuilder.js +2 -112
- package/dist/builders/AnySchemaBuilder.js.map +1 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +126 -23
- package/dist/builders/ArraySchemaBuilder.js +2 -284
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +109 -19
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +154 -34
- package/dist/builders/DateSchemaBuilder.js +2 -433
- package/dist/builders/DateSchemaBuilder.js.map +1 -0
- package/dist/builders/ExternSchemaBuilder.d.ts +202 -0
- package/dist/builders/ExternSchemaBuilder.js +2 -0
- package/dist/builders/ExternSchemaBuilder.js.map +1 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +205 -18
- package/dist/builders/FunctionSchemaBuilder.js +2 -113
- package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
- package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
- package/dist/builders/LazySchemaBuilder.d.ts +169 -0
- package/dist/builders/NullSchemaBuilder.d.ts +162 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -31
- package/dist/builders/NumberSchemaBuilder.js +2 -386
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +486 -61
- package/dist/builders/ObjectSchemaBuilder.js +2 -589
- package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
- package/dist/builders/ParseStringSchemaBuilder.d.ts +204 -0
- package/dist/builders/ParseStringSchemaBuilder.js +2 -0
- package/dist/builders/ParseStringSchemaBuilder.js.map +1 -0
- package/dist/builders/PromiseSchemaBuilder.d.ts +213 -0
- package/dist/builders/PromiseSchemaBuilder.js +2 -0
- package/dist/builders/PromiseSchemaBuilder.js.map +1 -0
- package/dist/builders/PropertyValidationResult.d.ts +68 -0
- package/dist/builders/RecordSchemaBuilder.d.ts +343 -0
- package/dist/builders/RecordSchemaBuilder.js +2 -0
- package/dist/builders/RecordSchemaBuilder.js.map +1 -0
- package/dist/builders/SchemaBuilder.d.ts +907 -30
- package/dist/builders/StringSchemaBuilder.d.ts +154 -37
- package/dist/builders/StringSchemaBuilder.js +2 -414
- package/dist/builders/StringSchemaBuilder.js.map +1 -0
- package/dist/builders/TupleSchemaBuilder.d.ts +250 -0
- package/dist/builders/TupleSchemaBuilder.js +2 -0
- package/dist/builders/TupleSchemaBuilder.js.map +1 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +141 -39
- package/dist/builders/UnionSchemaBuilder.js +2 -216
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-3JMDGYDT.js +2 -0
- package/dist/chunk-3JMDGYDT.js.map +1 -0
- package/dist/chunk-BUEVZ3KA.js +2 -0
- package/dist/chunk-BUEVZ3KA.js.map +1 -0
- package/dist/chunk-CFIJQ4GP.js +2 -0
- package/dist/chunk-CFIJQ4GP.js.map +1 -0
- package/dist/chunk-DY7J6RNN.js +2 -0
- package/dist/chunk-DY7J6RNN.js.map +1 -0
- package/dist/chunk-EIVZX4ZO.js +2 -0
- package/dist/chunk-EIVZX4ZO.js.map +1 -0
- package/dist/chunk-GXPV6UQK.js +2 -0
- package/dist/chunk-GXPV6UQK.js.map +1 -0
- package/dist/chunk-HN774HD7.js +2 -0
- package/dist/chunk-HN774HD7.js.map +1 -0
- package/dist/chunk-K6Z47OQY.js +2 -0
- package/dist/chunk-K6Z47OQY.js.map +1 -0
- package/dist/chunk-NUW3VXZV.js +2 -0
- package/dist/chunk-NUW3VXZV.js.map +1 -0
- package/dist/chunk-PHE4LIAN.js +2 -0
- package/dist/chunk-PHE4LIAN.js.map +1 -0
- package/dist/chunk-QARCEYGO.js +2 -0
- package/dist/chunk-QARCEYGO.js.map +1 -0
- package/dist/chunk-WDMJBGBD.js +2 -0
- package/dist/chunk-WDMJBGBD.js.map +1 -0
- package/dist/chunk-WQDYWDOE.js +2 -0
- package/dist/chunk-WQDYWDOE.js.map +1 -0
- package/dist/chunk-YQZHDMRF.js +2 -0
- package/dist/chunk-YQZHDMRF.js.map +1 -0
- package/dist/chunk-ZC6YBKCP.js +2 -0
- package/dist/chunk-ZC6YBKCP.js.map +1 -0
- package/dist/chunk-ZFI27R3L.js +2 -0
- package/dist/chunk-ZFI27R3L.js.map +1 -0
- package/dist/core.d.ts +28 -0
- package/dist/core.js +2 -0
- package/dist/core.js.map +1 -0
- package/dist/extension.d.ts +421 -0
- package/dist/extensions/array.d.ts +112 -0
- package/dist/extensions/enum.d.ts +190 -0
- package/dist/extensions/index.d.ts +112 -0
- package/dist/extensions/nullable.d.ts +26 -0
- package/dist/extensions/number.d.ts +228 -0
- package/dist/extensions/string.d.ts +332 -0
- package/dist/extensions/util.d.ts +45 -0
- package/dist/index.d.ts +10 -20
- package/dist/index.js +2 -19
- package/dist/index.js.map +1 -0
- package/dist/utils/transaction.d.ts +27 -4
- package/package.json +83 -7
- package/dist/builders/SchemaBuilder.js +0 -275
- package/dist/utils/transaction.js +0 -178
|
@@ -1,29 +1,145 @@
|
|
|
1
|
-
import {
|
|
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, 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
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Extracts the positional argument types from a schema whose inferred type is a
|
|
17
|
+
* function. When the branded `InferType<T>` resolves to a function type the
|
|
18
|
+
* parameter tuple is extracted; otherwise `any[]` is used as a safe fallback.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* type Args = ConstructorParams<typeof func().addParameter(string()).addParameter(number())>;
|
|
23
|
+
* // → [string, number]
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
type ConstructorParams<T extends SchemaBuilder<any, any, any, any, any>> = InferType<T> extends (...args: infer A) => any ? A : any[];
|
|
27
|
+
/**
|
|
28
|
+
* Converts a tuple of `FunctionSchemaBuilder`s into an intersection of
|
|
29
|
+
* `new (...args) => TInstance` call signatures, producing overloaded
|
|
30
|
+
* construct signatures in the inferred type.
|
|
31
|
+
*
|
|
32
|
+
* The recursion peels schemas off the front of `TSchemas` one at a time,
|
|
33
|
+
* each contributing one construct overload to the intersection.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* type Sigs = ConstructorSignatures<
|
|
38
|
+
* [typeof func().addParameter(string()), typeof func().addParameter(number())],
|
|
39
|
+
* { name: string }
|
|
40
|
+
* >;
|
|
41
|
+
* // → { new (p0: string): { name: string } }
|
|
42
|
+
* // & { new (p0: number): { name: string } }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
type ConstructorSignatures<TSchemas extends SchemaBuilder<any, any, any, any, any>[], TInstance> = TSchemas extends [
|
|
46
|
+
infer THead extends SchemaBuilder<any, any, any, any, any>,
|
|
47
|
+
...infer TTail extends SchemaBuilder<any, any, any, any, any>[]
|
|
48
|
+
] ? {
|
|
49
|
+
new (...args: ConstructorParams<THead>): TInstance;
|
|
50
|
+
} & ConstructorSignatures<TTail, TInstance> : unknown;
|
|
51
|
+
/**
|
|
52
|
+
* Wraps `TInstance` with constructor overload signatures derived from
|
|
53
|
+
* `TSchemas`. When the tuple is empty the type is returned unchanged
|
|
54
|
+
* (no-op); otherwise the result is an intersection of all construct
|
|
55
|
+
* signatures with `TInstance`.
|
|
56
|
+
*
|
|
57
|
+
* This is the type projected onto `InferType<ObjectSchemaBuilder>` when one
|
|
58
|
+
* or more constructors have been registered via `.addConstructor()`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* // No constructors — passthrough
|
|
63
|
+
* type A = WithConstructors<[], { name: string }>;
|
|
64
|
+
* // → { name: string }
|
|
65
|
+
*
|
|
66
|
+
* // One constructor
|
|
67
|
+
* type B = WithConstructors<[typeof func().addParameter(string())], { name: string }>;
|
|
68
|
+
* // → { new (p0: string): { name: string } } & { name: string }
|
|
69
|
+
*
|
|
70
|
+
* // Two constructors → overloaded construct signatures
|
|
71
|
+
* type C = WithConstructors<
|
|
72
|
+
* [typeof func().addParameter(string()), typeof func().addParameter(number())],
|
|
73
|
+
* { name: string }
|
|
74
|
+
* >;
|
|
75
|
+
* // → { new (p0: string): { name: string } }
|
|
76
|
+
* // & { new (p0: number): { name: string } }
|
|
77
|
+
* // & { name: string }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
type WithConstructors<TSchemas extends SchemaBuilder<any, any, any, any, any>[], TInstance> = TSchemas extends [] ? TInstance : ConstructorSignatures<TSchemas, TInstance> & TInstance;
|
|
81
|
+
export type RespectPropsOptionality<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
|
|
8
82
|
[K in RequiredProps<T>]: InferType<T[K]>;
|
|
9
83
|
} & {
|
|
10
84
|
[K in NotRequiredProps<T>]?: InferType<T[K]>;
|
|
11
85
|
};
|
|
12
|
-
type
|
|
86
|
+
type RespectPropsOptionalityForInput<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
|
|
87
|
+
[K in RequiredInputProps<T>]: InferType<T[K]>;
|
|
88
|
+
} & {
|
|
89
|
+
[K in NotRequiredInputProps<T>]?: InferType<T[K]>;
|
|
90
|
+
};
|
|
91
|
+
type MakeChildrenRequired<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
|
|
13
92
|
[K in keyof T]: ReturnType<T[K]['required']>;
|
|
14
93
|
};
|
|
15
|
-
type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any>>> = {
|
|
94
|
+
type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
|
|
16
95
|
[K in keyof T]: ReturnType<T[K]['optional']>;
|
|
17
96
|
};
|
|
18
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Recursively maps each property to its optional form, descending into
|
|
99
|
+
* nested `ObjectSchemaBuilder` schemas. All other schema types (arrays,
|
|
100
|
+
* unions, primitives) are only made optional at the top level.
|
|
101
|
+
*/
|
|
102
|
+
type DeepMakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = {
|
|
103
|
+
[K in keyof T]: T[K] extends ObjectSchemaBuilder<infer P extends Record<string, SchemaBuilder<any, any, any, any, any>>, any, any, any, any, any, any> ? ReturnType<ReturnType<T[K]['deepPartial']>['optional']> : ReturnType<T[K]['optional']>;
|
|
104
|
+
};
|
|
105
|
+
type MakeChildOptional<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T> = {
|
|
19
106
|
[K in keyof T]: K extends TProp ? ReturnType<T[K]['optional']> : T[K];
|
|
20
107
|
};
|
|
21
|
-
type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T> = {
|
|
108
|
+
type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any, any, any, any>>, TProp extends keyof T> = {
|
|
22
109
|
[K in keyof T]: K extends TProp ? ReturnType<T[K]['required']> : T[K];
|
|
23
110
|
};
|
|
24
|
-
type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T, TSchema extends SchemaBuilder<any, any>> = {
|
|
111
|
+
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
112
|
[K in keyof T]: K extends TProp ? TSchema : T[K];
|
|
26
113
|
};
|
|
114
|
+
export type ObjectSchemaValidationResult<T, TRootSchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any>, TSchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any> = TRootSchema> = Omit<ValidationResult<T>, 'errors'> & {
|
|
115
|
+
/**
|
|
116
|
+
* A flat list of validation errors.
|
|
117
|
+
*
|
|
118
|
+
* @deprecated Use {@link ObjectSchemaValidationResult.getErrorsFor | getErrorsFor()} instead for
|
|
119
|
+
* per-property error inspection with type-safe property selectors. The `errors` array on
|
|
120
|
+
* `ObjectSchemaBuilder` validation results will be removed in a future major version.
|
|
121
|
+
*/
|
|
122
|
+
errors?: ValidationError[];
|
|
123
|
+
/**
|
|
124
|
+
* Returns a nested validation error for the property selected by the `selector` function.
|
|
125
|
+
* This is the **recommended** way to inspect validation errors — it provides type-safe,
|
|
126
|
+
* per-property error details including `isValid`, `errors`, and `seenValue`.
|
|
127
|
+
*
|
|
128
|
+
* Prefer this over the deprecated `errors` array.
|
|
129
|
+
*
|
|
130
|
+
* @param selector a callback function to select property from the schema.
|
|
131
|
+
*/
|
|
132
|
+
getErrorsFor<TPropertySchema, TParentPropertyDescriptor>(selector?: (properties: PropertyDescriptorTree<TSchema, TRootSchema>) => PropertyDescriptor<TRootSchema, TPropertySchema, TParentPropertyDescriptor>): TPropertySchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any> ? PropertyValidationResult<TPropertySchema, TRootSchema, TParentPropertyDescriptor> : NestedValidationResult<TPropertySchema, TRootSchema, TParentPropertyDescriptor>;
|
|
133
|
+
/**
|
|
134
|
+
* Returns a list of all property validation results that have direct
|
|
135
|
+
* validation errors. Each entry exposes `.descriptor` (with
|
|
136
|
+
* `.toJsonPointer()` for the property's path) and `.errors`.
|
|
137
|
+
*
|
|
138
|
+
* This is useful for collecting all errors with their full JSON Pointer
|
|
139
|
+
* paths, e.g. for building RFC 9457 Problem Details responses.
|
|
140
|
+
*/
|
|
141
|
+
getInvalidProperties(): ReadonlyArray<NestedValidationResult<any, TRootSchema, any>>;
|
|
142
|
+
};
|
|
27
143
|
/**
|
|
28
144
|
* Object schema builder class. Similar to the `object` type
|
|
29
145
|
* in JS. Allows to define a schema for `object` value.
|
|
@@ -45,7 +161,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
|
|
|
45
161
|
* age: number()
|
|
46
162
|
* });
|
|
47
163
|
*
|
|
48
|
-
* const result =
|
|
164
|
+
* const result = schema.validate({
|
|
49
165
|
* name: 'John',
|
|
50
166
|
* age: 30
|
|
51
167
|
* });
|
|
@@ -61,7 +177,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
|
|
|
61
177
|
* age: number().optional()
|
|
62
178
|
* });
|
|
63
179
|
*
|
|
64
|
-
* const result =
|
|
180
|
+
* const result = schema.validate({
|
|
65
181
|
* name: 'John'
|
|
66
182
|
* });
|
|
67
183
|
* // result.valid === true
|
|
@@ -74,12 +190,13 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
|
|
|
74
190
|
* name: string(),
|
|
75
191
|
* age: number();
|
|
76
192
|
* });
|
|
77
|
-
* const result =
|
|
193
|
+
* const result = schema.validate({
|
|
78
194
|
* name: 'John'
|
|
79
195
|
* });
|
|
80
196
|
*
|
|
81
197
|
* // result.valid === false
|
|
82
|
-
* // result.errors
|
|
198
|
+
* // result.errors is deprecated — use result.getErrorsFor() instead
|
|
199
|
+
* // result.getErrorsFor((p) => p.age).errors // ["is expected to have property 'age'"]
|
|
83
200
|
* ```
|
|
84
201
|
*
|
|
85
202
|
* @example
|
|
@@ -91,7 +208,7 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
|
|
|
91
208
|
* country: string()
|
|
92
209
|
* })
|
|
93
210
|
* });
|
|
94
|
-
* const result =
|
|
211
|
+
* const result = schema.validate({
|
|
95
212
|
* name: 'John',
|
|
96
213
|
* address: {
|
|
97
214
|
* city: 'New York',
|
|
@@ -109,11 +226,16 @@ type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp exte
|
|
|
109
226
|
* ```
|
|
110
227
|
* @see {@link object}
|
|
111
228
|
*/
|
|
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
|
|
229
|
+
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 = {}, TConstructorSchemas extends SchemaBuilder<any, any, any, any, any>[] = []> extends SchemaBuilder<undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions> {
|
|
113
230
|
#private;
|
|
114
|
-
|
|
231
|
+
/** Marks this builder as having sub-properties for descriptor tree recursion. */
|
|
232
|
+
readonly [SYMBOL_HAS_PROPERTIES] = true;
|
|
233
|
+
/**
|
|
234
|
+
* @hidden
|
|
235
|
+
*/
|
|
236
|
+
static create<P extends Record<string, SchemaBuilder>, R extends boolean>(props: ObjectSchemaBuilderCreateProps<P, R>): ObjectSchemaBuilder<{}, true, false, undefined, false, {}, []>;
|
|
115
237
|
protected createFromProps<T extends Record<string, SchemaBuilder>, R extends boolean = true>(props: ObjectSchemaBuilderCreateProps<T, R>): this;
|
|
116
|
-
|
|
238
|
+
protected constructor(props: ObjectSchemaBuilderCreateProps);
|
|
117
239
|
introspect(): {
|
|
118
240
|
/**
|
|
119
241
|
* Properties defined in schema
|
|
@@ -126,51 +248,264 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
|
|
|
126
248
|
* Set to `false` by default
|
|
127
249
|
*/
|
|
128
250
|
acceptUnknownProps: boolean;
|
|
251
|
+
/**
|
|
252
|
+
* The list of constructor schemas registered via `.addConstructor()`.
|
|
253
|
+
* Each element is a `FunctionSchemaBuilder` whose inferred function
|
|
254
|
+
* type provides one overloaded construct signature in `InferType`.
|
|
255
|
+
* Empty array when no constructors have been added.
|
|
256
|
+
*/
|
|
257
|
+
constructorSchemas: TConstructorSchemas;
|
|
129
258
|
type: string;
|
|
130
259
|
isRequired: boolean;
|
|
131
|
-
|
|
132
|
-
|
|
260
|
+
isNullable: boolean;
|
|
261
|
+
isReadonly: boolean;
|
|
262
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType>[];
|
|
263
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType>[];
|
|
264
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
265
|
+
extensions: {
|
|
266
|
+
[x: string]: unknown;
|
|
267
|
+
};
|
|
268
|
+
hasDefault: boolean;
|
|
269
|
+
defaultValue: (undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType) | (() => undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType) | undefined;
|
|
270
|
+
description: string | undefined;
|
|
271
|
+
schemaName: string | undefined;
|
|
272
|
+
hasCatch: boolean;
|
|
273
|
+
catchValue: (undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType) | (() => undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType) | undefined;
|
|
274
|
+
example: unknown;
|
|
133
275
|
};
|
|
134
276
|
/**
|
|
135
277
|
* @hidden
|
|
136
278
|
*/
|
|
137
|
-
required(): ObjectSchemaBuilder<TProperties, true, TExplicitType
|
|
279
|
+
required(errorMessage?: ValidationErrorMessageProvider): ObjectSchemaBuilder<TProperties, true, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
280
|
+
/**
|
|
281
|
+
* @hidden
|
|
282
|
+
*/
|
|
283
|
+
optional(): ObjectSchemaBuilder<TProperties, false, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
284
|
+
/**
|
|
285
|
+
* @hidden
|
|
286
|
+
*/
|
|
287
|
+
default(value: (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) | (() => undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType)): ObjectSchemaBuilder<TProperties, true, TNullable, TExplicitType, true, TExtensions, TConstructorSchemas> & TExtensions;
|
|
138
288
|
/**
|
|
139
289
|
* @hidden
|
|
140
290
|
*/
|
|
141
|
-
|
|
291
|
+
clearDefault(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, false, TExtensions, TConstructorSchemas> & TExtensions;
|
|
292
|
+
/**
|
|
293
|
+
* @hidden
|
|
294
|
+
*/
|
|
295
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): ObjectSchemaBuilder<TProperties, TRequired, TNullable, (undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType) & {
|
|
296
|
+
readonly [K in BRAND]: TBrand;
|
|
297
|
+
}, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
298
|
+
/**
|
|
299
|
+
* Marks the inferred type as `Readonly<T>` — all top-level properties
|
|
300
|
+
* become `readonly` at the type level. Validation behaviour is unchanged.
|
|
301
|
+
*
|
|
302
|
+
* @see {@link SchemaBuilder.readonly}
|
|
303
|
+
*/
|
|
304
|
+
readonly(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, Readonly<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
305
|
+
protected preValidateSync(object: any, context?: ValidationContext<this>): PreValidationResult<InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>>, {
|
|
306
|
+
validatedObject: any;
|
|
307
|
+
}>;
|
|
308
|
+
protected preValidateAsync(object: any, context?: ValidationContext<this>): Promise<PreValidationResult<InferType<SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType, TRequired>>, {
|
|
309
|
+
validatedObject: any;
|
|
310
|
+
}>>;
|
|
311
|
+
/**
|
|
312
|
+
* Adds a `validator` to validators list.
|
|
313
|
+
*
|
|
314
|
+
* Object-level validators can return errors with a `property` selector
|
|
315
|
+
* to route the error to a specific property, making it visible via
|
|
316
|
+
* `getErrorsFor()`.
|
|
317
|
+
*
|
|
318
|
+
* ```ts
|
|
319
|
+
* schema.addValidator((value) => ({
|
|
320
|
+
* valid: false,
|
|
321
|
+
* errors: [{
|
|
322
|
+
* message: 'Passwords do not match',
|
|
323
|
+
* property: (t) => t.confirmPassword
|
|
324
|
+
* }]
|
|
325
|
+
* }));
|
|
326
|
+
* ```
|
|
327
|
+
*
|
|
328
|
+
* The `property` selector uses the same `PropertyDescriptorTree`
|
|
329
|
+
* as `getErrorsFor()` and react-form's `forProperty`.
|
|
330
|
+
*/
|
|
331
|
+
addValidator(validator: (object: undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, RespectPropsOptionality<TProperties>> : TExplicitType) => {
|
|
332
|
+
valid: boolean;
|
|
333
|
+
errors?: Array<{
|
|
334
|
+
message: string;
|
|
335
|
+
property?: (properties: PropertyDescriptorTree<ObjectSchemaBuilder<TProperties>>) => PropertyDescriptor<any, any, any>;
|
|
336
|
+
}>;
|
|
337
|
+
} | Promise<{
|
|
338
|
+
valid: boolean;
|
|
339
|
+
errors?: Array<{
|
|
340
|
+
message: string;
|
|
341
|
+
property?: (properties: PropertyDescriptorTree<ObjectSchemaBuilder<TProperties>>) => PropertyDescriptor<any, any, any>;
|
|
342
|
+
}>;
|
|
343
|
+
}>, options?: {
|
|
344
|
+
mutates?: boolean;
|
|
345
|
+
}): this;
|
|
346
|
+
/**
|
|
347
|
+
* Performs synchronous validation of object schema over the `object`.
|
|
348
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
349
|
+
*
|
|
350
|
+
* The returned result includes a `getErrorsFor()` method for type-safe,
|
|
351
|
+
* per-property error inspection.
|
|
352
|
+
*
|
|
353
|
+
* @param object The object to validate against this schema.
|
|
354
|
+
* @param context Optional `ValidationContext` settings.
|
|
355
|
+
*/
|
|
356
|
+
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>;
|
|
142
357
|
/**
|
|
143
|
-
*
|
|
358
|
+
* @param object The object to validate against this schema.
|
|
144
359
|
* @param context Optional `ValidationContext` settings.
|
|
145
360
|
*/
|
|
146
|
-
|
|
361
|
+
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>>;
|
|
362
|
+
/**
|
|
363
|
+
* Performs synchronous validation of object schema over the `object`.
|
|
364
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
365
|
+
*
|
|
366
|
+
* The returned result includes a `getErrorsFor()` method for type-safe,
|
|
367
|
+
* per-property error inspection.
|
|
368
|
+
*
|
|
369
|
+
* @param object The object to validate against this schema.
|
|
370
|
+
* @param context Optional `ValidationContext` settings.
|
|
371
|
+
*/
|
|
372
|
+
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>;
|
|
373
|
+
/**
|
|
374
|
+
* Performs async validation of object schema over the `object`.
|
|
375
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
376
|
+
*
|
|
377
|
+
* @param object The object to validate against this schema.
|
|
378
|
+
* @param context Optional `ValidationContext` settings.
|
|
379
|
+
*/
|
|
380
|
+
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
381
|
/**
|
|
148
382
|
* Fields not defined in `properties` will not be validated
|
|
149
383
|
* and will be passed through the validation.
|
|
150
384
|
*/
|
|
151
|
-
acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType
|
|
385
|
+
acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
152
386
|
/**
|
|
153
387
|
* Fields not defined in `properties` will be considered
|
|
154
388
|
* as schema violation. This is the default behavior.
|
|
155
389
|
*/
|
|
156
|
-
notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType
|
|
390
|
+
notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
157
391
|
/**
|
|
158
|
-
* @
|
|
392
|
+
* @inheritdoc
|
|
159
393
|
*/
|
|
160
|
-
hasType<T>(
|
|
394
|
+
hasType<T>(_notUsed?: T): ObjectSchemaBuilder<TProperties, TRequired, TNullable, T, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
161
395
|
/**
|
|
162
|
-
* @
|
|
396
|
+
* @inheritdoc
|
|
397
|
+
*/
|
|
398
|
+
clearHasType(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, undefined, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
399
|
+
/**
|
|
400
|
+
* Appends a constructor overload to the object schema.
|
|
401
|
+
*
|
|
402
|
+
* Each call extends the set of construct signatures on the inferred type by
|
|
403
|
+
* one overload. The accumulated argument lists are taken from the
|
|
404
|
+
* `FunctionSchemaBuilder` passed in — specifically from the positional
|
|
405
|
+
* parameter schemas registered via `.addParameter()`.
|
|
406
|
+
*
|
|
407
|
+
* At **runtime** the schema continues to validate plain objects; the
|
|
408
|
+
* constructor information is purely a TypeScript-level annotation and is
|
|
409
|
+
* stored in `introspect().constructorSchemas` for tooling use.
|
|
410
|
+
*
|
|
411
|
+
* Multiple calls are supported and produce **overloaded** construct
|
|
412
|
+
* signatures in the inferred type, modelling a class that exposes several
|
|
413
|
+
* constructor overloads.
|
|
414
|
+
*
|
|
415
|
+
* @param schema - A `FunctionSchemaBuilder` describing one constructor
|
|
416
|
+
* overload. Use `.addParameter()` on the function schema to declare the
|
|
417
|
+
* parameter types. The return type, if set via `.hasReturnType()`, is
|
|
418
|
+
* ignored — the return type of a constructor is always the instance type
|
|
419
|
+
* derived from the object schema's properties.
|
|
420
|
+
*
|
|
421
|
+
* @returns A new `ObjectSchemaBuilder` whose `TConstructorSchemas` tuple has
|
|
422
|
+
* been extended by `schema`, updating `InferType` to include the new
|
|
423
|
+
* construct signature.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* import { object, string, number, func, InferType } from '@cleverbrush/schema';
|
|
428
|
+
*
|
|
429
|
+
* // Single constructor
|
|
430
|
+
* const PersonSchema = object({ name: string(), age: number() })
|
|
431
|
+
* .addConstructor(
|
|
432
|
+
* func().addParameter(string()).addParameter(number())
|
|
433
|
+
* );
|
|
434
|
+
*
|
|
435
|
+
* type Person = InferType<typeof PersonSchema>;
|
|
436
|
+
* // → { new (p0: string, p1: number): { name: string; age: number } }
|
|
437
|
+
* // & { name: string; age: number }
|
|
438
|
+
* ```
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* import { object, string, number, func, InferType } from '@cleverbrush/schema';
|
|
443
|
+
*
|
|
444
|
+
* // Multiple constructors via chained calls → overloaded signatures
|
|
445
|
+
* const PointSchema = object({ x: number(), y: number() })
|
|
446
|
+
* .addConstructor(func()) // no-arg ctor
|
|
447
|
+
* .addConstructor(func().addParameter(number()).addParameter(number())); // (x, y) ctor
|
|
448
|
+
*
|
|
449
|
+
* type Point = InferType<typeof PointSchema>;
|
|
450
|
+
* // → { new (): { x: number; y: number } }
|
|
451
|
+
* // & { new (p0: number, p1: number): { x: number; y: number } }
|
|
452
|
+
* // & { x: number; y: number }
|
|
453
|
+
*
|
|
454
|
+
* // Validation still operates on plain objects — the constructor type is
|
|
455
|
+
* // a compile-time annotation only.
|
|
456
|
+
* const result = PointSchema.validate({ x: 1, y: 2 });
|
|
457
|
+
* // result.valid === true
|
|
458
|
+
*
|
|
459
|
+
* // Introspect the registered constructor schemas at runtime:
|
|
460
|
+
* const info = PointSchema.introspect();
|
|
461
|
+
* // info.constructorSchemas.length === 2
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
addConstructor<TFunc extends SchemaBuilder<any, any, any, any, any>>(schema: TFunc): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, [
|
|
465
|
+
...TConstructorSchemas,
|
|
466
|
+
TFunc
|
|
467
|
+
]> & TExtensions;
|
|
468
|
+
/**
|
|
469
|
+
* Removes all constructor overloads previously registered via
|
|
470
|
+
* `.addConstructor()`, resetting the `TConstructorSchemas` tuple to `[]`.
|
|
471
|
+
*
|
|
472
|
+
* After calling this method `InferType` reverts to the plain object type
|
|
473
|
+
* derived from the schema's properties — no construct signatures are
|
|
474
|
+
* included.
|
|
475
|
+
*
|
|
476
|
+
* `introspect().constructorSchemas` will return an empty array.
|
|
477
|
+
*
|
|
478
|
+
* @returns A new `ObjectSchemaBuilder` with `TConstructorSchemas = []`.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* ```ts
|
|
482
|
+
* import { object, string, func, InferType } from '@cleverbrush/schema';
|
|
483
|
+
*
|
|
484
|
+
* const WithCtor = object({ name: string() })
|
|
485
|
+
* .addConstructor(func().addParameter(string()));
|
|
486
|
+
*
|
|
487
|
+
* type WithCtorType = InferType<typeof WithCtor>;
|
|
488
|
+
* // → { new (p0: string): { name: string } } & { name: string }
|
|
489
|
+
*
|
|
490
|
+
* const Plain = WithCtor.clearConstructors();
|
|
491
|
+
*
|
|
492
|
+
* type PlainType = InferType<typeof Plain>;
|
|
493
|
+
* // → { name: string }
|
|
494
|
+
*
|
|
495
|
+
* Plain.introspect().constructorSchemas; // []
|
|
496
|
+
* ```
|
|
163
497
|
*/
|
|
164
|
-
|
|
498
|
+
clearConstructors(): ObjectSchemaBuilder<TProperties, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, [
|
|
499
|
+
]> & TExtensions;
|
|
165
500
|
/**
|
|
166
501
|
* Adds a new property to the object schema. The new property
|
|
167
502
|
* will be validated according to the provided schema.
|
|
168
503
|
* @param propName name of the new property
|
|
169
504
|
* @param schema schema builder of the new property
|
|
170
505
|
*/
|
|
171
|
-
addProp<TType extends SchemaBuilder<any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
|
|
506
|
+
addProp<TType extends SchemaBuilder<any, any, any, any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
|
|
172
507
|
[k in TName]: TType;
|
|
173
|
-
}, TRequired, TExplicitType
|
|
508
|
+
}, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
174
509
|
/**
|
|
175
510
|
* @hidden
|
|
176
511
|
* @deprecated this is for internal use, do not use if you are
|
|
@@ -185,82 +520,141 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
|
|
|
185
520
|
* comments were lost. Hopefully it will be fixed in the future by Typescript team or somebody will
|
|
186
521
|
* find a workaround/fix and create a pull request.
|
|
187
522
|
*/
|
|
188
|
-
optimize(): SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties
|
|
523
|
+
optimize(): SchemaBuilder<undefined extends TExplicitType ? WithConstructors<TConstructorSchemas, Id<RespectPropsOptionality<TProperties>>> : TExplicitType, TRequired, TNullable, THasDefault, TExtensions>;
|
|
189
524
|
/**
|
|
190
525
|
* Adds new properties to the object schema. The same as `.addProp()` but
|
|
191
526
|
* allows to add multiple properties with one call. The new properties
|
|
192
527
|
* will be validated according to the provided schemas.
|
|
193
528
|
* @param props a key/schema object map.
|
|
194
529
|
*/
|
|
195
|
-
addProps<TProps extends Record<string, SchemaBuilder<any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps,
|
|
530
|
+
addProps<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps, TRequired, TNullable, undefined, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
196
531
|
/**
|
|
197
532
|
* Adds all properties from the `schema` object schema to the current schema.
|
|
198
533
|
* @param schema an instance of `ObjectSchemaBuilder`
|
|
199
534
|
*/
|
|
200
|
-
addProps<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProp, infer
|
|
535
|
+
addProps<K extends ObjectSchemaBuilder<any, 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, TConstructorSchemas> & TExtensions : never;
|
|
201
536
|
/**
|
|
202
537
|
* Omits properties listed in `properties` from the schema.
|
|
203
538
|
* Consider `Omit<Type, 'prop1'|'prop2'...>` as a good illustration
|
|
204
539
|
* from the TS world.
|
|
205
540
|
* @param properties - array of property names (strings) to remove from the schema.
|
|
206
541
|
*/
|
|
207
|
-
omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TExplicitType
|
|
542
|
+
omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
208
543
|
/**
|
|
209
544
|
* Removes `propName` from the list of properties.
|
|
210
545
|
* @param propName property name to remove. Schema should contain
|
|
211
546
|
* this property. An error will be thrown otherwise.
|
|
212
547
|
*/
|
|
213
|
-
omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TExplicitType
|
|
548
|
+
omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
214
549
|
/**
|
|
215
550
|
* Removes all properties of `schema` from the current schema.
|
|
216
551
|
* `Omit<TSchema, keyof TAnotherSchema>` as a good illustration
|
|
217
552
|
* from the TS world.
|
|
218
553
|
* @param schema schema builder to take properties from.
|
|
219
554
|
*/
|
|
220
|
-
omit<T>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TRequired, infer TExplicitType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps>, TRequired, TExplicitType> : never;
|
|
555
|
+
omit<T>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TRequired, any, infer TExplicitType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions : never;
|
|
221
556
|
/**
|
|
222
557
|
* Adds all properties from `schema` to the current schema.
|
|
223
558
|
* `TSchema & TAnotherSchema` is a good example of the similar concept
|
|
224
559
|
* in the TS type system.
|
|
225
560
|
* @param schema an object schema to take properties from
|
|
226
561
|
*/
|
|
227
|
-
intersect<T extends ObjectSchemaBuilder<any, any, any>>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer
|
|
562
|
+
intersect<T extends ObjectSchemaBuilder<any, 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, TConstructorSchemas> & TExtensions : never;
|
|
228
563
|
/**
|
|
229
564
|
* Marks all properties in the current schema as optional.
|
|
230
565
|
* It is the same as call `.optional('propname')` where `propname` is the name
|
|
231
566
|
* of every property in the schema.
|
|
232
567
|
*/
|
|
233
|
-
partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType
|
|
568
|
+
partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
234
569
|
/**
|
|
235
570
|
* Marks all properties from `properties` as optional in the schema.
|
|
236
571
|
* @param properties list of property names (string) to make optional
|
|
237
572
|
*/
|
|
238
|
-
partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TExplicitType
|
|
573
|
+
partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
239
574
|
/**
|
|
240
575
|
* Marks property `propName` as optional in the schema.
|
|
241
576
|
* @param propName the name of the property (string).
|
|
242
577
|
*/
|
|
243
|
-
partial<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty> & Pick<MakeChildrenOptional<TProperties>, TProperty>, TRequired, TExplicitType
|
|
578
|
+
partial<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty> & Pick<MakeChildrenOptional<TProperties>, TProperty>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
579
|
+
/**
|
|
580
|
+
* Recursively marks all properties — and all properties of nested
|
|
581
|
+
* `object()` schemas — as optional. Useful for PATCH API bodies
|
|
582
|
+
* and partial form state where every field at every level is optional.
|
|
583
|
+
*
|
|
584
|
+
* Only nested `ObjectSchemaBuilder` schemas are recursed into.
|
|
585
|
+
* Other schema types (arrays, unions, primitives, lazy) are made
|
|
586
|
+
* optional at the top level but their internals are not modified.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```ts
|
|
590
|
+
* const Address = object({
|
|
591
|
+
* street: string(),
|
|
592
|
+
* city: string()
|
|
593
|
+
* });
|
|
594
|
+
*
|
|
595
|
+
* const User = object({
|
|
596
|
+
* name: string(),
|
|
597
|
+
* address: Address
|
|
598
|
+
* });
|
|
599
|
+
*
|
|
600
|
+
* const PatchUser = User.deepPartial();
|
|
601
|
+
* // PatchUser infers as:
|
|
602
|
+
* // { name?: string; address?: { street?: string; city?: string } }
|
|
603
|
+
*
|
|
604
|
+
* PatchUser.validate({ address: { city: 'Paris' } }); // valid
|
|
605
|
+
* PatchUser.validate({}); // valid
|
|
606
|
+
* ```
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* ```ts
|
|
610
|
+
* // Three-level nesting
|
|
611
|
+
* const schema = object({
|
|
612
|
+
* a: object({
|
|
613
|
+
* b: object({ c: string() })
|
|
614
|
+
* })
|
|
615
|
+
* }).deepPartial();
|
|
616
|
+
*
|
|
617
|
+
* schema.validate({}); // valid
|
|
618
|
+
* schema.validate({ a: {} }); // valid
|
|
619
|
+
* schema.validate({ a: { b: {} } }); // valid
|
|
620
|
+
* ```
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* ```ts
|
|
624
|
+
* // PATCH API body
|
|
625
|
+
* const CreateBody = object({
|
|
626
|
+
* profile: object({ displayName: string(), bio: string() }),
|
|
627
|
+
* settings: object({ theme: string(), language: string() })
|
|
628
|
+
* });
|
|
629
|
+
*
|
|
630
|
+
* const PatchBody = CreateBody.deepPartial();
|
|
631
|
+
* // All fields are optional at every level —
|
|
632
|
+
* // send only what you want to update.
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* @see {@link partial} for shallow-only property optionality.
|
|
636
|
+
*/
|
|
637
|
+
deepPartial(): ObjectSchemaBuilder<DeepMakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
244
638
|
/**
|
|
245
639
|
* Returns a new schema containing only properties listed in
|
|
246
640
|
* `properties` array.
|
|
247
641
|
* @param properties array of property names (strings)
|
|
248
642
|
*/
|
|
249
|
-
pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined
|
|
643
|
+
pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
250
644
|
/**
|
|
251
645
|
* Returns new schema based on the current schema. This new schema
|
|
252
646
|
* will consists only from properties which names are taken from the
|
|
253
647
|
* `schema` object schema.
|
|
254
648
|
* @param schema schema to take property names list from
|
|
255
649
|
*/
|
|
256
|
-
pick<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProps, infer
|
|
650
|
+
pick<K extends ObjectSchemaBuilder<any, 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, TConstructorSchemas> & TExtensions : never;
|
|
257
651
|
/**
|
|
258
652
|
* Returns a new schema consisting of only one property
|
|
259
653
|
* (taken from the `property` property name). If the property
|
|
260
654
|
* does not exists in the current schema, an error will be thrown.
|
|
261
655
|
* @param property the name of the property (string).
|
|
262
656
|
*/
|
|
263
|
-
pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined
|
|
657
|
+
pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, TNullable, undefined, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
264
658
|
/**
|
|
265
659
|
* Modify schema for `propName` and return a new schema.
|
|
266
660
|
* Could be useful if you want to leave all schema intact, but
|
|
@@ -270,43 +664,74 @@ export declare class ObjectSchemaBuilder<TProperties extends Record<string, Sche
|
|
|
270
664
|
* you will receive an old schema for `propName`.
|
|
271
665
|
* @returns
|
|
272
666
|
*/
|
|
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
|
|
667
|
+
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, TConstructorSchemas> & TExtensions;
|
|
274
668
|
/**
|
|
275
669
|
* An alias for `.partial(prop: string)`
|
|
276
670
|
* @param prop name of the property
|
|
277
671
|
*/
|
|
278
|
-
makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TExplicitType
|
|
672
|
+
makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
279
673
|
/**
|
|
280
674
|
* Marks `prop` as required property.
|
|
281
675
|
* If `prop` does not exists in the current schema,
|
|
282
676
|
* an error will be thrown.
|
|
283
677
|
* @param prop name of the property
|
|
284
678
|
*/
|
|
285
|
-
makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TExplicitType
|
|
679
|
+
makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
286
680
|
/**
|
|
287
681
|
* `Partial<T>` would be a good example of the
|
|
288
682
|
* same operation in the TS world.
|
|
289
683
|
*/
|
|
290
|
-
makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType
|
|
684
|
+
makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
291
685
|
/**
|
|
292
686
|
* `Required<T>` would be a good example of the
|
|
293
687
|
* same operation in the TS world.
|
|
294
688
|
*/
|
|
295
|
-
makeAllPropsRequired(): ObjectSchemaBuilder<MakeChildrenRequired<TProperties>, TRequired, TExplicitType
|
|
689
|
+
makeAllPropsRequired(): ObjectSchemaBuilder<MakeChildrenRequired<TProperties>, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TConstructorSchemas> & TExtensions;
|
|
690
|
+
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>;
|
|
691
|
+
static isValidPropertyDescriptor(descriptor: PropertyDescriptor<any, any, any>): boolean;
|
|
692
|
+
nullable(): ObjectSchemaBuilder<TProperties, TRequired, true, TExplicitType, THasDefault, TExtensions, TConstructorSchemas>;
|
|
693
|
+
notNullable(): ObjectSchemaBuilder<TProperties, TRequired, false, TExplicitType, THasDefault, TExtensions, TConstructorSchemas>;
|
|
296
694
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
695
|
+
export interface Object {
|
|
696
|
+
/**
|
|
697
|
+
* Defines a schema for empty object `{}`
|
|
698
|
+
*/
|
|
699
|
+
(): ObjectSchemaBuilder<{}, true>;
|
|
700
|
+
/**
|
|
701
|
+
* Defines an object schema, properties definitions are takens from `props`.
|
|
702
|
+
* @param props key/schema object map for schema's properties.
|
|
703
|
+
*/
|
|
704
|
+
<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ObjectSchemaBuilder<TProps, true>;
|
|
705
|
+
/**
|
|
706
|
+
* Defines an object schema, properties definitions are takens from `props`.
|
|
707
|
+
* @param props key/schema object map for schema's properties.
|
|
708
|
+
*/
|
|
709
|
+
<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props?: TProps): ObjectSchemaBuilder<TProps, true>;
|
|
710
|
+
/**
|
|
711
|
+
* Returns a tree of property descriptors for the given `schema`.
|
|
712
|
+
* The structure of the tree is the same as the structure of the `schema`.
|
|
713
|
+
* Which gives you an opportunity to access property descriptors for each
|
|
714
|
+
* property in the schema in a useful and type-safe way.
|
|
715
|
+
* @param schema
|
|
716
|
+
*/
|
|
717
|
+
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>;
|
|
718
|
+
/**
|
|
719
|
+
* Verifies if the given `descriptor` is a valid property descriptor.
|
|
720
|
+
* @param descriptor a property descriptor to check
|
|
721
|
+
*/
|
|
722
|
+
isValidPropertyDescriptor(descriptor: PropertyDescriptor<any, any, any>): boolean;
|
|
723
|
+
}
|
|
724
|
+
declare const object: Object;
|
|
725
|
+
export { object };
|
|
726
|
+
type RequiredProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
|
|
727
|
+
[k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, any> ? TReq extends true ? k : never : never]: T[k];
|
|
728
|
+
};
|
|
729
|
+
type NotRequiredProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
|
|
730
|
+
[k in keyof T as T[k] extends SchemaBuilder<any, infer TReq, any, any> ? TReq extends true ? never : k : never]: T[k];
|
|
731
|
+
};
|
|
732
|
+
type RequiredInputProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
|
|
733
|
+
[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
734
|
};
|
|
309
|
-
type
|
|
310
|
-
[k in keyof T as T[k] extends SchemaBuilder<infer
|
|
735
|
+
type NotRequiredInputProps<T extends Record<string, SchemaBuilder<any, any, any, any, any>>> = keyof {
|
|
736
|
+
[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
737
|
};
|
|
312
|
-
export {};
|