@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,414 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Allows to define a schema for a string. It can be: required or optional,
|
|
4
|
-
* restricted to be equal to a certain value, restricted to have a certain
|
|
5
|
-
* length, restricted to start with a certain value, restricted to end with
|
|
6
|
-
* a certain value, restricted to match a certain regular expression.
|
|
7
|
-
*
|
|
8
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
9
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
10
|
-
* directly. Use {@link string | string()} function instead.
|
|
11
|
-
*
|
|
12
|
-
* @example ```ts
|
|
13
|
-
* const schema = string().equals('hello');
|
|
14
|
-
* const result = await schema.validate('hello');
|
|
15
|
-
* // result.valid === true
|
|
16
|
-
* // result.object === 'hello'
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @example ```ts
|
|
20
|
-
* const schema = string().equals('hello');
|
|
21
|
-
* const result = await schema.validate('world');
|
|
22
|
-
* // result.valid === false
|
|
23
|
-
* // result.errors[0].message === "is expected to be equal to 'hello'"
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @example ```ts
|
|
27
|
-
* const schema = string().minLength(5);
|
|
28
|
-
* const result = await schema.validate('hello');
|
|
29
|
-
* // result.valid === true
|
|
30
|
-
* // result.object === 'hello'
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @example ```ts
|
|
34
|
-
* const schema = string().minLength(5);
|
|
35
|
-
* const result = await schema.validate('hi');
|
|
36
|
-
* // result.valid === false
|
|
37
|
-
* // result.errors[0].message === 'is expected to have a length of at least 5'
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example ```ts
|
|
41
|
-
* const schema = string().minLength(2).maxLength(5);
|
|
42
|
-
* const result = await schema.validate('yes');
|
|
43
|
-
* // result.valid === true
|
|
44
|
-
* // result.object === 'yes'
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @example ```ts
|
|
48
|
-
* const schema = string('no');
|
|
49
|
-
* const result = await schema.validate('yes');
|
|
50
|
-
* // result.valid === false
|
|
51
|
-
* // result.errors[0].message === "is expected to be equal to 'no'"
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* @see {@link string}
|
|
55
|
-
*/
|
|
56
|
-
export class StringSchemaBuilder extends SchemaBuilder {
|
|
57
|
-
#minLength;
|
|
58
|
-
#maxLength;
|
|
59
|
-
#equalsTo;
|
|
60
|
-
#startsWith;
|
|
61
|
-
#endsWith;
|
|
62
|
-
#matches;
|
|
63
|
-
static create(props) {
|
|
64
|
-
return new StringSchemaBuilder({
|
|
65
|
-
type: 'string',
|
|
66
|
-
...props
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
constructor(props) {
|
|
70
|
-
super(props);
|
|
71
|
-
if (typeof props.minLength === 'number') {
|
|
72
|
-
this.#minLength = props.minLength;
|
|
73
|
-
}
|
|
74
|
-
if (typeof props.maxLength === 'number') {
|
|
75
|
-
this.#maxLength = props.maxLength;
|
|
76
|
-
}
|
|
77
|
-
if (typeof props.equalsTo === 'string' ||
|
|
78
|
-
typeof props.equalsTo === 'undefined') {
|
|
79
|
-
this.#equalsTo = props.equalsTo;
|
|
80
|
-
}
|
|
81
|
-
if (typeof props.startsWith === 'string' &&
|
|
82
|
-
props.startsWith.length > 0) {
|
|
83
|
-
this.#startsWith = props.startsWith;
|
|
84
|
-
}
|
|
85
|
-
if (typeof props.endsWith === 'string' && props.endsWith.length > 0) {
|
|
86
|
-
this.#endsWith = props.endsWith;
|
|
87
|
-
}
|
|
88
|
-
if (props.matches instanceof RegExp) {
|
|
89
|
-
this.#matches = props.matches;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
introspect() {
|
|
93
|
-
return {
|
|
94
|
-
...super.introspect(),
|
|
95
|
-
/**
|
|
96
|
-
* Min length of the string (if defined).
|
|
97
|
-
*/
|
|
98
|
-
minLength: this.#minLength,
|
|
99
|
-
/**
|
|
100
|
-
* Max length of the string (if defined).
|
|
101
|
-
*/
|
|
102
|
-
maxLength: this.#maxLength,
|
|
103
|
-
/**
|
|
104
|
-
* If set, restrict object to be equal to a certain value.
|
|
105
|
-
*/
|
|
106
|
-
equalsTo: this.#equalsTo,
|
|
107
|
-
/**
|
|
108
|
-
* If set, restrict string to start with a certain value.
|
|
109
|
-
*/
|
|
110
|
-
startsWith: this.#startsWith,
|
|
111
|
-
/**
|
|
112
|
-
* If set, restrict string to end with a certain value.
|
|
113
|
-
*/
|
|
114
|
-
endsWith: this.#endsWith,
|
|
115
|
-
/**
|
|
116
|
-
* If set, restrict string to match a certain regular expression.
|
|
117
|
-
*/
|
|
118
|
-
matches: this.#matches,
|
|
119
|
-
/**
|
|
120
|
-
* Array of preprocessor functions
|
|
121
|
-
*/
|
|
122
|
-
preprocessors: this.preprocessors,
|
|
123
|
-
/**
|
|
124
|
-
* Array of validator functions
|
|
125
|
-
*/
|
|
126
|
-
validators: this.validators
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* @hidden
|
|
131
|
-
*/
|
|
132
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
133
|
-
hasType(notUsed) {
|
|
134
|
-
return this.createFromProps({
|
|
135
|
-
...this.introspect()
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* @hidden
|
|
140
|
-
*/
|
|
141
|
-
clearHasType() {
|
|
142
|
-
return this.createFromProps({
|
|
143
|
-
...this.introspect()
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Performs validion of string schema over `object`.
|
|
148
|
-
* @param context Optional `ValidationContext` settings.
|
|
149
|
-
*/
|
|
150
|
-
async validate(object, context) {
|
|
151
|
-
const superResult = await super.preValidate(object, context);
|
|
152
|
-
const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
|
|
153
|
-
const { path } = prevalidationContext;
|
|
154
|
-
if (!valid) {
|
|
155
|
-
return {
|
|
156
|
-
valid,
|
|
157
|
-
errors
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
161
|
-
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
162
|
-
this.isRequired === false) {
|
|
163
|
-
return {
|
|
164
|
-
valid: true,
|
|
165
|
-
object: objToValidate
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
if (typeof objToValidate !== 'string')
|
|
169
|
-
return {
|
|
170
|
-
valid: false,
|
|
171
|
-
errors: [
|
|
172
|
-
{
|
|
173
|
-
message: `expected type string, but saw ${typeof objToValidate}`,
|
|
174
|
-
path: path
|
|
175
|
-
}
|
|
176
|
-
]
|
|
177
|
-
};
|
|
178
|
-
if (typeof this.#equalsTo !== 'undefined' &&
|
|
179
|
-
objToValidate !== this.#equalsTo) {
|
|
180
|
-
return {
|
|
181
|
-
valid: false,
|
|
182
|
-
errors: [
|
|
183
|
-
{
|
|
184
|
-
message: `is expected to be equal to ${this.#equalsTo}`,
|
|
185
|
-
path: path
|
|
186
|
-
}
|
|
187
|
-
]
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
if (typeof this.#startsWith === 'string' &&
|
|
191
|
-
this.#startsWith.length > 0 &&
|
|
192
|
-
!objToValidate.startsWith(this.#startsWith)) {
|
|
193
|
-
return {
|
|
194
|
-
valid: false,
|
|
195
|
-
errors: [
|
|
196
|
-
{
|
|
197
|
-
message: `is expected to start with '${this.#startsWith}'`,
|
|
198
|
-
path: path
|
|
199
|
-
}
|
|
200
|
-
]
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
if (typeof this.#endsWith === 'string' &&
|
|
204
|
-
this.#endsWith.length > 0 &&
|
|
205
|
-
!objToValidate.endsWith(this.#endsWith)) {
|
|
206
|
-
return {
|
|
207
|
-
valid: false,
|
|
208
|
-
errors: [
|
|
209
|
-
{
|
|
210
|
-
message: `is expected to end with '${this.#endsWith}'`,
|
|
211
|
-
path: path
|
|
212
|
-
}
|
|
213
|
-
]
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
if (typeof this.#minLength !== 'undefined') {
|
|
217
|
-
if (objToValidate.length < this.#minLength)
|
|
218
|
-
return {
|
|
219
|
-
valid: false,
|
|
220
|
-
errors: [
|
|
221
|
-
{
|
|
222
|
-
message: `expected to has at least ${this.#minLength} characters length`,
|
|
223
|
-
path: path
|
|
224
|
-
}
|
|
225
|
-
]
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
if (typeof this.#maxLength !== 'undefined') {
|
|
229
|
-
if (objToValidate.length > this.#maxLength)
|
|
230
|
-
return {
|
|
231
|
-
valid: false,
|
|
232
|
-
errors: [
|
|
233
|
-
{
|
|
234
|
-
message: `must not exceed ${this.#maxLength} characters`,
|
|
235
|
-
path: path
|
|
236
|
-
}
|
|
237
|
-
]
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
if (this.#matches instanceof RegExp) {
|
|
241
|
-
if (!this.#matches.test(objToValidate)) {
|
|
242
|
-
return {
|
|
243
|
-
valid: false,
|
|
244
|
-
errors: [
|
|
245
|
-
{
|
|
246
|
-
message: `does not match to ${this.#matches}`,
|
|
247
|
-
path: path
|
|
248
|
-
}
|
|
249
|
-
]
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
return {
|
|
254
|
-
valid: true,
|
|
255
|
-
object: objToValidate
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
createFromProps(props) {
|
|
259
|
-
return StringSchemaBuilder.create(props);
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Restricts object to be equal to `value`.
|
|
263
|
-
*/
|
|
264
|
-
equals(value) {
|
|
265
|
-
if (typeof value !== 'string')
|
|
266
|
-
throw new Error('string expected');
|
|
267
|
-
return this.createFromProps({
|
|
268
|
-
...this.introspect(),
|
|
269
|
-
equalsTo: value
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Cancels `equals()` call.
|
|
274
|
-
*/
|
|
275
|
-
clearEquals() {
|
|
276
|
-
return this.createFromProps({
|
|
277
|
-
...this.introspect(),
|
|
278
|
-
equalsTo: undefined
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* @hidden
|
|
283
|
-
*/
|
|
284
|
-
required() {
|
|
285
|
-
return super.required();
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* @hidden
|
|
289
|
-
*/
|
|
290
|
-
optional() {
|
|
291
|
-
return super.optional();
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* Set minimal length of the valid value for schema.
|
|
295
|
-
* @param {number} length
|
|
296
|
-
*/
|
|
297
|
-
minLength(length) {
|
|
298
|
-
if (typeof length !== 'number')
|
|
299
|
-
throw new Error('length must be a number');
|
|
300
|
-
return this.createFromProps({
|
|
301
|
-
...this.introspect(),
|
|
302
|
-
minLength: length
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Cancel `minLength()` call.
|
|
307
|
-
*/
|
|
308
|
-
clearMinLength() {
|
|
309
|
-
const schema = this.introspect();
|
|
310
|
-
delete schema.minLength;
|
|
311
|
-
return this.createFromProps({
|
|
312
|
-
...schema
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Set maximal length of the valid value for schema.
|
|
317
|
-
* @length {number} length
|
|
318
|
-
*/
|
|
319
|
-
maxLength(length) {
|
|
320
|
-
if (typeof length !== 'number')
|
|
321
|
-
throw new Error('length must be a number');
|
|
322
|
-
return this.createFromProps({
|
|
323
|
-
...this.introspect(),
|
|
324
|
-
maxLength: length
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* cancel `maxLength()` call.
|
|
329
|
-
*/
|
|
330
|
-
clearMaxLength() {
|
|
331
|
-
const schema = this.introspect();
|
|
332
|
-
delete schema.maxLength;
|
|
333
|
-
return this.createFromProps({
|
|
334
|
-
...schema
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Restricts string to start with `val`.
|
|
339
|
-
*/
|
|
340
|
-
startsWith(val) {
|
|
341
|
-
if (typeof val !== 'string' || !val)
|
|
342
|
-
throw new Error('non empty string expected');
|
|
343
|
-
return this.createFromProps({
|
|
344
|
-
...this.introspect(),
|
|
345
|
-
startsWith: val
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Cancels `startsWith()` call.
|
|
350
|
-
*/
|
|
351
|
-
clearStartsWith() {
|
|
352
|
-
const schema = this.introspect();
|
|
353
|
-
delete schema.startsWith;
|
|
354
|
-
return this.createFromProps({
|
|
355
|
-
...schema
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Restricts string to end with `val`.
|
|
360
|
-
*/
|
|
361
|
-
endsWith(val) {
|
|
362
|
-
if (typeof val !== 'string' || !val)
|
|
363
|
-
throw new Error('non empty string expected');
|
|
364
|
-
return this.createFromProps({
|
|
365
|
-
...this.introspect(),
|
|
366
|
-
endsWith: val
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Cancels `endsWith()` call.
|
|
371
|
-
*/
|
|
372
|
-
clearEndsWith() {
|
|
373
|
-
const schema = this.introspect();
|
|
374
|
-
delete schema.endsWith;
|
|
375
|
-
return this.createFromProps({
|
|
376
|
-
...schema
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Restricts string to match `regexp`.
|
|
381
|
-
*/
|
|
382
|
-
matches(regexp) {
|
|
383
|
-
if (!(regexp instanceof RegExp))
|
|
384
|
-
throw new Error('regexp expected');
|
|
385
|
-
return this.createFromProps({
|
|
386
|
-
...this.introspect(),
|
|
387
|
-
matches: regexp
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Cancels `matches()` call.
|
|
392
|
-
*/
|
|
393
|
-
clearMatches() {
|
|
394
|
-
const schema = this.introspect();
|
|
395
|
-
delete schema.matches;
|
|
396
|
-
return this.createFromProps({
|
|
397
|
-
...schema
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Creates a string schema.
|
|
403
|
-
*/
|
|
404
|
-
export function string(equals) {
|
|
405
|
-
if (typeof equals === 'string') {
|
|
406
|
-
return StringSchemaBuilder.create({
|
|
407
|
-
isRequired: true,
|
|
408
|
-
equalsTo: equals
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
return StringSchemaBuilder.create({
|
|
412
|
-
isRequired: true
|
|
413
|
-
});
|
|
414
|
-
}
|
|
1
|
+
import{a,b}from"../chunk-FWUEUW2N.js";import"../chunk-CTP4RHDG.js";export{a as StringSchemaBuilder,b as string};
|
|
2
|
+
//# sourceMappingURL=StringSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import type { ObjectSchemaBuilder, ObjectSchemaValidationResult } from './ObjectSchemaBuilder.js';
|
|
2
|
+
import { type BRAND, type InferType, type NestedValidationResult, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
3
|
+
import type { UnionSchemaBuilder, UnionSchemaValidationResult } from './UnionSchemaBuilder.js';
|
|
4
|
+
/**
|
|
5
|
+
* Maps a tuple of schema builders to a tuple of their per-position
|
|
6
|
+
* validation result types.
|
|
7
|
+
* Union schema elements get `UnionSchemaValidationResult`,
|
|
8
|
+
* object schema elements get `ObjectSchemaValidationResult`,
|
|
9
|
+
* other types get `ValidationResult`.
|
|
10
|
+
*/
|
|
11
|
+
export type TupleElementValidationResults<TElements extends readonly SchemaBuilder<any, any, any, any, any>[]> = {
|
|
12
|
+
[K in keyof TElements]: TElements[K] extends UnionSchemaBuilder<infer UOptions extends readonly SchemaBuilder<any, any, any, any, any>[], any, any> ? UnionSchemaValidationResult<InferType<TElements[K]>, UOptions> : TElements[K] extends ObjectSchemaBuilder<any, any, any, any, any> ? ObjectSchemaValidationResult<InferType<TElements[K]>, TElements[K]> : ValidationResult<InferType<TElements[K]>>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Validation result type returned by `TupleSchemaBuilder.validate()`.
|
|
16
|
+
* Extends `ValidationResult` with `getNestedErrors` for root-level tuple
|
|
17
|
+
* errors and per-position validation results.
|
|
18
|
+
*/
|
|
19
|
+
export type TupleSchemaValidationResult<TResult, TElements extends readonly SchemaBuilder<any, any, any, any, any>[]> = ValidationResult<TResult> & {
|
|
20
|
+
/**
|
|
21
|
+
* Returns root-level tuple validation errors combined with
|
|
22
|
+
* per-position validation results.
|
|
23
|
+
* The returned value has both `NestedValidationResult` properties
|
|
24
|
+
* (`errors`, `isValid`, `descriptor`, `seenValue`) and indexed
|
|
25
|
+
* position results (`[0]`, `[1]`, etc.).
|
|
26
|
+
*/
|
|
27
|
+
getNestedErrors(): TupleElementValidationResults<TElements> & NestedValidationResult<any, any, any>;
|
|
28
|
+
};
|
|
29
|
+
type TupleSchemaBuilderCreateProps<TElements extends readonly SchemaBuilder<any, any, any, any, any>[], TRestSchema extends SchemaBuilder<any, any, any, any, any> | undefined = undefined, R extends boolean = true, N extends boolean = false> = Partial<ReturnType<TupleSchemaBuilder<TElements, R, N, undefined, false, {}, TRestSchema>['introspect']>>;
|
|
30
|
+
/**
|
|
31
|
+
* Fixed-length array schema builder with per-position type validation.
|
|
32
|
+
* Similar to TypeScript's tuple types — each element at a specific array index
|
|
33
|
+
* is validated against its own schema.
|
|
34
|
+
*
|
|
35
|
+
* Use it when you need to validate function arguments, CSV rows, coordinate
|
|
36
|
+
* pairs, structured event payloads, or any other fixed-structure array.
|
|
37
|
+
*
|
|
38
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
39
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
40
|
+
* directly. Use {@link tuple | tuple()} function instead.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const schema = tuple([string(), number(), boolean()]);
|
|
45
|
+
* // Inferred TypeScript type: [string, number, boolean]
|
|
46
|
+
*
|
|
47
|
+
* schema.validate(['hello', 42, true]);
|
|
48
|
+
* // result.valid === true
|
|
49
|
+
* // result.object === ['hello', 42, true]
|
|
50
|
+
*
|
|
51
|
+
* schema.validate(['hello', 42]);
|
|
52
|
+
* // result.valid === false (too few elements)
|
|
53
|
+
*
|
|
54
|
+
* schema.validate(['hello', 'oops', true]);
|
|
55
|
+
* // result.valid === false (wrong type at position 1)
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* // Tuple with variadic rest elements
|
|
61
|
+
* const schema = tuple([string(), number()]).rest(boolean());
|
|
62
|
+
* // Inferred TypeScript type: [string, number, ...boolean[]]
|
|
63
|
+
*
|
|
64
|
+
* schema.validate(['hello', 42, true, false]);
|
|
65
|
+
* // result.valid === true — any number of extra booleans allowed
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* // Nested tuple combining with object schemas
|
|
71
|
+
* const point = tuple([number(), number()]);
|
|
72
|
+
* const segment = tuple([point, point]);
|
|
73
|
+
*
|
|
74
|
+
* segment.validate([[0, 0], [10, 20]]);
|
|
75
|
+
* // result.valid === true
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @see {@link tuple}
|
|
79
|
+
*/
|
|
80
|
+
export declare class TupleSchemaBuilder<TElements extends readonly SchemaBuilder<any, any, any, any, any>[], TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TRestSchema extends SchemaBuilder<any, any, any, any, any> | undefined = undefined, TResult = TExplicitType extends undefined ? TRestSchema extends SchemaBuilder<any, any, any, any, any> ? [
|
|
81
|
+
...{
|
|
82
|
+
[K in keyof TElements]: InferType<TElements[K]>;
|
|
83
|
+
},
|
|
84
|
+
...Array<InferType<TRestSchema>>
|
|
85
|
+
] : {
|
|
86
|
+
[K in keyof TElements]: InferType<TElements[K]>;
|
|
87
|
+
} : TExplicitType> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
88
|
+
#private;
|
|
89
|
+
/**
|
|
90
|
+
* @hidden
|
|
91
|
+
*/
|
|
92
|
+
static create(props: TupleSchemaBuilderCreateProps<any, any, any, any>): TupleSchemaBuilder<readonly SchemaBuilder<any, any, any, any, any>[], true, false, undefined, false, {}, undefined, readonly any[]>;
|
|
93
|
+
protected constructor(props: TupleSchemaBuilderCreateProps<TElements, TRestSchema, TRequired, TNullable>);
|
|
94
|
+
/**
|
|
95
|
+
* @inheritdoc
|
|
96
|
+
*/
|
|
97
|
+
hasType<T>(_notUsed?: T): TupleSchemaBuilder<TElements, true, TNullable, T, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
98
|
+
/**
|
|
99
|
+
* @inheritdoc
|
|
100
|
+
*/
|
|
101
|
+
clearHasType(): TupleSchemaBuilder<TElements, TRequired, TNullable, undefined, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
102
|
+
/**
|
|
103
|
+
* Performs synchronous validation of the schema over `object`. {@inheritDoc SchemaBuilder.validate}
|
|
104
|
+
*/
|
|
105
|
+
validate(object: TResult, context?: ValidationContext): TupleSchemaValidationResult<TResult, TElements>;
|
|
106
|
+
/**
|
|
107
|
+
* Performs asynchronous validation of the schema over `object`. {@inheritDoc SchemaBuilder.validateAsync}
|
|
108
|
+
*/
|
|
109
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<TupleSchemaValidationResult<TResult, TElements>>;
|
|
110
|
+
/**
|
|
111
|
+
* Performs synchronous validation of the schema over `object`.
|
|
112
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
113
|
+
* @param context Optional `ValidationContext` settings.
|
|
114
|
+
*/
|
|
115
|
+
protected _validate(object: TResult, context?: ValidationContext): TupleSchemaValidationResult<TResult, TElements>;
|
|
116
|
+
/**
|
|
117
|
+
* Performs async validation of the schema over `object`.
|
|
118
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
119
|
+
* @param context Optional `ValidationContext` settings.
|
|
120
|
+
*/
|
|
121
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<TupleSchemaValidationResult<TResult, TElements>>;
|
|
122
|
+
/**
|
|
123
|
+
* @hidden
|
|
124
|
+
*/
|
|
125
|
+
protected createFromProps<TReq extends boolean>(props: TupleSchemaBuilderCreateProps<TElements, TRestSchema, TReq>): this;
|
|
126
|
+
/**
|
|
127
|
+
* @hidden
|
|
128
|
+
*/
|
|
129
|
+
required(errorMessage?: ValidationErrorMessageProvider): TupleSchemaBuilder<TElements, true, TNullable, TExplicitType, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
130
|
+
/**
|
|
131
|
+
* @hidden
|
|
132
|
+
*/
|
|
133
|
+
optional(): TupleSchemaBuilder<TElements, false, TNullable, TExplicitType, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
134
|
+
/**
|
|
135
|
+
* @hidden
|
|
136
|
+
*/
|
|
137
|
+
default(value: TResult | (() => TResult)): TupleSchemaBuilder<TElements, true, TNullable, TExplicitType, true, TExtensions, TRestSchema> & TExtensions;
|
|
138
|
+
/**
|
|
139
|
+
* @hidden
|
|
140
|
+
*/
|
|
141
|
+
clearDefault(): TupleSchemaBuilder<TElements, TRequired, TNullable, TExplicitType, false, TExtensions, TRestSchema> & TExtensions;
|
|
142
|
+
/**
|
|
143
|
+
* @hidden
|
|
144
|
+
*/
|
|
145
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): TupleSchemaBuilder<TElements, TRequired, TNullable, TResult & {
|
|
146
|
+
readonly [K in BRAND]: TBrand;
|
|
147
|
+
}, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
148
|
+
introspect(): {
|
|
149
|
+
/**
|
|
150
|
+
* Per-position element schemas defining the fixed tuple structure.
|
|
151
|
+
*/
|
|
152
|
+
elements: TElements;
|
|
153
|
+
/**
|
|
154
|
+
* Optional schema for elements beyond the fixed positions.
|
|
155
|
+
* When set, additional elements are validated against this schema.
|
|
156
|
+
* Mirrors TypeScript's rest element syntax: `[string, number, ...boolean[]]`.
|
|
157
|
+
*/
|
|
158
|
+
restSchema: (TRestSchema & SchemaBuilder<any, any, any, any, any>) | undefined;
|
|
159
|
+
type: string;
|
|
160
|
+
isRequired: boolean;
|
|
161
|
+
isNullable: boolean;
|
|
162
|
+
isReadonly: boolean;
|
|
163
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TResult>[];
|
|
164
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TResult>[];
|
|
165
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
166
|
+
extensions: {
|
|
167
|
+
[x: string]: unknown;
|
|
168
|
+
};
|
|
169
|
+
hasDefault: boolean;
|
|
170
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
171
|
+
description: string | undefined;
|
|
172
|
+
hasCatch: boolean;
|
|
173
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Sets a schema that all elements beyond the fixed positions must satisfy.
|
|
177
|
+
* Mirrors TypeScript's variadic tuple tail: `[string, number, ...boolean[]]`.
|
|
178
|
+
*
|
|
179
|
+
* When set, the tuple length must be at least equal to the number of fixed
|
|
180
|
+
* elements, and any additional elements are validated against `schema`.
|
|
181
|
+
* When not set, the tuple length must be exactly equal to the fixed count.
|
|
182
|
+
*
|
|
183
|
+
* @param schema Schema that extra array elements must satisfy.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* const schema = tuple([string(), number()]).rest(boolean());
|
|
188
|
+
* // Inferred TypeScript type: [string, number, ...boolean[]]
|
|
189
|
+
*
|
|
190
|
+
* schema.validate(['hello', 42]); // valid
|
|
191
|
+
* schema.validate(['hello', 42, true]); // valid
|
|
192
|
+
* schema.validate(['hello', 42, true, false]); // valid
|
|
193
|
+
* schema.validate(['hello', 42, 'extra']); // invalid — 'extra' not boolean
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
rest<TSchema extends SchemaBuilder<any, any, any, any, any>>(schema: TSchema): TupleSchemaBuilder<TElements, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TSchema> & TExtensions;
|
|
197
|
+
/**
|
|
198
|
+
* Removes the rest schema set by `rest()`. After this call, the tuple
|
|
199
|
+
* length must be exactly equal to the number of fixed element schemas.
|
|
200
|
+
*/
|
|
201
|
+
clearRest(): TupleSchemaBuilder<TElements, TRequired, TNullable, TExplicitType, THasDefault, TExtensions, undefined> & TExtensions;
|
|
202
|
+
/**
|
|
203
|
+
* @hidden
|
|
204
|
+
*/
|
|
205
|
+
nullable(): TupleSchemaBuilder<TElements, TRequired, true, TExplicitType, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
206
|
+
/**
|
|
207
|
+
* @hidden
|
|
208
|
+
*/
|
|
209
|
+
notNullable(): TupleSchemaBuilder<TElements, TRequired, false, TExplicitType, THasDefault, TExtensions, TRestSchema> & TExtensions;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Creates a fixed-length array schema (tuple) where each element at a
|
|
213
|
+
* specific index is validated against its own schema.
|
|
214
|
+
*
|
|
215
|
+
* @param elements Array of per-position schemas. The length of this array
|
|
216
|
+
* determines the required tuple length (unless `.rest()` is used).
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* import { tuple, string, number, boolean } from '@cleverbrush/schema';
|
|
221
|
+
*
|
|
222
|
+
* const schema = tuple([string(), number(), boolean()]);
|
|
223
|
+
* // Inferred TypeScript type: [string, number, boolean]
|
|
224
|
+
*
|
|
225
|
+
* schema.validate(['hello', 42, true]); // valid
|
|
226
|
+
* schema.validate(['hello', 42]); // invalid — too few elements
|
|
227
|
+
* schema.validate(['hello', 'oops', true]); // invalid — wrong type at [1]
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* // 2-D coordinate pair
|
|
233
|
+
* const point = tuple([number(), number()]);
|
|
234
|
+
* const result = point.validate([10.5, 20.3]);
|
|
235
|
+
* // result.valid === true
|
|
236
|
+
* // result.object === [10.5, 20.3]
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* // Optional tuple with default value
|
|
242
|
+
* const schema = tuple([string(), number()])
|
|
243
|
+
* .optional()
|
|
244
|
+
* .default(() => ['', 0]);
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export declare const tuple: <const TElements extends readonly SchemaBuilder<any, any, any, any, any>[]>(elements: [...TElements]) => TupleSchemaBuilder<TElements, true>;
|
|
248
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|