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