@cleverbrush/schema 1.1.11 → 3.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.
Files changed (98) hide show
  1. package/README.md +1721 -104
  2. package/dist/builders/AnySchemaBuilder.d.ts +54 -14
  3. package/dist/builders/AnySchemaBuilder.js +2 -112
  4. package/dist/builders/AnySchemaBuilder.js.map +1 -0
  5. package/dist/builders/ArraySchemaBuilder.d.ts +126 -23
  6. package/dist/builders/ArraySchemaBuilder.js +2 -284
  7. package/dist/builders/ArraySchemaBuilder.js.map +1 -0
  8. package/dist/builders/BooleanSchemaBuilder.d.ts +109 -19
  9. package/dist/builders/BooleanSchemaBuilder.js +2 -150
  10. package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
  11. package/dist/builders/DateSchemaBuilder.d.ts +154 -34
  12. package/dist/builders/DateSchemaBuilder.js +2 -433
  13. package/dist/builders/DateSchemaBuilder.js.map +1 -0
  14. package/dist/builders/ExternSchemaBuilder.d.ts +202 -0
  15. package/dist/builders/ExternSchemaBuilder.js +2 -0
  16. package/dist/builders/ExternSchemaBuilder.js.map +1 -0
  17. package/dist/builders/FunctionSchemaBuilder.d.ts +205 -18
  18. package/dist/builders/FunctionSchemaBuilder.js +2 -113
  19. package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
  20. package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
  21. package/dist/builders/LazySchemaBuilder.d.ts +169 -0
  22. package/dist/builders/NullSchemaBuilder.d.ts +162 -0
  23. package/dist/builders/NumberSchemaBuilder.d.ts +159 -31
  24. package/dist/builders/NumberSchemaBuilder.js +2 -386
  25. package/dist/builders/NumberSchemaBuilder.js.map +1 -0
  26. package/dist/builders/ObjectSchemaBuilder.d.ts +486 -61
  27. package/dist/builders/ObjectSchemaBuilder.js +2 -589
  28. package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
  29. package/dist/builders/ParseStringSchemaBuilder.d.ts +204 -0
  30. package/dist/builders/ParseStringSchemaBuilder.js +2 -0
  31. package/dist/builders/ParseStringSchemaBuilder.js.map +1 -0
  32. package/dist/builders/PromiseSchemaBuilder.d.ts +213 -0
  33. package/dist/builders/PromiseSchemaBuilder.js +2 -0
  34. package/dist/builders/PromiseSchemaBuilder.js.map +1 -0
  35. package/dist/builders/PropertyValidationResult.d.ts +68 -0
  36. package/dist/builders/RecordSchemaBuilder.d.ts +343 -0
  37. package/dist/builders/RecordSchemaBuilder.js +2 -0
  38. package/dist/builders/RecordSchemaBuilder.js.map +1 -0
  39. package/dist/builders/SchemaBuilder.d.ts +907 -30
  40. package/dist/builders/StringSchemaBuilder.d.ts +154 -37
  41. package/dist/builders/StringSchemaBuilder.js +2 -414
  42. package/dist/builders/StringSchemaBuilder.js.map +1 -0
  43. package/dist/builders/TupleSchemaBuilder.d.ts +250 -0
  44. package/dist/builders/TupleSchemaBuilder.js +2 -0
  45. package/dist/builders/TupleSchemaBuilder.js.map +1 -0
  46. package/dist/builders/UnionSchemaBuilder.d.ts +141 -39
  47. package/dist/builders/UnionSchemaBuilder.js +2 -216
  48. package/dist/builders/UnionSchemaBuilder.js.map +1 -0
  49. package/dist/chunk-3JMDGYDT.js +2 -0
  50. package/dist/chunk-3JMDGYDT.js.map +1 -0
  51. package/dist/chunk-BUEVZ3KA.js +2 -0
  52. package/dist/chunk-BUEVZ3KA.js.map +1 -0
  53. package/dist/chunk-CFIJQ4GP.js +2 -0
  54. package/dist/chunk-CFIJQ4GP.js.map +1 -0
  55. package/dist/chunk-DY7J6RNN.js +2 -0
  56. package/dist/chunk-DY7J6RNN.js.map +1 -0
  57. package/dist/chunk-EIVZX4ZO.js +2 -0
  58. package/dist/chunk-EIVZX4ZO.js.map +1 -0
  59. package/dist/chunk-GXPV6UQK.js +2 -0
  60. package/dist/chunk-GXPV6UQK.js.map +1 -0
  61. package/dist/chunk-HN774HD7.js +2 -0
  62. package/dist/chunk-HN774HD7.js.map +1 -0
  63. package/dist/chunk-K6Z47OQY.js +2 -0
  64. package/dist/chunk-K6Z47OQY.js.map +1 -0
  65. package/dist/chunk-NUW3VXZV.js +2 -0
  66. package/dist/chunk-NUW3VXZV.js.map +1 -0
  67. package/dist/chunk-PHE4LIAN.js +2 -0
  68. package/dist/chunk-PHE4LIAN.js.map +1 -0
  69. package/dist/chunk-QARCEYGO.js +2 -0
  70. package/dist/chunk-QARCEYGO.js.map +1 -0
  71. package/dist/chunk-WDMJBGBD.js +2 -0
  72. package/dist/chunk-WDMJBGBD.js.map +1 -0
  73. package/dist/chunk-WQDYWDOE.js +2 -0
  74. package/dist/chunk-WQDYWDOE.js.map +1 -0
  75. package/dist/chunk-YQZHDMRF.js +2 -0
  76. package/dist/chunk-YQZHDMRF.js.map +1 -0
  77. package/dist/chunk-ZC6YBKCP.js +2 -0
  78. package/dist/chunk-ZC6YBKCP.js.map +1 -0
  79. package/dist/chunk-ZFI27R3L.js +2 -0
  80. package/dist/chunk-ZFI27R3L.js.map +1 -0
  81. package/dist/core.d.ts +28 -0
  82. package/dist/core.js +2 -0
  83. package/dist/core.js.map +1 -0
  84. package/dist/extension.d.ts +421 -0
  85. package/dist/extensions/array.d.ts +112 -0
  86. package/dist/extensions/enum.d.ts +190 -0
  87. package/dist/extensions/index.d.ts +112 -0
  88. package/dist/extensions/nullable.d.ts +26 -0
  89. package/dist/extensions/number.d.ts +228 -0
  90. package/dist/extensions/string.d.ts +332 -0
  91. package/dist/extensions/util.d.ts +45 -0
  92. package/dist/index.d.ts +10 -20
  93. package/dist/index.js +2 -19
  94. package/dist/index.js.map +1 -0
  95. package/dist/utils/transaction.d.ts +27 -4
  96. package/package.json +83 -7
  97. package/dist/builders/SchemaBuilder.js +0 -275
  98. package/dist/utils/transaction.js +0 -178
@@ -1,386 +1,2 @@
1
- import { SchemaBuilder } from './SchemaBuilder.js';
2
- /**
3
- * Number schema builder class. Allows to create Number schemas.
4
- * Can be required or optional, can be restricted to be equal to a certain value,
5
- * can be restricted to be in a certain range, can be restricted to be integer.
6
- *
7
- * **NOTE** this class is exported only to give opportunity to extend it
8
- * by inheriting. It is not recommended to create an instance of this class
9
- * directly. Use {@link number | number()} function instead.
10
- *
11
- * @example ```ts
12
- * const schema = number().equals(42);
13
- * const result = await schema.validate(42);
14
- * // result.valid === true
15
- * // result.object === 42
16
- * ```
17
- * @example ```ts
18
- * const schema = number();
19
- * const result = await schema.validate('42');
20
- * // result.valid === false
21
- * // result.errors[0].message === 'is expected to be a number'
22
- * ```
23
- * @example ```ts
24
- * const schema = number().min(0).max(100);
25
- * const result = await schema.validate(42);
26
- * // result.valid === true
27
- * // result.object === 42
28
- * ```
29
- *
30
- * @example ```ts
31
- * const schema = number().min(0).max(100);
32
- * const result = await schema.validate(142.5);
33
- * // result.valid === false
34
- * // result.errors[0].message === 'is expected to be less than or equal to 100'
35
- * ```
36
- *
37
- * @see {@link number}
38
- */
39
- export class NumberSchemaBuilder extends SchemaBuilder {
40
- #min;
41
- #max;
42
- #equalsTo;
43
- #ensureNotNaN = true;
44
- #ensureIsFinite = true;
45
- #isInteger = true;
46
- static create(props) {
47
- return new NumberSchemaBuilder({
48
- type: 'number',
49
- ...props
50
- });
51
- }
52
- constructor(props) {
53
- super(props);
54
- if (typeof props.min === 'number') {
55
- this.#min = props.min;
56
- }
57
- if (typeof props.max === 'number') {
58
- this.#max = props.max;
59
- }
60
- if (typeof props.ensureNotNaN === 'boolean') {
61
- this.#ensureNotNaN = props.ensureNotNaN;
62
- }
63
- if (typeof props.ensureIsFinite === 'boolean') {
64
- this.#ensureIsFinite = props.ensureIsFinite;
65
- }
66
- if (typeof props.isInteger === 'boolean') {
67
- this.#isInteger = props.isInteger;
68
- }
69
- if (typeof props.equalsTo === 'number' ||
70
- typeof props.equalsTo === 'undefined') {
71
- this.#equalsTo = props.equalsTo;
72
- }
73
- }
74
- introspect() {
75
- return {
76
- ...super.introspect(),
77
- /**
78
- * Min valid value (if defined).
79
- */
80
- min: this.#min,
81
- /**
82
- * Max valid value (if defined).
83
- */
84
- max: this.#max,
85
- /**
86
- * Make sure that object is not `NaN`. `true` by default.
87
- */
88
- ensureNotNaN: this.#ensureNotNaN,
89
- /**
90
- * Make sure that object is not different kinds of `infinity`. `true` by default.
91
- */
92
- ensureIsFinite: this.#ensureIsFinite,
93
- /**
94
- * If set, restrict object to be equal to a certain value.
95
- */
96
- equalsTo: this.#equalsTo,
97
- /**
98
- * Allow only integer values (floating point values will be rejected
99
- * as invalid)
100
- */
101
- isInteger: this.#isInteger,
102
- /**
103
- * Array of preprocessor functions
104
- */
105
- preprocessors: this.preprocessors,
106
- /**
107
- * Array of validator functions
108
- */
109
- validators: this.validators
110
- };
111
- }
112
- /**
113
- * @hidden
114
- */
115
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
- hasType(notUsed) {
117
- return this.createFromProps({
118
- ...this.introspect()
119
- });
120
- }
121
- /**
122
- * @hidden
123
- */
124
- clearHasType() {
125
- return this.createFromProps({
126
- ...this.introspect()
127
- });
128
- }
129
- /**
130
- * Performs validion of number schema over `object`.
131
- * @param context Optional `ValidationContext` settings.
132
- */
133
- async validate(object, context) {
134
- const superResult = await super.preValidate(object, context);
135
- const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
136
- const { path } = prevalidationContext;
137
- if (!valid) {
138
- return {
139
- valid,
140
- errors
141
- };
142
- }
143
- const { object: { validatedObject: objToValidate } } = preValidationTransaction;
144
- if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
145
- this.isRequired === false) {
146
- return {
147
- valid: true,
148
- object: objToValidate
149
- };
150
- }
151
- if (typeof objToValidate !== 'number')
152
- return {
153
- valid: false,
154
- errors: [
155
- {
156
- message: `expected type number, but saw ${typeof objToValidate}`,
157
- path: path
158
- }
159
- ]
160
- };
161
- if (typeof this.#equalsTo !== 'undefined' &&
162
- objToValidate !== this.#equalsTo) {
163
- return {
164
- valid: false,
165
- errors: [
166
- {
167
- message: `is expected to be equal to ${this.#equalsTo}`,
168
- path: path
169
- }
170
- ]
171
- };
172
- }
173
- if (this.#ensureNotNaN && Number.isNaN(objToValidate)) {
174
- return {
175
- valid: false,
176
- errors: [
177
- {
178
- message: 'is not expected to be NaN',
179
- path: path
180
- }
181
- ]
182
- };
183
- }
184
- if (this.#ensureIsFinite &&
185
- !Number.isFinite(objToValidate) &&
186
- this.#ensureNotNaN &&
187
- !Number.isNaN(objToValidate)) {
188
- return {
189
- valid: false,
190
- errors: [
191
- {
192
- message: 'is expected to be a finite number',
193
- path: path
194
- }
195
- ]
196
- };
197
- }
198
- if (this.#isInteger &&
199
- !Number.isNaN(objToValidate) &&
200
- Number.isFinite(objToValidate) &&
201
- !Number.isInteger(objToValidate)) {
202
- return {
203
- valid: false,
204
- errors: [
205
- {
206
- message: `is expected to be integer`,
207
- path: path
208
- }
209
- ]
210
- };
211
- }
212
- if (typeof this.#min !== 'undefined') {
213
- if (objToValidate < this.#min)
214
- return {
215
- valid: false,
216
- errors: [
217
- {
218
- message: `expected to be at least ${this.#min}`,
219
- path: path
220
- }
221
- ]
222
- };
223
- }
224
- if (typeof this.#max !== 'undefined') {
225
- if (objToValidate > this.#max)
226
- return {
227
- valid: false,
228
- errors: [
229
- {
230
- message: `expected to be no more than or equal to ${this.#max}`,
231
- path: path
232
- }
233
- ]
234
- };
235
- }
236
- return {
237
- valid: true,
238
- object: preValidationTransaction.commit().validatedObject
239
- };
240
- }
241
- createFromProps(props) {
242
- return NumberSchemaBuilder.create(props);
243
- }
244
- /**
245
- * Restricts number to be equal to `value`.
246
- */
247
- equals(value) {
248
- if (typeof value !== 'number')
249
- throw new Error('number expected');
250
- return this.createFromProps({
251
- ...this.introspect(),
252
- equalsTo: value
253
- });
254
- }
255
- /**
256
- * Clear `equals()` call.
257
- */
258
- clearEquals() {
259
- return this.createFromProps({
260
- ...this.introspect(),
261
- equalsTo: undefined
262
- });
263
- }
264
- /**
265
- * Float values will be considered as valid after this call.
266
- */
267
- isFloat() {
268
- return this.createFromProps({
269
- ...this.introspect(),
270
- isInteger: false
271
- });
272
- }
273
- /**
274
- * Only integer values will be considered as valid after this call.
275
- */
276
- isInteger() {
277
- return this.createFromProps({
278
- ...this.introspect(),
279
- isInteger: true
280
- });
281
- }
282
- /**
283
- * @hidden
284
- */
285
- required() {
286
- return super.required();
287
- }
288
- /**
289
- * @hidden
290
- */
291
- optional() {
292
- return super.optional();
293
- }
294
- /**
295
- * Do not accept NaN value
296
- */
297
- notNaN() {
298
- return this.createFromProps({
299
- ...this.introspect(),
300
- ensureNotNaN: true
301
- });
302
- }
303
- /**
304
- * Consider NaN value as valid
305
- */
306
- canBeNaN() {
307
- return this.createFromProps({
308
- ...this.introspect(),
309
- ensureNotNaN: false
310
- });
311
- }
312
- /**
313
- * Do not accept `Infinity`.
314
- */
315
- isFinite() {
316
- return this.createFromProps({
317
- ...this.introspect(),
318
- ensureIsFinite: true
319
- });
320
- }
321
- /**
322
- * Consider `Infinity` as valid.
323
- */
324
- canBeInfinite() {
325
- return this.createFromProps({
326
- ...this.introspect(),
327
- ensureIsFinite: false
328
- });
329
- }
330
- /**
331
- * Restrict number to be at least `minValue`.
332
- */
333
- min(minValue) {
334
- if (typeof minValue !== 'number')
335
- throw new Error('minValue must be a number');
336
- return this.createFromProps({
337
- ...this.introspect(),
338
- min: minValue
339
- });
340
- }
341
- /**
342
- * Clear `min()` call.
343
- */
344
- clearMin() {
345
- const schema = this.introspect();
346
- delete schema.min;
347
- return this.createFromProps({
348
- ...schema
349
- });
350
- }
351
- /**
352
- * Restrict number to be no more than `maxValue`.
353
- */
354
- max(maxValue) {
355
- if (typeof maxValue !== 'number')
356
- throw new Error('maxValue must be a number');
357
- return this.createFromProps({
358
- ...this.introspect(),
359
- max: maxValue
360
- });
361
- }
362
- /**
363
- * Clear `max()` call.
364
- */
365
- clearMax() {
366
- const schema = this.introspect();
367
- delete schema.max;
368
- return this.createFromProps({
369
- ...schema
370
- });
371
- }
372
- }
373
- /**
374
- * Creates a number schema.
375
- */
376
- export function number(equals) {
377
- if (typeof equals === 'number') {
378
- return NumberSchemaBuilder.create({
379
- isRequired: true,
380
- equalsTo: equals
381
- });
382
- }
383
- return NumberSchemaBuilder.create({
384
- isRequired: true
385
- });
386
- }
1
+ import{a,b}from"../chunk-BUEVZ3KA.js";import"../chunk-3JMDGYDT.js";export{a as NumberSchemaBuilder,b as number};
2
+ //# sourceMappingURL=NumberSchemaBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}