@cleverbrush/schema 1.1.11 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1314 -101
- 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 +124 -23
- package/dist/builders/ArraySchemaBuilder.js +2 -284
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +94 -19
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +134 -34
- package/dist/builders/DateSchemaBuilder.js +2 -433
- package/dist/builders/DateSchemaBuilder.js.map +1 -0
- package/dist/builders/ExternSchemaBuilder.d.ts +200 -0
- package/dist/builders/ExternSchemaBuilder.js +2 -0
- package/dist/builders/ExternSchemaBuilder.js.map +1 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +57 -14
- package/dist/builders/FunctionSchemaBuilder.js +2 -113
- package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
- package/dist/builders/LazySchemaBuilder.d.ts +167 -0
- package/dist/builders/NullSchemaBuilder.d.ts +162 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +143 -31
- package/dist/builders/NumberSchemaBuilder.js +2 -386
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +266 -61
- package/dist/builders/ObjectSchemaBuilder.js +2 -589
- package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
- package/dist/builders/PropertyValidationResult.d.ts +68 -0
- package/dist/builders/RecordSchemaBuilder.d.ts +341 -0
- package/dist/builders/RecordSchemaBuilder.js +2 -0
- package/dist/builders/RecordSchemaBuilder.js.map +1 -0
- package/dist/builders/SchemaBuilder.d.ts +818 -30
- package/dist/builders/StringSchemaBuilder.d.ts +152 -37
- package/dist/builders/StringSchemaBuilder.js +2 -414
- package/dist/builders/StringSchemaBuilder.js.map +1 -0
- package/dist/builders/TupleSchemaBuilder.d.ts +248 -0
- package/dist/builders/TupleSchemaBuilder.js +2 -0
- package/dist/builders/TupleSchemaBuilder.js.map +1 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +133 -39
- package/dist/builders/UnionSchemaBuilder.js +2 -216
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-BFCMDDTX.js +2 -0
- package/dist/chunk-BFCMDDTX.js.map +1 -0
- package/dist/chunk-CTP4RHDG.js +2 -0
- package/dist/chunk-CTP4RHDG.js.map +1 -0
- package/dist/chunk-EUQ5AE3H.js +2 -0
- package/dist/chunk-EUQ5AE3H.js.map +1 -0
- package/dist/chunk-FWUEUW2N.js +2 -0
- package/dist/chunk-FWUEUW2N.js.map +1 -0
- package/dist/chunk-IJ6FO37G.js +2 -0
- package/dist/chunk-IJ6FO37G.js.map +1 -0
- package/dist/chunk-KESLT5EE.js +2 -0
- package/dist/chunk-KESLT5EE.js.map +1 -0
- package/dist/chunk-KFTO7TMB.js +2 -0
- package/dist/chunk-KFTO7TMB.js.map +1 -0
- package/dist/chunk-MKAYFPAR.js +2 -0
- package/dist/chunk-MKAYFPAR.js.map +1 -0
- package/dist/chunk-NXPH3ZUW.js +2 -0
- package/dist/chunk-NXPH3ZUW.js.map +1 -0
- package/dist/chunk-S5TC6NSU.js +2 -0
- package/dist/chunk-S5TC6NSU.js.map +1 -0
- package/dist/chunk-YWBNVHR6.js +2 -0
- package/dist/chunk-YWBNVHR6.js.map +1 -0
- package/dist/chunk-Z7QZ7IXT.js +2 -0
- package/dist/chunk-Z7QZ7IXT.js.map +1 -0
- package/dist/chunk-ZAN4ZHCJ.js +2 -0
- package/dist/chunk-ZAN4ZHCJ.js.map +1 -0
- package/dist/chunk-ZUPYV5TI.js +2 -0
- package/dist/chunk-ZUPYV5TI.js.map +1 -0
- package/dist/core.d.ts +23 -0
- package/dist/core.js +2 -0
- package/dist/core.js.map +1 -0
- package/dist/extension.d.ts +413 -0
- package/dist/extensions/array.d.ts +112 -0
- package/dist/extensions/enum.d.ts +190 -0
- package/dist/extensions/index.d.ts +108 -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 +7 -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 +74 -6
- package/dist/builders/SchemaBuilder.js +0 -275
- package/dist/utils/transaction.js +0 -178
|
@@ -1,589 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Object schema builder class. Similar to the `object` type
|
|
4
|
-
* in JS. Allows to define a schema for `object` value.
|
|
5
|
-
* Should be used to validate objects with specific properties.
|
|
6
|
-
* Properties should be defined as their own schema builders.
|
|
7
|
-
* You can use any `SchemaBuilder` e.g. `string()`, `number()`,
|
|
8
|
-
* `boolean()`, `array()`, `object()`, etc. to define properties.
|
|
9
|
-
* Which means that you can define nested objects and arrays of
|
|
10
|
-
* any complexity.
|
|
11
|
-
*
|
|
12
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
13
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
14
|
-
* directly. Use {@link object | object()} function instead.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const schema = object({
|
|
19
|
-
* name: string(),
|
|
20
|
-
* age: number()
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* const result = await schema.validate({
|
|
24
|
-
* name: 'John',
|
|
25
|
-
* age: 30
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* // result.valid === true
|
|
29
|
-
* // result.object === { name: 'John', age: 30 }
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* const schema = object({
|
|
35
|
-
* name: string(),
|
|
36
|
-
* age: number().optional()
|
|
37
|
-
* });
|
|
38
|
-
*
|
|
39
|
-
* const result = await schema.validate({
|
|
40
|
-
* name: 'John'
|
|
41
|
-
* });
|
|
42
|
-
* // result.valid === true
|
|
43
|
-
* // result.object === { name: 'John' }
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* const schema = object({
|
|
49
|
-
* name: string(),
|
|
50
|
-
* age: number();
|
|
51
|
-
* });
|
|
52
|
-
* const result = await schema.validate({
|
|
53
|
-
* name: 'John'
|
|
54
|
-
* });
|
|
55
|
-
*
|
|
56
|
-
* // result.valid === false
|
|
57
|
-
* // result.errors[0].message === "is expected to have property 'age'"
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* const schema = object({
|
|
63
|
-
* name: string(),
|
|
64
|
-
* address: object({
|
|
65
|
-
* city: string(),
|
|
66
|
-
* country: string()
|
|
67
|
-
* })
|
|
68
|
-
* });
|
|
69
|
-
* const result = await schema.validate({
|
|
70
|
-
* name: 'John',
|
|
71
|
-
* address: {
|
|
72
|
-
* city: 'New York',
|
|
73
|
-
* country: 'USA'
|
|
74
|
-
* }
|
|
75
|
-
* });
|
|
76
|
-
* // result.valid === true
|
|
77
|
-
* // result.object === {
|
|
78
|
-
* // name: 'John',
|
|
79
|
-
* // address: {
|
|
80
|
-
* // city: 'New York',
|
|
81
|
-
* // country: 'USA'
|
|
82
|
-
* // }
|
|
83
|
-
* // }
|
|
84
|
-
* ```
|
|
85
|
-
* @see {@link object}
|
|
86
|
-
*/
|
|
87
|
-
export class ObjectSchemaBuilder extends SchemaBuilder {
|
|
88
|
-
#properties = {};
|
|
89
|
-
#acceptUnknownProps = false;
|
|
90
|
-
static create(props) {
|
|
91
|
-
return new ObjectSchemaBuilder({
|
|
92
|
-
type: 'object',
|
|
93
|
-
...props
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
createFromProps(props) {
|
|
97
|
-
return ObjectSchemaBuilder.create(props);
|
|
98
|
-
}
|
|
99
|
-
constructor(props) {
|
|
100
|
-
super(props);
|
|
101
|
-
if (typeof props.properties === 'object' && props.properties) {
|
|
102
|
-
this.#properties = props.properties;
|
|
103
|
-
}
|
|
104
|
-
if (typeof props.acceptUnknownProps === 'boolean') {
|
|
105
|
-
this.#acceptUnknownProps = props.acceptUnknownProps;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
introspect() {
|
|
109
|
-
return {
|
|
110
|
-
...super.introspect(),
|
|
111
|
-
/**
|
|
112
|
-
* Properties defined in schema
|
|
113
|
-
*/
|
|
114
|
-
properties: { ...this.#properties },
|
|
115
|
-
/**
|
|
116
|
-
* If set to `true`, schema validation will not
|
|
117
|
-
* return errors if object contains fields which
|
|
118
|
-
* are not defined in the schema `properties`.
|
|
119
|
-
* Set to `false` by default
|
|
120
|
-
*/
|
|
121
|
-
acceptUnknownProps: this.#acceptUnknownProps
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* @hidden
|
|
126
|
-
*/
|
|
127
|
-
required() {
|
|
128
|
-
return super.required();
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* @hidden
|
|
132
|
-
*/
|
|
133
|
-
optional() {
|
|
134
|
-
return super.optional();
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Performs validion of object schema over the `object`.
|
|
138
|
-
* @param context Optional `ValidationContext` settings.
|
|
139
|
-
*/
|
|
140
|
-
async validate(object, context) {
|
|
141
|
-
const prevalidatedResult = await super.preValidate(object, context);
|
|
142
|
-
const { valid, context: prevalidationContext, transaction: validationTransaction, errors: preValidationErrors } = prevalidatedResult;
|
|
143
|
-
const { path, doNotStopOnFirstError } = prevalidationContext;
|
|
144
|
-
let errors = prevalidatedResult.errors || [];
|
|
145
|
-
if (!valid && !doNotStopOnFirstError) {
|
|
146
|
-
return {
|
|
147
|
-
valid,
|
|
148
|
-
errors: preValidationErrors
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
const { object: { validatedObject: objToValidate } } = validationTransaction;
|
|
152
|
-
if (!this.isRequired &&
|
|
153
|
-
(typeof objToValidate === 'undefined' || objToValidate === null)) {
|
|
154
|
-
return {
|
|
155
|
-
valid: true,
|
|
156
|
-
object: validationTransaction.commit().validatedObject
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
if (typeof objToValidate !== 'object') {
|
|
160
|
-
errors.push({
|
|
161
|
-
message: 'must be an object',
|
|
162
|
-
path: path
|
|
163
|
-
});
|
|
164
|
-
if (!doNotStopOnFirstError) {
|
|
165
|
-
if (validationTransaction) {
|
|
166
|
-
validationTransaction.rollback();
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
valid: false,
|
|
170
|
-
errors: [errors[0]]
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
const propKeys = Object.keys(this.#properties);
|
|
175
|
-
const objKeys = Object.keys(objToValidate);
|
|
176
|
-
if (propKeys.length === 0) {
|
|
177
|
-
if (objKeys.length === 0) {
|
|
178
|
-
if (doNotStopOnFirstError && errors.length > 0) {
|
|
179
|
-
return {
|
|
180
|
-
valid: false,
|
|
181
|
-
errors
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
if (validationTransaction) {
|
|
185
|
-
validationTransaction.commit().validatedObject;
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
valid: true,
|
|
189
|
-
object: {}
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
const validationResults = await Promise.all(propKeys.map(async (key) => ({
|
|
194
|
-
key,
|
|
195
|
-
result: await this.#properties[key].validate(objToValidate[key], {
|
|
196
|
-
...context,
|
|
197
|
-
path: `${path}.${key}`
|
|
198
|
-
})
|
|
199
|
-
})));
|
|
200
|
-
const notValidResults = validationResults.filter((res) => !res.result.valid);
|
|
201
|
-
validationResults
|
|
202
|
-
.filter((res) => res.result.valid)
|
|
203
|
-
.forEach(({ key, result }) => {
|
|
204
|
-
objToValidate[key] = result.object;
|
|
205
|
-
});
|
|
206
|
-
errors = [
|
|
207
|
-
...errors,
|
|
208
|
-
...notValidResults.reduce((acc, val) => [...acc, ...(val?.result?.errors || [])], [])
|
|
209
|
-
];
|
|
210
|
-
for (let i = 0; i < objKeys.length; i++) {
|
|
211
|
-
const key = objKeys[i];
|
|
212
|
-
if (!(key in this.#properties) && !this.#acceptUnknownProps) {
|
|
213
|
-
errors.push({
|
|
214
|
-
message: `unknown property '${key}'`,
|
|
215
|
-
path: path
|
|
216
|
-
});
|
|
217
|
-
if (!doNotStopOnFirstError) {
|
|
218
|
-
if (validationTransaction) {
|
|
219
|
-
validationTransaction.rollback();
|
|
220
|
-
}
|
|
221
|
-
return {
|
|
222
|
-
valid: false,
|
|
223
|
-
errors: [errors[0]]
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
if (notValidResults.length === 0 && errors.length === 0) {
|
|
229
|
-
const commited = validationTransaction.commit();
|
|
230
|
-
return {
|
|
231
|
-
valid: true,
|
|
232
|
-
object: commited.validatedObject
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
validationTransaction.rollback();
|
|
236
|
-
return {
|
|
237
|
-
valid: false,
|
|
238
|
-
errors: doNotStopOnFirstError
|
|
239
|
-
? errors
|
|
240
|
-
: errors[0]
|
|
241
|
-
? [errors[0]]
|
|
242
|
-
: []
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Fields not defined in `properties` will not be validated
|
|
247
|
-
* and will be passed through the validation.
|
|
248
|
-
*/
|
|
249
|
-
acceptUnknownProps() {
|
|
250
|
-
return this.createFromProps({
|
|
251
|
-
...this.introspect(),
|
|
252
|
-
acceptUnknownProps: true
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Fields not defined in `properties` will be considered
|
|
257
|
-
* as schema violation. This is the default behavior.
|
|
258
|
-
*/
|
|
259
|
-
notAcceptUnknownProps() {
|
|
260
|
-
return this.createFromProps({
|
|
261
|
-
...this.introspect(),
|
|
262
|
-
acceptUnknownProps: false
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* @hidden
|
|
267
|
-
*/
|
|
268
|
-
hasType(
|
|
269
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
270
|
-
notUsed) {
|
|
271
|
-
return this.createFromProps({
|
|
272
|
-
...this.introspect()
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* @hidden
|
|
277
|
-
*/
|
|
278
|
-
clearHasType() {
|
|
279
|
-
return this.createFromProps({
|
|
280
|
-
...this.introspect()
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Adds a new property to the object schema. The new property
|
|
285
|
-
* will be validated according to the provided schema.
|
|
286
|
-
* @param propName name of the new property
|
|
287
|
-
* @param schema schema builder of the new property
|
|
288
|
-
*/
|
|
289
|
-
addProp(propName, schema) {
|
|
290
|
-
if (typeof propName !== 'string' || !propName) {
|
|
291
|
-
throw new Error('propName must be a non empty string');
|
|
292
|
-
}
|
|
293
|
-
if (propName in this.#properties) {
|
|
294
|
-
throw new Error(`Property ${propName} already exists`);
|
|
295
|
-
}
|
|
296
|
-
if (!(schema instanceof SchemaBuilder)) {
|
|
297
|
-
throw new Error('schema must be an instance of the SchemaBuilder class');
|
|
298
|
-
}
|
|
299
|
-
return this.createFromProps({
|
|
300
|
-
...this.introspect(),
|
|
301
|
-
properties: {
|
|
302
|
-
...this.introspect().properties,
|
|
303
|
-
[propName]: schema
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* @hidden
|
|
309
|
-
* @deprecated this is for internal use, do not use if you are
|
|
310
|
-
* not sure you need it.
|
|
311
|
-
*
|
|
312
|
-
* TODO: This is used to avoid `&` in resulting types. For example,
|
|
313
|
-
* when you have a schema like `object({prop1: string()})` and then use `addProp({prop2: string()})` method,
|
|
314
|
-
* the resulting type without `optimize` will be something like `{prop1: string} & {prop2: string}`. Which
|
|
315
|
-
* is not we would like to have. Instead we want to have `{prop1: string, prop2: string}`. This is what
|
|
316
|
-
* `optimize` method does. However it is not always possible to do this optimization without losing
|
|
317
|
-
* JSDoc comments, which is sucks. For example, I had to disable optimization for UnionSchemas, because
|
|
318
|
-
* comments were lost. Hopefully it will be fixed in the future by Typescript team or somebody will
|
|
319
|
-
* find a workaround/fix and create a pull request.
|
|
320
|
-
*/
|
|
321
|
-
optimize() {
|
|
322
|
-
return this.createFromProps({
|
|
323
|
-
...this.introspect()
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
addProps(props) {
|
|
327
|
-
if (props instanceof ObjectSchemaBuilder) {
|
|
328
|
-
return this.addProps(props.introspect().properties);
|
|
329
|
-
}
|
|
330
|
-
if (typeof props !== 'object') {
|
|
331
|
-
throw new Error('props should be an object');
|
|
332
|
-
}
|
|
333
|
-
if (props === null) {
|
|
334
|
-
throw new Error('props should not be null');
|
|
335
|
-
}
|
|
336
|
-
const newProps = { ...this.#properties };
|
|
337
|
-
for (const key in props) {
|
|
338
|
-
if (key in this.#properties) {
|
|
339
|
-
throw new Error(`property '${key}' already exists`);
|
|
340
|
-
}
|
|
341
|
-
if (!(props[key] instanceof SchemaBuilder)) {
|
|
342
|
-
throw new Error(`${key} is not a SchemaBuilder`);
|
|
343
|
-
}
|
|
344
|
-
newProps[key] = props[key];
|
|
345
|
-
}
|
|
346
|
-
return this.createFromProps({
|
|
347
|
-
...this.introspect(),
|
|
348
|
-
properties: newProps
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
omit(propNameOrArrayOrPropsOrBuilder) {
|
|
352
|
-
if (typeof propNameOrArrayOrPropsOrBuilder === 'string') {
|
|
353
|
-
// remove one field
|
|
354
|
-
const propName = propNameOrArrayOrPropsOrBuilder;
|
|
355
|
-
if (!propName || !(propName in this.#properties)) {
|
|
356
|
-
throw new Error(`property ${propName.toString()} does not exists in the schema`);
|
|
357
|
-
}
|
|
358
|
-
return this.createFromProps({
|
|
359
|
-
...this.introspect(),
|
|
360
|
-
properties: (() => {
|
|
361
|
-
const result = { ...this.#properties };
|
|
362
|
-
delete result[propName];
|
|
363
|
-
return result;
|
|
364
|
-
})()
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
else if (Array.isArray(propNameOrArrayOrPropsOrBuilder)) {
|
|
368
|
-
const propsArray = propNameOrArrayOrPropsOrBuilder;
|
|
369
|
-
const distinctKeys = new Map();
|
|
370
|
-
propsArray.forEach((key) => {
|
|
371
|
-
if (typeof key !== 'string' || !key) {
|
|
372
|
-
throw new Error('property name must be a string');
|
|
373
|
-
}
|
|
374
|
-
if (!(key in this.#properties)) {
|
|
375
|
-
throw new Error(`property ${key.toString()} does not exists in the schema`);
|
|
376
|
-
}
|
|
377
|
-
distinctKeys.set(key.toString(), true);
|
|
378
|
-
});
|
|
379
|
-
if (distinctKeys.size === 0) {
|
|
380
|
-
throw new Error('please provide at least one property to omit');
|
|
381
|
-
}
|
|
382
|
-
const props = {
|
|
383
|
-
...this.introspect()
|
|
384
|
-
};
|
|
385
|
-
for (const key of distinctKeys.keys()) {
|
|
386
|
-
delete props.properties[key];
|
|
387
|
-
}
|
|
388
|
-
return this.createFromProps(props);
|
|
389
|
-
}
|
|
390
|
-
else if (propNameOrArrayOrPropsOrBuilder instanceof ObjectSchemaBuilder) {
|
|
391
|
-
const propsToOmit = {
|
|
392
|
-
...propNameOrArrayOrPropsOrBuilder.introspect().properties
|
|
393
|
-
};
|
|
394
|
-
const props = {
|
|
395
|
-
...this.introspect()
|
|
396
|
-
};
|
|
397
|
-
for (const key in propsToOmit) {
|
|
398
|
-
if (key in props.properties) {
|
|
399
|
-
delete props.properties[key];
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
return this.createFromProps(props);
|
|
403
|
-
}
|
|
404
|
-
throw new Error('this parameter type is not supported');
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Adds all properties from `schema` to the current schema.
|
|
408
|
-
* `TSchema & TAnotherSchema` is a good example of the similar concept
|
|
409
|
-
* in the TS type system.
|
|
410
|
-
* @param schema an object schema to take properties from
|
|
411
|
-
*/
|
|
412
|
-
intersect(schema) {
|
|
413
|
-
if (!(schema instanceof ObjectSchemaBuilder)) {
|
|
414
|
-
throw new Error('schema must be an instance of the ObjectSchemaBuilder class');
|
|
415
|
-
}
|
|
416
|
-
const remoteProps = schema.introspect().properties;
|
|
417
|
-
const localProps = this.introspect();
|
|
418
|
-
const newProps = Object.keys(localProps.properties).reduce((acc, curr) => {
|
|
419
|
-
acc[curr] =
|
|
420
|
-
curr in remoteProps ? remoteProps[curr] : localProps[curr];
|
|
421
|
-
return acc;
|
|
422
|
-
}, {});
|
|
423
|
-
return this.createFromProps({
|
|
424
|
-
...this.introspect(),
|
|
425
|
-
properties: newProps
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
partial(propNameOrArray) {
|
|
429
|
-
if (typeof propNameOrArray === 'undefined' ||
|
|
430
|
-
propNameOrArray === null) {
|
|
431
|
-
return this.createFromProps({
|
|
432
|
-
...this.introspect(),
|
|
433
|
-
properties: Object.keys(this.#properties).reduce((acc, key) => {
|
|
434
|
-
acc[key] = this.#properties[key].optional();
|
|
435
|
-
return acc;
|
|
436
|
-
}, {})
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
if (Array.isArray(propNameOrArray)) {
|
|
440
|
-
const propsArray = propNameOrArray;
|
|
441
|
-
if (propsArray.length === 0) {
|
|
442
|
-
throw new Error('properties cannot be empty');
|
|
443
|
-
}
|
|
444
|
-
const newProps = {
|
|
445
|
-
...this.introspect()
|
|
446
|
-
};
|
|
447
|
-
propsArray.forEach((key) => {
|
|
448
|
-
if (typeof key !== 'string') {
|
|
449
|
-
throw new Error('each propery in property list must be as string value');
|
|
450
|
-
}
|
|
451
|
-
if (!(key in newProps.properties)) {
|
|
452
|
-
throw new Error(`property ${key} does not exists`);
|
|
453
|
-
}
|
|
454
|
-
newProps.properties[key] =
|
|
455
|
-
newProps.properties[key].optional();
|
|
456
|
-
});
|
|
457
|
-
return this.createFromProps(newProps);
|
|
458
|
-
}
|
|
459
|
-
if (typeof propNameOrArray === 'string') {
|
|
460
|
-
return this.modifyPropSchema(propNameOrArray, (schema) => schema.optional());
|
|
461
|
-
}
|
|
462
|
-
throw new Error('expecting string or string[] parameter');
|
|
463
|
-
}
|
|
464
|
-
pick(properties) {
|
|
465
|
-
if (typeof properties === 'string') {
|
|
466
|
-
const property = properties;
|
|
467
|
-
if (!property) {
|
|
468
|
-
throw new Error('property cannot be empty');
|
|
469
|
-
}
|
|
470
|
-
if (!(property in this.#properties)) {
|
|
471
|
-
throw new Error(`property ${property} does not exists`);
|
|
472
|
-
}
|
|
473
|
-
return this.createFromProps({
|
|
474
|
-
...this.introspect(),
|
|
475
|
-
properties: {
|
|
476
|
-
[property]: this.#properties[property]
|
|
477
|
-
}
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
if (Array.isArray(properties)) {
|
|
481
|
-
if (properties.length === 0) {
|
|
482
|
-
throw new Error('properties must be a non empty erray');
|
|
483
|
-
}
|
|
484
|
-
const newProperties = properties.reduce((acc, curr) => {
|
|
485
|
-
if (typeof curr !== 'string' || !curr) {
|
|
486
|
-
throw new Error('each property name must be a non empty string');
|
|
487
|
-
}
|
|
488
|
-
if (!(curr in this.#properties)) {
|
|
489
|
-
throw new Error(`property ${curr} does not exists`);
|
|
490
|
-
}
|
|
491
|
-
acc[curr] = this.#properties[curr];
|
|
492
|
-
return acc;
|
|
493
|
-
}, {});
|
|
494
|
-
return this.createFromProps({
|
|
495
|
-
...this.introspect(),
|
|
496
|
-
properties: newProperties
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
if (properties instanceof ObjectSchemaBuilder) {
|
|
500
|
-
const externalSchema = properties;
|
|
501
|
-
const props = Object.keys(externalSchema.introspect().properties).filter((p) => typeof this.#properties[p] !== 'undefined');
|
|
502
|
-
if (props.length === 0) {
|
|
503
|
-
throw new Error('there are no common properties in provided schemas');
|
|
504
|
-
}
|
|
505
|
-
return this.pick(props);
|
|
506
|
-
}
|
|
507
|
-
throw new Error('string, array or ObjectSchemaBuilder is expected');
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Modify schema for `propName` and return a new schema.
|
|
511
|
-
* Could be useful if you want to leave all schema intact, but
|
|
512
|
-
* change a type of one property.
|
|
513
|
-
* @param propName name of the property (string)
|
|
514
|
-
* @param callback callback function returning a new schema fo the `propName`. As a first parameter
|
|
515
|
-
* you will receive an old schema for `propName`.
|
|
516
|
-
* @returns
|
|
517
|
-
*/
|
|
518
|
-
modifyPropSchema(propName, callback) {
|
|
519
|
-
if (typeof propName !== 'string' || !propName) {
|
|
520
|
-
throw new Error('propName must be a non empty string');
|
|
521
|
-
}
|
|
522
|
-
if (!(propName in this.#properties)) {
|
|
523
|
-
throw new Error(`property ${propName} does not exists in the schema`);
|
|
524
|
-
}
|
|
525
|
-
if (typeof callback !== 'function') {
|
|
526
|
-
throw new Error('callback must be a function');
|
|
527
|
-
}
|
|
528
|
-
const callbackResult = callback(this.#properties[propName]);
|
|
529
|
-
if (!(callbackResult instanceof SchemaBuilder)) {
|
|
530
|
-
throw new Error('callback must return a SchemaBuilder object');
|
|
531
|
-
}
|
|
532
|
-
const props = {
|
|
533
|
-
...this.introspect()
|
|
534
|
-
};
|
|
535
|
-
props.properties = {
|
|
536
|
-
...props.properties,
|
|
537
|
-
[propName]: callbackResult
|
|
538
|
-
};
|
|
539
|
-
return this.createFromProps(props);
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* An alias for `.partial(prop: string)`
|
|
543
|
-
* @param prop name of the property
|
|
544
|
-
*/
|
|
545
|
-
makePropOptional(prop) {
|
|
546
|
-
return this.modifyPropSchema(prop, (builder) => builder.optional());
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Marks `prop` as required property.
|
|
550
|
-
* If `prop` does not exists in the current schema,
|
|
551
|
-
* an error will be thrown.
|
|
552
|
-
* @param prop name of the property
|
|
553
|
-
*/
|
|
554
|
-
makePropRequired(prop) {
|
|
555
|
-
return this.modifyPropSchema(prop, (builder) => builder.required());
|
|
556
|
-
}
|
|
557
|
-
/**
|
|
558
|
-
* `Partial<T>` would be a good example of the
|
|
559
|
-
* same operation in the TS world.
|
|
560
|
-
*/
|
|
561
|
-
makeAllPropsOptional() {
|
|
562
|
-
return this.createFromProps({
|
|
563
|
-
...this.introspect(),
|
|
564
|
-
properties: Object.keys(this.#properties).reduce((acc, curr) => {
|
|
565
|
-
acc[curr] = this.#properties[curr].optional();
|
|
566
|
-
return acc;
|
|
567
|
-
}, {})
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
* `Required<T>` would be a good example of the
|
|
572
|
-
* same operation in the TS world.
|
|
573
|
-
*/
|
|
574
|
-
makeAllPropsRequired() {
|
|
575
|
-
return this.createFromProps({
|
|
576
|
-
...this.introspect(),
|
|
577
|
-
properties: Object.keys(this.#properties).reduce((acc, curr) => {
|
|
578
|
-
acc[curr] = this.#properties[curr].required();
|
|
579
|
-
return acc;
|
|
580
|
-
}, {})
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
export function object(props) {
|
|
585
|
-
return ObjectSchemaBuilder.create({
|
|
586
|
-
isRequired: true,
|
|
587
|
-
properties: props
|
|
588
|
-
});
|
|
589
|
-
}
|
|
1
|
+
import{a,b}from"../chunk-NXPH3ZUW.js";import"../chunk-CTP4RHDG.js";export{a as ObjectSchemaBuilder,b as object};
|
|
2
|
+
//# sourceMappingURL=ObjectSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ObjectSchemaBuilder } from './ObjectSchemaBuilder.js';
|
|
2
|
+
import { type InferType, type NestedValidationResult, type PropertyDescriptorInner, type PropertyDescriptorTree } from './SchemaBuilder.js';
|
|
3
|
+
/**
|
|
4
|
+
* Mutable container for nested validation results associated with a specific
|
|
5
|
+
* property descriptor. Implements {@link NestedValidationResult} and tracks
|
|
6
|
+
* the seen value, accumulated error messages, and child results for
|
|
7
|
+
* nested object properties.
|
|
8
|
+
*
|
|
9
|
+
* Used internally by `ObjectSchemaBuilder` during validation to build up
|
|
10
|
+
* a tree of per-property validation results.
|
|
11
|
+
*/
|
|
12
|
+
export declare class PropertyValidationResult<TSchema extends ObjectSchemaBuilder<any, any, any, any, any> = ObjectSchemaBuilder<any, any, any, any, any>, TRootSchema extends ObjectSchemaBuilder<any, any, any, any, any> = ObjectSchemaBuilder<any, any, any, any, any>, TParentPropertyDescriptor = any> implements NestedValidationResult<TSchema, TRootSchema, TParentPropertyDescriptor> {
|
|
13
|
+
#private;
|
|
14
|
+
/**
|
|
15
|
+
* The value that was seen at the property location described by the descriptor.
|
|
16
|
+
* Retrieves the value from the root object using the property descriptor's `getValue` method.
|
|
17
|
+
* Returns `undefined` if the property is not found.
|
|
18
|
+
*/
|
|
19
|
+
get seenValue(): InferType<TSchema> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* The list of validation error messages accumulated for this property.
|
|
22
|
+
*/
|
|
23
|
+
get errors(): ReadonlyArray<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Whether validation passed for this property and all of its children.
|
|
26
|
+
* Returns `true` only when there are no errors and no child errors.
|
|
27
|
+
*/
|
|
28
|
+
get isValid(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the list of child `NestedValidationResult` instances
|
|
31
|
+
* representing validation results for nested properties.
|
|
32
|
+
*/
|
|
33
|
+
getChildErrors(): ReadonlyArray<NestedValidationResult<any, any, any>>;
|
|
34
|
+
/**
|
|
35
|
+
* The inner property descriptor providing `getValue`, `setValue`, and `getSchema`
|
|
36
|
+
* operations for the property this error relates to.
|
|
37
|
+
*/
|
|
38
|
+
get descriptor(): PropertyDescriptorInner<TRootSchema, TSchema, TParentPropertyDescriptor>;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new `PropertyValidationResult`.
|
|
41
|
+
*
|
|
42
|
+
* @param descriptor - the property descriptor tree node this error is associated with;
|
|
43
|
+
* must be a valid descriptor (checked via `ObjectSchemaBuilder.isValidPropertyDescriptor`)
|
|
44
|
+
* @param rootObjectValue - the root object being validated, used to resolve property values
|
|
45
|
+
* @param errors - optional initial list of error message strings
|
|
46
|
+
* @throws if `descriptor` is not a valid property descriptor
|
|
47
|
+
*/
|
|
48
|
+
constructor(descriptor: PropertyDescriptorTree<TSchema, TRootSchema, any, TParentPropertyDescriptor>, rootObjectValue: InferType<TRootSchema> | undefined, errors?: string[]);
|
|
49
|
+
/**
|
|
50
|
+
* Appends a validation error message to this property's error list.
|
|
51
|
+
* @param error - the error message string to add
|
|
52
|
+
*/
|
|
53
|
+
addError(error: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* Appends a child `NestedValidationResult` for a nested property.
|
|
56
|
+
* @param childError - the child validation result to add
|
|
57
|
+
*/
|
|
58
|
+
addChildError(childError: NestedValidationResult<any, any, any>): void;
|
|
59
|
+
/**
|
|
60
|
+
* Returns a JSON-serializable representation of this validation result.
|
|
61
|
+
* This ensures `JSON.stringify` includes `isValid` and `errors`,
|
|
62
|
+
* which are otherwise non-enumerable prototype getters.
|
|
63
|
+
*/
|
|
64
|
+
toJSON(): {
|
|
65
|
+
isValid: boolean;
|
|
66
|
+
errors: ReadonlyArray<string>;
|
|
67
|
+
};
|
|
68
|
+
}
|