@cleverbrush/schema 1.0.0-beta.5 → 1.1.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 +118 -320
- package/dist/builders/AnySchemaBuilder.d.ts +76 -0
- package/dist/builders/AnySchemaBuilder.js +112 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
- package/dist/builders/ArraySchemaBuilder.js +254 -111
- package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
- package/dist/builders/BooleanSchemaBuilder.js +134 -74
- package/dist/builders/DateSchemaBuilder.d.ts +179 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
- package/dist/builders/FunctionSchemaBuilder.js +102 -58
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +310 -81
- package/dist/builders/ObjectSchemaBuilder.js +528 -283
- package/dist/builders/SchemaBuilder.d.ts +157 -34
- package/dist/builders/SchemaBuilder.js +258 -71
- package/dist/builders/StringSchemaBuilder.d.ts +138 -13
- package/dist/builders/StringSchemaBuilder.js +261 -115
- package/dist/builders/UnionSchemaBuilder.d.ts +132 -9
- package/dist/builders/UnionSchemaBuilder.js +196 -75
- package/dist/index.d.ts +20 -40
- package/dist/index.js +19 -32
- package/dist/utils/transaction.d.ts +46 -0
- package/dist/utils/transaction.js +178 -0
- package/package.json +27 -14
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/schema.d.ts +0 -231
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -49
- package/dist/schemaRegistry.js +0 -278
- package/dist/validators/validateArray.d.ts +0 -3
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -30
- package/dist/validators/validateFunction.d.ts +0 -2
- package/dist/validators/validateFunction.js +0 -21
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -3
- package/dist/validators/validateObject.js +0 -95
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/dist/validators/validateUnion.d.ts +0 -3
- package/dist/validators/validateUnion.js +0 -30
- package/src/builders/ArraySchemaBuilder.test.ts +0 -270
- package/src/builders/ArraySchemaBuilder.ts +0 -381
- package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
- package/src/builders/BooleanSchemaBuilder.ts +0 -155
- package/src/builders/FunctionSchemaBuilder.test.ts +0 -145
- package/src/builders/FunctionSchemaBuilder.ts +0 -117
- package/src/builders/NumberSchemaBuilder.test.ts +0 -493
- package/src/builders/NumberSchemaBuilder.ts +0 -789
- package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
- package/src/builders/ObjectSchemaBuilder.ts +0 -794
- package/src/builders/SchemaBuilder.test.ts +0 -73
- package/src/builders/SchemaBuilder.ts +0 -135
- package/src/builders/StringSchemaBuilder.test.ts +0 -318
- package/src/builders/StringSchemaBuilder.ts +0 -392
- package/src/builders/UnionSchemaBuilder.test.ts +0 -162
- package/src/builders/UnionSchemaBuilder.ts +0 -154
- package/src/defaultSchemas.ts +0 -44
- package/src/index.ts +0 -66
- package/src/schema.ts +0 -849
- package/src/schemaRegistry.builders.test.ts +0 -1393
- package/src/schemaRegistry.test.ts +0 -118
- package/src/schemaRegistry.ts +0 -461
- package/src/validators/validateArray.ts +0 -76
- package/src/validators/validateBoolean.ts +0 -36
- package/src/validators/validateFunction.ts +0 -25
- package/src/validators/validateNumber.ts +0 -82
- package/src/validators/validateObject.ts +0 -119
- package/src/validators/validateString.ts +0 -59
- package/src/validators/validateUnion.ts +0 -36
- package/tsconfig.json +0 -16
|
@@ -1,83 +1,312 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
import { InferType, SchemaBuilder, ValidationContext, ValidationResult } from './SchemaBuilder.js';
|
|
2
|
+
type ObjectSchemaBuilderProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = ReturnType<ObjectSchemaBuilder<T, TRequired>['introspect']>;
|
|
3
|
+
type ObjectSchemaBuilderCreateProps<T extends Record<string, SchemaBuilder> = {}, TRequired extends boolean = true> = Partial<ObjectSchemaBuilderProps<T, TRequired>>;
|
|
4
|
+
type Id<T> = T extends infer U ? {
|
|
5
|
+
[K in keyof U]: U[K];
|
|
6
|
+
} : never;
|
|
7
|
+
export type RespectPropsOptionality<T extends Record<string, SchemaBuilder<any, any>>> = {
|
|
8
|
+
[K in RequiredProps<T>]: InferType<T[K]>;
|
|
9
|
+
} & {
|
|
10
|
+
[K in NotRequiredProps<T>]?: InferType<T[K]>;
|
|
11
|
+
};
|
|
12
|
+
type MakeChildrenRequired<T extends Record<string, SchemaBuilder<any, any>>> = {
|
|
13
|
+
[K in keyof T]: ReturnType<T[K]['required']>;
|
|
14
|
+
};
|
|
15
|
+
type MakeChildrenOptional<T extends Record<string, SchemaBuilder<any, any>>> = {
|
|
16
|
+
[K in keyof T]: ReturnType<T[K]['optional']>;
|
|
17
|
+
};
|
|
18
|
+
type MakeChildOptional<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T> = {
|
|
19
|
+
[K in keyof T]: K extends TProp ? ReturnType<T[K]['optional']> : T[K];
|
|
20
|
+
};
|
|
21
|
+
type MakeChildRequired<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T> = {
|
|
22
|
+
[K in keyof T]: K extends TProp ? ReturnType<T[K]['required']> : T[K];
|
|
23
|
+
};
|
|
24
|
+
type ModifyPropSchema<T extends Record<any, SchemaBuilder<any, any>>, TProp extends keyof T, TSchema extends SchemaBuilder<any, any>> = {
|
|
25
|
+
[K in keyof T]: K extends TProp ? TSchema : T[K];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Object schema builder class. Similar to the `object` type
|
|
29
|
+
* in JS. Allows to define a schema for `object` value.
|
|
30
|
+
* Should be used to validate objects with specific properties.
|
|
31
|
+
* Properties should be defined as their own schema builders.
|
|
32
|
+
* You can use any `SchemaBuilder` e.g. `string()`, `number()`,
|
|
33
|
+
* `boolean()`, `array()`, `object()`, etc. to define properties.
|
|
34
|
+
* Which means that you can define nested objects and arrays of
|
|
35
|
+
* any complexity.
|
|
36
|
+
*
|
|
37
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
38
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
39
|
+
* directly. Use {@link object | object()} function instead.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const schema = object({
|
|
44
|
+
* name: string(),
|
|
45
|
+
* age: number()
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* const result = await schema.validate({
|
|
49
|
+
* name: 'John',
|
|
50
|
+
* age: 30
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* // result.valid === true
|
|
54
|
+
* // result.object === { name: 'John', age: 30 }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const schema = object({
|
|
60
|
+
* name: string(),
|
|
61
|
+
* age: number().optional()
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* const result = await schema.validate({
|
|
65
|
+
* name: 'John'
|
|
66
|
+
* });
|
|
67
|
+
* // result.valid === true
|
|
68
|
+
* // result.object === { name: 'John' }
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const schema = object({
|
|
74
|
+
* name: string(),
|
|
75
|
+
* age: number();
|
|
76
|
+
* });
|
|
77
|
+
* const result = await schema.validate({
|
|
78
|
+
* name: 'John'
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* // result.valid === false
|
|
82
|
+
* // result.errors[0].message === "is expected to have property 'age'"
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const schema = object({
|
|
88
|
+
* name: string(),
|
|
89
|
+
* address: object({
|
|
90
|
+
* city: string(),
|
|
91
|
+
* country: string()
|
|
92
|
+
* })
|
|
93
|
+
* });
|
|
94
|
+
* const result = await schema.validate({
|
|
95
|
+
* name: 'John',
|
|
96
|
+
* address: {
|
|
97
|
+
* city: 'New York',
|
|
98
|
+
* country: 'USA'
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
* // result.valid === true
|
|
102
|
+
* // result.object === {
|
|
103
|
+
* // name: 'John',
|
|
104
|
+
* // address: {
|
|
105
|
+
* // city: 'New York',
|
|
106
|
+
* // country: 'USA'
|
|
107
|
+
* // }
|
|
108
|
+
* // }
|
|
109
|
+
* ```
|
|
110
|
+
* @see {@link object}
|
|
111
|
+
*/
|
|
112
|
+
export declare class ObjectSchemaBuilder<TProperties extends Record<string, SchemaBuilder<any, any>> = {}, TRequired extends boolean = true, TExplicitType = undefined> extends SchemaBuilder<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType, TRequired> {
|
|
113
|
+
#private;
|
|
114
|
+
static create<P extends Record<string, SchemaBuilder>, R extends boolean>(props: ObjectSchemaBuilderCreateProps<P, R>): ObjectSchemaBuilder<{}, true, undefined>;
|
|
115
|
+
protected createFromProps<T extends Record<string, SchemaBuilder>, R extends boolean = true>(props: ObjectSchemaBuilderCreateProps<T, R>): this;
|
|
42
116
|
private constructor();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
117
|
+
introspect(): {
|
|
118
|
+
/**
|
|
119
|
+
* Properties defined in schema
|
|
120
|
+
*/
|
|
121
|
+
properties: TProperties;
|
|
122
|
+
/**
|
|
123
|
+
* If set to `true`, schema validation will not
|
|
124
|
+
* return errors if object contains fields which
|
|
125
|
+
* are not defined in the schema `properties`.
|
|
126
|
+
* Set to `false` by default
|
|
127
|
+
*/
|
|
128
|
+
acceptUnknownProps: boolean;
|
|
129
|
+
type: string;
|
|
130
|
+
isRequired: boolean;
|
|
131
|
+
preprocessors: readonly import("./SchemaBuilder.js").Preprocessor<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
|
|
132
|
+
validators: readonly import("./SchemaBuilder.js").Validator<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>[];
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* @hidden
|
|
136
|
+
*/
|
|
137
|
+
required(): ObjectSchemaBuilder<TProperties, true, TExplicitType>;
|
|
138
|
+
/**
|
|
139
|
+
* @hidden
|
|
140
|
+
*/
|
|
141
|
+
optional(): ObjectSchemaBuilder<TProperties, false, TExplicitType>;
|
|
142
|
+
/**
|
|
143
|
+
* Performs validion of object schema over the `object`.
|
|
144
|
+
* @param context Optional `ValidationContext` settings.
|
|
145
|
+
*/
|
|
146
|
+
validate(object: undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType, context?: ValidationContext): Promise<ValidationResult<undefined extends TExplicitType ? RespectPropsOptionality<TProperties> : TExplicitType>>;
|
|
147
|
+
/**
|
|
148
|
+
* Fields not defined in `properties` will not be validated
|
|
149
|
+
* and will be passed through the validation.
|
|
150
|
+
*/
|
|
151
|
+
acceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>;
|
|
152
|
+
/**
|
|
153
|
+
* Fields not defined in `properties` will be considered
|
|
154
|
+
* as schema violation. This is the default behavior.
|
|
155
|
+
*/
|
|
156
|
+
notAcceptUnknownProps(): ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>;
|
|
157
|
+
/**
|
|
158
|
+
* @hidden
|
|
159
|
+
*/
|
|
160
|
+
hasType<T>(notUsed?: T): ObjectSchemaBuilder<TProperties, TRequired, T>;
|
|
161
|
+
/**
|
|
162
|
+
* @hidden
|
|
163
|
+
*/
|
|
164
|
+
clearHasType(): ObjectSchemaBuilder<TProperties, TRequired>;
|
|
165
|
+
/**
|
|
166
|
+
* Adds a new property to the object schema. The new property
|
|
167
|
+
* will be validated according to the provided schema.
|
|
168
|
+
* @param propName name of the new property
|
|
169
|
+
* @param schema schema builder of the new property
|
|
170
|
+
*/
|
|
171
|
+
addProp<TType extends SchemaBuilder<any, any>, TName extends string>(propName: TName, schema: TType): ObjectSchemaBuilder<TProperties & {
|
|
172
|
+
[k in TName]: TType;
|
|
173
|
+
}, TRequired, TExplicitType>;
|
|
174
|
+
/**
|
|
175
|
+
* @hidden
|
|
176
|
+
* @deprecated this is for internal use, do not use if you are
|
|
177
|
+
* not sure you need it.
|
|
178
|
+
*
|
|
179
|
+
* TODO: This is used to avoid `&` in resulting types. For example,
|
|
180
|
+
* when you have a schema like `object({prop1: string()})` and then use `addProp({prop2: string()})` method,
|
|
181
|
+
* the resulting type without `optimize` will be something like `{prop1: string} & {prop2: string}`. Which
|
|
182
|
+
* is not we would like to have. Instead we want to have `{prop1: string, prop2: string}`. This is what
|
|
183
|
+
* `optimize` method does. However it is not always possible to do this optimization without losing
|
|
184
|
+
* JSDoc comments, which is sucks. For example, I had to disable optimization for UnionSchemas, because
|
|
185
|
+
* comments were lost. Hopefully it will be fixed in the future by Typescript team or somebody will
|
|
186
|
+
* find a workaround/fix and create a pull request.
|
|
187
|
+
*/
|
|
188
|
+
optimize(): SchemaBuilder<undefined extends TExplicitType ? Id<RespectPropsOptionality<TProperties>> : TExplicitType>;
|
|
189
|
+
/**
|
|
190
|
+
* Adds new properties to the object schema. The same as `.addProp()` but
|
|
191
|
+
* allows to add multiple properties with one call. The new properties
|
|
192
|
+
* will be validated according to the provided schemas.
|
|
193
|
+
* @param props a key/schema object map.
|
|
194
|
+
*/
|
|
195
|
+
addProps<TProps extends Record<string, SchemaBuilder<any, any>>>(props: TProps): ObjectSchemaBuilder<TProperties & TProps, true, undefined>;
|
|
196
|
+
/**
|
|
197
|
+
* Adds all properties from the `schema` object schema to the current schema.
|
|
198
|
+
* @param schema an instance of `ObjectSchemaBuilder`
|
|
199
|
+
*/
|
|
200
|
+
addProps<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProp, infer TReq, infer TExpType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProp> & TProp, TRequired, TExplicitType> : never;
|
|
201
|
+
/**
|
|
202
|
+
* Omits properties listed in `properties` from the schema.
|
|
203
|
+
* Consider `Omit<Type, 'prop1'|'prop2'...>` as a good illustration
|
|
204
|
+
* from the TS world.
|
|
205
|
+
* @param properties - array of property names (strings) to remove from the schema.
|
|
206
|
+
*/
|
|
207
|
+
omit<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K>, TRequired, TExplicitType>;
|
|
208
|
+
/**
|
|
209
|
+
* Removes `propName` from the list of properties.
|
|
210
|
+
* @param propName property name to remove. Schema should contain
|
|
211
|
+
* this property. An error will be thrown otherwise.
|
|
212
|
+
*/
|
|
213
|
+
omit<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty>, TRequired, TExplicitType>;
|
|
214
|
+
/**
|
|
215
|
+
* Removes all properties of `schema` from the current schema.
|
|
216
|
+
* `Omit<TSchema, keyof TAnotherSchema>` as a good illustration
|
|
217
|
+
* from the TS world.
|
|
218
|
+
* @param schema schema builder to take properties from.
|
|
219
|
+
*/
|
|
220
|
+
omit<T>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TRequired, infer TExplicitType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps>, TRequired, TExplicitType> : never;
|
|
221
|
+
/**
|
|
222
|
+
* Adds all properties from `schema` to the current schema.
|
|
223
|
+
* `TSchema & TAnotherSchema` is a good example of the similar concept
|
|
224
|
+
* in the TS type system.
|
|
225
|
+
* @param schema an object schema to take properties from
|
|
226
|
+
*/
|
|
227
|
+
intersect<T extends ObjectSchemaBuilder<any, any, any>>(schema: T): T extends ObjectSchemaBuilder<infer TProps, infer TReq, infer TExplType> ? ObjectSchemaBuilder<Omit<TProperties, keyof TProps> & TProps, TRequired, TExplType> : never;
|
|
228
|
+
/**
|
|
229
|
+
* Marks all properties in the current schema as optional.
|
|
230
|
+
* It is the same as call `.optional('propname')` where `propname` is the name
|
|
231
|
+
* of every property in the schema.
|
|
232
|
+
*/
|
|
233
|
+
partial(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType>;
|
|
234
|
+
/**
|
|
235
|
+
* Marks all properties from `properties` as optional in the schema.
|
|
236
|
+
* @param properties list of property names (string) to make optional
|
|
237
|
+
*/
|
|
238
|
+
partial<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Omit<TProperties, K> & Pick<MakeChildrenOptional<TProperties>, K>, TRequired, TExplicitType>;
|
|
239
|
+
/**
|
|
240
|
+
* Marks property `propName` as optional in the schema.
|
|
241
|
+
* @param propName the name of the property (string).
|
|
242
|
+
*/
|
|
243
|
+
partial<TProperty extends keyof TProperties>(propName: TProperty): ObjectSchemaBuilder<Omit<TProperties, TProperty> & Pick<MakeChildrenOptional<TProperties>, TProperty>, TRequired, TExplicitType>;
|
|
244
|
+
/**
|
|
245
|
+
* Returns a new schema containing only properties listed in
|
|
246
|
+
* `properties` array.
|
|
247
|
+
* @param properties array of property names (strings)
|
|
248
|
+
*/
|
|
249
|
+
pick<K extends keyof TProperties>(properties: K[]): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined>;
|
|
250
|
+
/**
|
|
251
|
+
* Returns new schema based on the current schema. This new schema
|
|
252
|
+
* will consists only from properties which names are taken from the
|
|
253
|
+
* `schema` object schema.
|
|
254
|
+
* @param schema schema to take property names list from
|
|
255
|
+
*/
|
|
256
|
+
pick<K extends ObjectSchemaBuilder<any, any, any>>(schema: K): K extends ObjectSchemaBuilder<infer TProps, infer T1, infer T2> ? ObjectSchemaBuilder<Omit<TProperties, keyof Omit<TProperties, keyof TProps>>, TRequired, undefined> : never;
|
|
257
|
+
/**
|
|
258
|
+
* Returns a new schema consisting of only one property
|
|
259
|
+
* (taken from the `property` property name). If the property
|
|
260
|
+
* does not exists in the current schema, an error will be thrown.
|
|
261
|
+
* @param property the name of the property (string).
|
|
262
|
+
*/
|
|
263
|
+
pick<K extends keyof TProperties>(property: K): ObjectSchemaBuilder<Pick<TProperties, K>, TRequired, undefined>;
|
|
264
|
+
/**
|
|
265
|
+
* Modify schema for `propName` and return a new schema.
|
|
266
|
+
* Could be useful if you want to leave all schema intact, but
|
|
267
|
+
* change a type of one property.
|
|
268
|
+
* @param propName name of the property (string)
|
|
269
|
+
* @param callback callback function returning a new schema fo the `propName`. As a first parameter
|
|
270
|
+
* you will receive an old schema for `propName`.
|
|
271
|
+
* @returns
|
|
272
|
+
*/
|
|
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>;
|
|
274
|
+
/**
|
|
275
|
+
* An alias for `.partial(prop: string)`
|
|
276
|
+
* @param prop name of the property
|
|
277
|
+
*/
|
|
278
|
+
makePropOptional<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildOptional<TProperties, K>, TRequired, TExplicitType>;
|
|
279
|
+
/**
|
|
280
|
+
* Marks `prop` as required property.
|
|
281
|
+
* If `prop` does not exists in the current schema,
|
|
282
|
+
* an error will be thrown.
|
|
283
|
+
* @param prop name of the property
|
|
284
|
+
*/
|
|
285
|
+
makePropRequired<K extends keyof TProperties>(prop: K): ObjectSchemaBuilder<MakeChildRequired<TProperties, K>, TRequired, TExplicitType>;
|
|
286
|
+
/**
|
|
287
|
+
* `Partial<T>` would be a good example of the
|
|
288
|
+
* same operation in the TS world.
|
|
289
|
+
*/
|
|
290
|
+
makeAllPropsOptional(): ObjectSchemaBuilder<MakeChildrenOptional<TProperties>, TRequired, TExplicitType>;
|
|
291
|
+
/**
|
|
292
|
+
* `Required<T>` would be a good example of the
|
|
293
|
+
* same operation in the TS world.
|
|
294
|
+
*/
|
|
295
|
+
makeAllPropsRequired(): ObjectSchemaBuilder<MakeChildrenRequired<TProperties>, TRequired, TExplicitType>;
|
|
82
296
|
}
|
|
83
|
-
|
|
297
|
+
/**
|
|
298
|
+
* Defines a schema for empty object `{}`
|
|
299
|
+
*/
|
|
300
|
+
export declare function object(): ObjectSchemaBuilder<{}, true>;
|
|
301
|
+
/**
|
|
302
|
+
* Defines an object schema, properties definitions are takens from `props`.
|
|
303
|
+
* @param props key/schema object map for schema's properties.
|
|
304
|
+
*/
|
|
305
|
+
export declare function object<TProps extends Record<string, SchemaBuilder<any, any>>>(props: TProps): ObjectSchemaBuilder<TProps, true>;
|
|
306
|
+
type RequiredProps<T extends Record<string, SchemaBuilder<any, any>>> = keyof {
|
|
307
|
+
[k in keyof T as T[k] extends SchemaBuilder<infer TRes, infer TReq> ? TReq extends true ? k : never : never]: T[k];
|
|
308
|
+
};
|
|
309
|
+
type NotRequiredProps<T extends Record<string, SchemaBuilder<any, any>>> = keyof {
|
|
310
|
+
[k in keyof T as T[k] extends SchemaBuilder<infer TRes, infer TReq> ? TReq extends true ? never : k : never]: T[k];
|
|
311
|
+
};
|
|
312
|
+
export {};
|