@cleverbrush/schema 1.1.10 → 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 +55 -15
- 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 +95 -20
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +135 -37
- 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 +58 -15
- 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,433 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
const parseFromJsonPreprocessor = (value) => {
|
|
4
|
-
if (typeof value === 'undefined')
|
|
5
|
-
return value;
|
|
6
|
-
if (typeof value === 'string') {
|
|
7
|
-
const time = Date.parse(value);
|
|
8
|
-
if (Number.isNaN(time))
|
|
9
|
-
return value;
|
|
10
|
-
return new Date(time);
|
|
11
|
-
}
|
|
12
|
-
return value;
|
|
13
|
-
};
|
|
14
|
-
const parseFromEpochPreprocessor = (value) => {
|
|
15
|
-
if (typeof value === 'undefined')
|
|
16
|
-
return value;
|
|
17
|
-
if (typeof value === 'number') {
|
|
18
|
-
const time = new Date(value);
|
|
19
|
-
if (Number.isNaN(time.getTime()))
|
|
20
|
-
return value;
|
|
21
|
-
return time;
|
|
22
|
-
}
|
|
23
|
-
return value;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Allows to create Date schema. It can be required or optional.
|
|
27
|
-
* It can be restricted to be: equal to a certain value, in future, in past, in a certain range.
|
|
28
|
-
* Supports parsing from JSON string and UNIX epoch (using preprocessors).
|
|
29
|
-
*
|
|
30
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
31
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
32
|
-
* directly. Use {@link date | date()} function instead.
|
|
33
|
-
*
|
|
34
|
-
* @example ```ts
|
|
35
|
-
* const date = new Date(2020, 0, 2);
|
|
36
|
-
* const schema = date().min(new Date(2020, 0, 1));
|
|
37
|
-
* const result = await schema.validate(date);
|
|
38
|
-
* // result.valid === true
|
|
39
|
-
* // result.object === date
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* @example ```ts
|
|
43
|
-
* const schema = date();
|
|
44
|
-
* const result = await schema.validate('2020-01-01');
|
|
45
|
-
* // result.valid === false
|
|
46
|
-
* // result.errors[0].message === 'is expected to be a date'
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @example ```ts
|
|
50
|
-
* const schema = date().parseFromJson();
|
|
51
|
-
* const result = await schema.validate('2020-01-01T00:00:00.000Z');
|
|
52
|
-
* // result.valid === true
|
|
53
|
-
* // result.object is equal to corresponding Date object
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* @example ```ts
|
|
57
|
-
* const schema = date().parseFromEpoch();
|
|
58
|
-
* const result = await schema.validate(1577836800000);
|
|
59
|
-
* // result.valid === true
|
|
60
|
-
* // result.object is equal to corresponding Date object
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* @see {@link date}
|
|
64
|
-
*/
|
|
65
|
-
export class DateSchemaBuilder extends SchemaBuilder {
|
|
66
|
-
#min;
|
|
67
|
-
#max;
|
|
68
|
-
#equalsTo;
|
|
69
|
-
#ensureIsInFuture = false;
|
|
70
|
-
#ensureIsInPast = false;
|
|
71
|
-
#parseFromJson = false;
|
|
72
|
-
#parseFromEpoch = false;
|
|
73
|
-
static create(props) {
|
|
74
|
-
return new DateSchemaBuilder({
|
|
75
|
-
type: 'date',
|
|
76
|
-
...props
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
constructor(props) {
|
|
80
|
-
super(props);
|
|
81
|
-
if (props.min instanceof Date) {
|
|
82
|
-
this.#min = props.min;
|
|
83
|
-
}
|
|
84
|
-
if (props.max instanceof Date) {
|
|
85
|
-
this.#max = props.max;
|
|
86
|
-
}
|
|
87
|
-
if (typeof props.ensureIsInFuture === 'boolean') {
|
|
88
|
-
this.#ensureIsInFuture = props.ensureIsInFuture;
|
|
89
|
-
}
|
|
90
|
-
if (typeof props.ensureIsInPast === 'boolean') {
|
|
91
|
-
this.#ensureIsInPast = props.ensureIsInPast;
|
|
92
|
-
}
|
|
93
|
-
if (typeof props.parseFromJson === 'boolean') {
|
|
94
|
-
this.#parseFromJson = props.parseFromJson;
|
|
95
|
-
}
|
|
96
|
-
if (typeof props.parseFromEpoch === 'boolean') {
|
|
97
|
-
this.#parseFromEpoch = props.parseFromEpoch;
|
|
98
|
-
}
|
|
99
|
-
if (props.equalsTo instanceof Date ||
|
|
100
|
-
typeof props.equalsTo === 'undefined') {
|
|
101
|
-
this.#equalsTo = props.equalsTo;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
introspect() {
|
|
105
|
-
return {
|
|
106
|
-
...super.introspect(),
|
|
107
|
-
/**
|
|
108
|
-
* Min valid value (if defined).
|
|
109
|
-
*/
|
|
110
|
-
min: this.#min,
|
|
111
|
-
/**
|
|
112
|
-
* Max valid value (if defined).
|
|
113
|
-
*/
|
|
114
|
-
max: this.#max,
|
|
115
|
-
/**
|
|
116
|
-
* Make sure that date is in future. `false` by default.
|
|
117
|
-
*/
|
|
118
|
-
ensureIsInFuture: this.#ensureIsInFuture,
|
|
119
|
-
/**
|
|
120
|
-
* Make sure that date is in past. `false` by default.
|
|
121
|
-
*/
|
|
122
|
-
ensureIsInPast: this.#ensureIsInPast,
|
|
123
|
-
/**
|
|
124
|
-
* If set, restrict date to be equal to a certain value.
|
|
125
|
-
*/
|
|
126
|
-
equalsTo: this.#equalsTo,
|
|
127
|
-
/**
|
|
128
|
-
* If set, schema will try to parse date from the UNIX epoch (number).
|
|
129
|
-
* `false` by default.
|
|
130
|
-
*/
|
|
131
|
-
parseFromEpoch: this.#parseFromEpoch,
|
|
132
|
-
/**
|
|
133
|
-
* If set, schema will try to parse date from JSON string.
|
|
134
|
-
* `false` by default.
|
|
135
|
-
*/
|
|
136
|
-
parseFromJson: this.#parseFromJson,
|
|
137
|
-
/**
|
|
138
|
-
* Array of preprocessor functions
|
|
139
|
-
*/
|
|
140
|
-
preprocessors: this.preprocessors,
|
|
141
|
-
/**
|
|
142
|
-
* Array of validator functions
|
|
143
|
-
*/
|
|
144
|
-
validators: this.validators
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* @hidden
|
|
149
|
-
*/
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
151
|
-
hasType(notUsed) {
|
|
152
|
-
return this.createFromProps({
|
|
153
|
-
...this.introspect()
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* @hidden
|
|
158
|
-
*/
|
|
159
|
-
clearHasType() {
|
|
160
|
-
return this.createFromProps({
|
|
161
|
-
...this.introspect()
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Performs validion of Date schema over `object`.
|
|
166
|
-
* @param context Optional `ValidationContext` settings.
|
|
167
|
-
*/
|
|
168
|
-
async validate(object, context) {
|
|
169
|
-
const superResult = await super.preValidate(object, context);
|
|
170
|
-
const { valid, context: prevalidationContext, errors } = superResult;
|
|
171
|
-
const { path } = prevalidationContext;
|
|
172
|
-
if (!valid) {
|
|
173
|
-
return {
|
|
174
|
-
valid,
|
|
175
|
-
errors
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
let { transaction: preValidationTransaction } = superResult;
|
|
179
|
-
let { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
180
|
-
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
181
|
-
this.isRequired === false) {
|
|
182
|
-
return {
|
|
183
|
-
valid: true,
|
|
184
|
-
object: objToValidate
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
if (this.#parseFromJson) {
|
|
188
|
-
preValidationTransaction = transaction({
|
|
189
|
-
validatedObject: parseFromJsonPreprocessor(objToValidate)
|
|
190
|
-
});
|
|
191
|
-
objToValidate = preValidationTransaction.object.validatedObject;
|
|
192
|
-
}
|
|
193
|
-
if (this.#parseFromEpoch) {
|
|
194
|
-
preValidationTransaction = transaction({
|
|
195
|
-
validatedObject: parseFromEpochPreprocessor(objToValidate)
|
|
196
|
-
});
|
|
197
|
-
objToValidate = preValidationTransaction.object.validatedObject;
|
|
198
|
-
}
|
|
199
|
-
if (!(objToValidate instanceof Date))
|
|
200
|
-
return {
|
|
201
|
-
valid: false,
|
|
202
|
-
errors: [
|
|
203
|
-
{
|
|
204
|
-
message: `expected instance of Date, but saw ${typeof objToValidate}`,
|
|
205
|
-
path: path
|
|
206
|
-
}
|
|
207
|
-
]
|
|
208
|
-
};
|
|
209
|
-
if (typeof this.#equalsTo !== 'undefined' &&
|
|
210
|
-
objToValidate.getTime() !== this.#equalsTo.getTime()) {
|
|
211
|
-
return {
|
|
212
|
-
valid: false,
|
|
213
|
-
errors: [
|
|
214
|
-
{
|
|
215
|
-
message: `is expected to be equal to ${this.#equalsTo}`,
|
|
216
|
-
path: path
|
|
217
|
-
}
|
|
218
|
-
]
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
if (this.#ensureIsInFuture && objToValidate <= new Date()) {
|
|
222
|
-
return {
|
|
223
|
-
valid: false,
|
|
224
|
-
errors: [
|
|
225
|
-
{
|
|
226
|
-
message: 'is expected to be in future',
|
|
227
|
-
path: path
|
|
228
|
-
}
|
|
229
|
-
]
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
if (this.#ensureIsInPast && objToValidate >= new Date()) {
|
|
233
|
-
return {
|
|
234
|
-
valid: false,
|
|
235
|
-
errors: [
|
|
236
|
-
{
|
|
237
|
-
message: 'is expected to be in past',
|
|
238
|
-
path: path
|
|
239
|
-
}
|
|
240
|
-
]
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
if (typeof this.#min !== 'undefined') {
|
|
244
|
-
if (objToValidate < this.#min)
|
|
245
|
-
return {
|
|
246
|
-
valid: false,
|
|
247
|
-
errors: [
|
|
248
|
-
{
|
|
249
|
-
message: `expected to be at least ${this.#min}`,
|
|
250
|
-
path: path
|
|
251
|
-
}
|
|
252
|
-
]
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
if (typeof this.#max !== 'undefined') {
|
|
256
|
-
if (objToValidate > this.#max)
|
|
257
|
-
return {
|
|
258
|
-
valid: false,
|
|
259
|
-
errors: [
|
|
260
|
-
{
|
|
261
|
-
message: `expected to be no more than or equal to ${this.#max}`,
|
|
262
|
-
path: path
|
|
263
|
-
}
|
|
264
|
-
]
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
return {
|
|
268
|
-
valid: true,
|
|
269
|
-
object: preValidationTransaction.commit()
|
|
270
|
-
.validatedObject
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* @hidden
|
|
275
|
-
*/
|
|
276
|
-
createFromProps(props) {
|
|
277
|
-
return DateSchemaBuilder.create(props);
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Restricts Date to be equal to `value`.
|
|
281
|
-
*/
|
|
282
|
-
equals(value) {
|
|
283
|
-
if (!(value instanceof Date))
|
|
284
|
-
throw new Error('Date expected');
|
|
285
|
-
return this.createFromProps({
|
|
286
|
-
...this.introspect(),
|
|
287
|
-
equalsTo: value
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Clears `equals()` call.
|
|
292
|
-
*/
|
|
293
|
-
clearEquals() {
|
|
294
|
-
return this.createFromProps({
|
|
295
|
-
...this.introspect(),
|
|
296
|
-
equalsTo: undefined
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* @hidden
|
|
301
|
-
*/
|
|
302
|
-
required() {
|
|
303
|
-
return super.required();
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* @hidden
|
|
307
|
-
*/
|
|
308
|
-
optional() {
|
|
309
|
-
return super.optional();
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Accept only dates in the future.
|
|
313
|
-
*/
|
|
314
|
-
isInFuture() {
|
|
315
|
-
return this.createFromProps({
|
|
316
|
-
...this.introspect(),
|
|
317
|
-
ensureIsInFuture: true
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Cancel `isInFuture()` call.
|
|
322
|
-
*/
|
|
323
|
-
clearIsInFuture() {
|
|
324
|
-
return this.createFromProps({
|
|
325
|
-
...this.introspect(),
|
|
326
|
-
ensureIsInFuture: false
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Accept only dates in the past.
|
|
331
|
-
*/
|
|
332
|
-
isInPast() {
|
|
333
|
-
return this.createFromProps({
|
|
334
|
-
...this.introspect(),
|
|
335
|
-
ensureIsInPast: true
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Cancel `isInPast()` call.
|
|
340
|
-
*/
|
|
341
|
-
clearIsInPast() {
|
|
342
|
-
return this.createFromProps({
|
|
343
|
-
...this.introspect(),
|
|
344
|
-
ensureIsInPast: false
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Set minimal valid Date value for schema.
|
|
349
|
-
*/
|
|
350
|
-
min(minValue) {
|
|
351
|
-
if (!(minValue instanceof Date))
|
|
352
|
-
throw new Error('minValue must be a Date');
|
|
353
|
-
return this.createFromProps({
|
|
354
|
-
...this.introspect(),
|
|
355
|
-
min: minValue
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Clear `min()` call.
|
|
360
|
-
*/
|
|
361
|
-
clearMin() {
|
|
362
|
-
const schema = this.introspect();
|
|
363
|
-
delete schema.min;
|
|
364
|
-
return this.createFromProps({
|
|
365
|
-
...schema
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Set maximal valid Date value for schema.
|
|
370
|
-
*/
|
|
371
|
-
max(maxValue) {
|
|
372
|
-
if (!(maxValue instanceof Date))
|
|
373
|
-
throw new Error('maxValue must be a Date');
|
|
374
|
-
return this.createFromProps({
|
|
375
|
-
...this.introspect(),
|
|
376
|
-
max: maxValue
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* Clear `max()` call.
|
|
381
|
-
*/
|
|
382
|
-
clearMax() {
|
|
383
|
-
const schema = this.introspect();
|
|
384
|
-
delete schema.max;
|
|
385
|
-
return this.createFromProps({
|
|
386
|
-
...schema
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Accepts JSON string as a valid Date.
|
|
391
|
-
* String must be in ISO format and will be parsed using `JSON.parse()`.
|
|
392
|
-
*/
|
|
393
|
-
acceptJsonString() {
|
|
394
|
-
return this.createFromProps({
|
|
395
|
-
...this.introspect(),
|
|
396
|
-
parseFromJson: true
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Cancel `acceptJsonString()` call.
|
|
401
|
-
*/
|
|
402
|
-
doNotAcceptJsonString() {
|
|
403
|
-
return this.createFromProps({
|
|
404
|
-
...this.introspect(),
|
|
405
|
-
parseFromJson: false
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Accepts epoch number as a valid Date.
|
|
410
|
-
* Epoch number will be parsed using `new Date(epoch)`.
|
|
411
|
-
*/
|
|
412
|
-
acceptEpoch() {
|
|
413
|
-
return this.createFromProps({
|
|
414
|
-
...this.introspect(),
|
|
415
|
-
parseFromEpoch: true
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* Cancel `acceptEpoch()` call.
|
|
420
|
-
*/
|
|
421
|
-
doNotAcceptEpoch() {
|
|
422
|
-
return this.createFromProps({
|
|
423
|
-
...this.introspect(),
|
|
424
|
-
parseFromEpoch: false
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Creates a Date schema.
|
|
430
|
-
*/
|
|
431
|
-
export const date = () => DateSchemaBuilder.create({
|
|
432
|
-
isRequired: true
|
|
433
|
-
});
|
|
1
|
+
import{a,b}from"../chunk-YWBNVHR6.js";import"../chunk-CTP4RHDG.js";export{a as DateSchemaBuilder,b as date};
|
|
2
|
+
//# sourceMappingURL=DateSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { type BRAND, SchemaBuilder, SYMBOL_HAS_PROPERTIES, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
3
|
+
type ExternSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<ExternSchemaBuilder<any, R>['introspect']>> & {
|
|
4
|
+
standardSchema: StandardSchemaV1;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Schema builder that wraps an external
|
|
8
|
+
* [Standard Schema v1](https://standardschema.dev/) compatible schema
|
|
9
|
+
* (e.g. Zod, Valibot, ArkType) into a `@cleverbrush/schema` builder.
|
|
10
|
+
*
|
|
11
|
+
* This enables cross-library schema composition — you can use a Zod schema
|
|
12
|
+
* as a property inside a `@cleverbrush/schema` object schema, and the
|
|
13
|
+
* inferred TypeScript type will be correct.
|
|
14
|
+
*
|
|
15
|
+
* Validation is delegated entirely to the external schema's
|
|
16
|
+
* `['~standard'].validate()` method. Standard Schema issues are mapped to
|
|
17
|
+
* `@cleverbrush/schema` `ValidationError` objects, with any issue paths
|
|
18
|
+
* formatted as dotted prefixes (e.g. `"address.city: must be a string"`).
|
|
19
|
+
*
|
|
20
|
+
* When used inside an `object()` schema, the property descriptor tree is
|
|
21
|
+
* built dynamically (via Proxy) from the external schema's output type,
|
|
22
|
+
* so `getErrorsFor(t => t.order.id)` works without any additional
|
|
23
|
+
* configuration.
|
|
24
|
+
*
|
|
25
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
26
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
27
|
+
* directly. Use the {@link extern | extern()} factory function instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { z } from 'zod';
|
|
32
|
+
* import { object, date, extern, InferType } from '@cleverbrush/schema';
|
|
33
|
+
*
|
|
34
|
+
* const zodUser = z.object({ first: z.string(), last: z.string() });
|
|
35
|
+
*
|
|
36
|
+
* const order = object({
|
|
37
|
+
* user: extern(zodUser),
|
|
38
|
+
* date: date(),
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* type Order = InferType<typeof order>;
|
|
42
|
+
* // { user: { first: string; last: string }; date: Date }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @see {@link extern}
|
|
46
|
+
*/
|
|
47
|
+
export declare class ExternSchemaBuilder<TStandardSchema extends StandardSchemaV1 = StandardSchemaV1, TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TResult = TExplicitType extends undefined ? StandardSchemaV1.InferOutput<TStandardSchema> : TExplicitType> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
48
|
+
#private;
|
|
49
|
+
/**
|
|
50
|
+
* Always `true` for extern schemas — enables Proxy-based property
|
|
51
|
+
* descriptor trees and nested error propagation in
|
|
52
|
+
* `ObjectSchemaBuilder`.
|
|
53
|
+
*/
|
|
54
|
+
readonly [SYMBOL_HAS_PROPERTIES] = true;
|
|
55
|
+
/**
|
|
56
|
+
* @hidden
|
|
57
|
+
*/
|
|
58
|
+
static create(props: ExternSchemaBuilderCreateProps<any>): ExternSchemaBuilder<StandardSchemaV1<unknown, unknown>, true, false, undefined, false, {}, unknown>;
|
|
59
|
+
protected constructor(props: ExternSchemaBuilderCreateProps<TRequired>);
|
|
60
|
+
/**
|
|
61
|
+
* Returns the wrapped Standard Schema instance.
|
|
62
|
+
*/
|
|
63
|
+
get standardSchema(): TStandardSchema;
|
|
64
|
+
/**
|
|
65
|
+
* @inheritdoc
|
|
66
|
+
*/
|
|
67
|
+
hasType<T>(_notUsed?: T): ExternSchemaBuilder<TStandardSchema, true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
68
|
+
/**
|
|
69
|
+
* @inheritdoc
|
|
70
|
+
*/
|
|
71
|
+
clearHasType(): ExternSchemaBuilder<TStandardSchema, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
72
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
73
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
74
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
75
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
76
|
+
/**
|
|
77
|
+
* Performs synchronous validation by delegating to the external
|
|
78
|
+
* Standard Schema's `validate()` method.
|
|
79
|
+
*
|
|
80
|
+
* If the external schema returns a `Promise` (async validation),
|
|
81
|
+
* this method throws — use {@link validateAsync} instead.
|
|
82
|
+
*/
|
|
83
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Performs async validation by delegating to the external
|
|
86
|
+
* Standard Schema's `validate()` method. Supports external schemas
|
|
87
|
+
* that return a `Promise` from their `validate()`.
|
|
88
|
+
*/
|
|
89
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
90
|
+
protected createFromProps<TReq extends boolean>(props: ExternSchemaBuilderCreateProps<TReq>): this;
|
|
91
|
+
/**
|
|
92
|
+
* Returns a snapshot of the builder's internal state.
|
|
93
|
+
* Includes the wrapped `standardSchema` reference.
|
|
94
|
+
*/
|
|
95
|
+
introspect(): {
|
|
96
|
+
standardSchema: TStandardSchema;
|
|
97
|
+
type: string;
|
|
98
|
+
isRequired: boolean;
|
|
99
|
+
isNullable: boolean;
|
|
100
|
+
isReadonly: boolean;
|
|
101
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TResult>[];
|
|
102
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TResult>[];
|
|
103
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
104
|
+
extensions: {
|
|
105
|
+
[x: string]: unknown;
|
|
106
|
+
};
|
|
107
|
+
hasDefault: boolean;
|
|
108
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
109
|
+
description: string | undefined;
|
|
110
|
+
hasCatch: boolean;
|
|
111
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* @hidden
|
|
115
|
+
*/
|
|
116
|
+
required(errorMessage?: ValidationErrorMessageProvider): ExternSchemaBuilder<TStandardSchema, true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
117
|
+
/**
|
|
118
|
+
* @hidden
|
|
119
|
+
*/
|
|
120
|
+
optional(): ExternSchemaBuilder<TStandardSchema, false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
121
|
+
/**
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
default(value: TResult | (() => TResult)): ExternSchemaBuilder<TStandardSchema, true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
125
|
+
/**
|
|
126
|
+
* @hidden
|
|
127
|
+
*/
|
|
128
|
+
clearDefault(): ExternSchemaBuilder<TStandardSchema, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
129
|
+
/**
|
|
130
|
+
* @hidden
|
|
131
|
+
*/
|
|
132
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): ExternSchemaBuilder<TStandardSchema, TRequired, TNullable, TResult & {
|
|
133
|
+
readonly [K in BRAND]: TBrand;
|
|
134
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
135
|
+
/**
|
|
136
|
+
* Marks the inferred type as `Readonly<T>`. Sets the `isReadonly`
|
|
137
|
+
* introspection flag for tooling consistency.
|
|
138
|
+
*
|
|
139
|
+
* @see {@link SchemaBuilder.readonly}
|
|
140
|
+
*/
|
|
141
|
+
readonly(): ExternSchemaBuilder<TStandardSchema, TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions> & TExtensions;
|
|
142
|
+
/**
|
|
143
|
+
* @hidden
|
|
144
|
+
*/
|
|
145
|
+
nullable(): ExternSchemaBuilder<TStandardSchema, TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
146
|
+
/**
|
|
147
|
+
* @hidden
|
|
148
|
+
*/
|
|
149
|
+
notNullable(): ExternSchemaBuilder<TStandardSchema, TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Wraps an external [Standard Schema v1](https://standardschema.dev/)
|
|
153
|
+
* compatible schema into a `@cleverbrush/schema` builder.
|
|
154
|
+
*
|
|
155
|
+
* This enables cross-library schema composition — use schemas from Zod,
|
|
156
|
+
* Valibot, ArkType, or any Standard Schema v1 compliant library as
|
|
157
|
+
* properties inside `@cleverbrush/schema` object schemas with full type
|
|
158
|
+
* inference.
|
|
159
|
+
*
|
|
160
|
+
* @param standardSchema - A Standard Schema v1 compliant schema instance
|
|
161
|
+
* (any object that exposes a `['~standard']` property with `version: 1`
|
|
162
|
+
* and a `validate` function).
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { z } from 'zod';
|
|
167
|
+
* import { object, date, extern, InferType } from '@cleverbrush/schema';
|
|
168
|
+
*
|
|
169
|
+
* const zodUser = z.object({ first: z.string(), last: z.string() });
|
|
170
|
+
*
|
|
171
|
+
* const order = object({
|
|
172
|
+
* user: extern(zodUser),
|
|
173
|
+
* date: date(),
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* type Order = InferType<typeof order>;
|
|
177
|
+
* // { user: { first: string; last: string }; date: Date }
|
|
178
|
+
*
|
|
179
|
+
* order.validate({
|
|
180
|
+
* user: { first: 'Alice', last: 'Smith' },
|
|
181
|
+
* date: new Date(),
|
|
182
|
+
* }); // { valid: true, object: { user: …, date: … } }
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* // Optional external schema
|
|
188
|
+
* const schema = extern(zodUser).optional();
|
|
189
|
+
* schema.validate(undefined); // valid
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* // Inside an array
|
|
195
|
+
* import { array, extern } from '@cleverbrush/schema';
|
|
196
|
+
* const users = array(extern(zodUser));
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export declare const extern: <T extends StandardSchemaV1>(standardSchema: T) => ExternSchemaBuilder<T, true>;
|
|
200
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|