@cleverbrush/schema 1.0.0-beta.5 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -320
- package/dist/builders/AnySchemaBuilder.d.ts +76 -0
- package/dist/builders/AnySchemaBuilder.js +112 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
- package/dist/builders/ArraySchemaBuilder.js +254 -111
- package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
- package/dist/builders/BooleanSchemaBuilder.js +134 -74
- package/dist/builders/DateSchemaBuilder.d.ts +177 -0
- package/dist/builders/DateSchemaBuilder.js +433 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +59 -25
- package/dist/builders/FunctionSchemaBuilder.js +102 -58
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
- package/dist/builders/NumberSchemaBuilder.js +345 -165
- package/dist/builders/ObjectSchemaBuilder.d.ts +357 -81
- package/dist/builders/ObjectSchemaBuilder.js +519 -283
- package/dist/builders/SchemaBuilder.d.ts +158 -34
- package/dist/builders/SchemaBuilder.js +258 -71
- package/dist/builders/StringSchemaBuilder.d.ts +138 -13
- package/dist/builders/StringSchemaBuilder.js +261 -115
- package/dist/builders/UnionSchemaBuilder.d.ts +129 -9
- package/dist/builders/UnionSchemaBuilder.js +196 -75
- package/dist/index.d.ts +20 -40
- package/dist/index.js +19 -32
- package/dist/utils/transaction.d.ts +46 -0
- package/dist/utils/transaction.js +178 -0
- package/package.json +27 -14
- package/dist/defaultSchemas.d.ts +0 -4
- package/dist/defaultSchemas.js +0 -45
- package/dist/schema.d.ts +0 -231
- package/dist/schema.js +0 -2
- package/dist/schemaRegistry.d.ts +0 -49
- package/dist/schemaRegistry.js +0 -278
- package/dist/validators/validateArray.d.ts +0 -3
- package/dist/validators/validateArray.js +0 -60
- package/dist/validators/validateBoolean.d.ts +0 -2
- package/dist/validators/validateBoolean.js +0 -30
- package/dist/validators/validateFunction.d.ts +0 -2
- package/dist/validators/validateFunction.js +0 -21
- package/dist/validators/validateNumber.d.ts +0 -2
- package/dist/validators/validateNumber.js +0 -69
- package/dist/validators/validateObject.d.ts +0 -3
- package/dist/validators/validateObject.js +0 -95
- package/dist/validators/validateString.d.ts +0 -2
- package/dist/validators/validateString.js +0 -50
- package/dist/validators/validateUnion.d.ts +0 -3
- package/dist/validators/validateUnion.js +0 -30
- package/src/builders/ArraySchemaBuilder.test.ts +0 -270
- package/src/builders/ArraySchemaBuilder.ts +0 -381
- package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
- package/src/builders/BooleanSchemaBuilder.ts +0 -155
- package/src/builders/FunctionSchemaBuilder.test.ts +0 -145
- package/src/builders/FunctionSchemaBuilder.ts +0 -117
- package/src/builders/NumberSchemaBuilder.test.ts +0 -493
- package/src/builders/NumberSchemaBuilder.ts +0 -789
- package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
- package/src/builders/ObjectSchemaBuilder.ts +0 -794
- package/src/builders/SchemaBuilder.test.ts +0 -73
- package/src/builders/SchemaBuilder.ts +0 -135
- package/src/builders/StringSchemaBuilder.test.ts +0 -318
- package/src/builders/StringSchemaBuilder.ts +0 -392
- package/src/builders/UnionSchemaBuilder.test.ts +0 -162
- package/src/builders/UnionSchemaBuilder.ts +0 -154
- package/src/defaultSchemas.ts +0 -44
- package/src/index.ts +0 -66
- package/src/schema.ts +0 -849
- package/src/schemaRegistry.builders.test.ts +0 -1393
- package/src/schemaRegistry.test.ts +0 -118
- package/src/schemaRegistry.ts +0 -461
- package/src/validators/validateArray.ts +0 -76
- package/src/validators/validateBoolean.ts +0 -36
- package/src/validators/validateFunction.ts +0 -25
- package/src/validators/validateNumber.ts +0 -82
- package/src/validators/validateObject.ts +0 -119
- package/src/validators/validateString.ts +0 -59
- package/src/validators/validateUnion.ts +0 -36
- package/tsconfig.json +0 -16
|
@@ -1,206 +1,386 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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;
|
|
43
56
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.isRequired = defaultSchema.isRequired;
|
|
47
|
-
this.isNullable = defaultSchema.isNullable;
|
|
48
|
-
this._ensureNotNaN = defaultSchema.ensureNotNaN;
|
|
49
|
-
this._ensureIsFinite = defaultSchema.ensureIsFinite;
|
|
57
|
+
if (typeof props.max === 'number') {
|
|
58
|
+
this.#max = props.max;
|
|
50
59
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
_min;
|
|
54
|
-
_max;
|
|
55
|
-
_isInteger;
|
|
56
|
-
_ensureNotNaN = true;
|
|
57
|
-
_ensureIsFinite = true;
|
|
58
|
-
_equals;
|
|
59
|
-
min(val) {
|
|
60
|
-
if (this._min === val) {
|
|
61
|
-
return this;
|
|
60
|
+
if (typeof props.ensureNotNaN === 'boolean') {
|
|
61
|
+
this.#ensureNotNaN = props.ensureNotNaN;
|
|
62
62
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
min: val
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
clearMin() {
|
|
69
|
-
if (typeof this._min === 'undefined') {
|
|
70
|
-
return this;
|
|
63
|
+
if (typeof props.ensureIsFinite === 'boolean') {
|
|
64
|
+
this.#ensureIsFinite = props.ensureIsFinite;
|
|
71
65
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
min: undefined
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
max(val) {
|
|
78
|
-
if (this._max === val) {
|
|
79
|
-
return this;
|
|
66
|
+
if (typeof props.isInteger === 'boolean') {
|
|
67
|
+
this.#isInteger = props.isInteger;
|
|
80
68
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
clearMax() {
|
|
87
|
-
if (typeof this._max === 'undefined') {
|
|
88
|
-
return this;
|
|
69
|
+
if (typeof props.equalsTo === 'number' ||
|
|
70
|
+
typeof props.equalsTo === 'undefined') {
|
|
71
|
+
this.#equalsTo = props.equalsTo;
|
|
89
72
|
}
|
|
90
|
-
return NumberSchemaBuilder.create({
|
|
91
|
-
...this._schema,
|
|
92
|
-
max: undefined
|
|
93
|
-
});
|
|
94
73
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
+
};
|
|
103
111
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
112
|
+
/**
|
|
113
|
+
* @hidden
|
|
114
|
+
*/
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
116
|
+
hasType(notUsed) {
|
|
117
|
+
return this.createFromProps({
|
|
118
|
+
...this.introspect()
|
|
111
119
|
});
|
|
112
120
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return
|
|
118
|
-
...this.
|
|
119
|
-
isNullable: true
|
|
121
|
+
/**
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
clearHasType() {
|
|
125
|
+
return this.createFromProps({
|
|
126
|
+
...this.introspect()
|
|
120
127
|
});
|
|
121
128
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
+
};
|
|
125
142
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return this;
|
|
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
|
+
};
|
|
134
150
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
|
138
253
|
});
|
|
139
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Clear `equals()` call.
|
|
257
|
+
*/
|
|
140
258
|
clearEquals() {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
|
147
271
|
});
|
|
148
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Only integer values will be considered as valid after this call.
|
|
275
|
+
*/
|
|
149
276
|
isInteger() {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
return NumberSchemaBuilder.create({
|
|
154
|
-
...this._schema,
|
|
277
|
+
return this.createFromProps({
|
|
278
|
+
...this.introspect(),
|
|
155
279
|
isInteger: true
|
|
156
280
|
});
|
|
157
281
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return
|
|
163
|
-
...this._schema,
|
|
164
|
-
isInteger: undefined
|
|
165
|
-
});
|
|
282
|
+
/**
|
|
283
|
+
* @hidden
|
|
284
|
+
*/
|
|
285
|
+
required() {
|
|
286
|
+
return super.required();
|
|
166
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* @hidden
|
|
290
|
+
*/
|
|
291
|
+
optional() {
|
|
292
|
+
return super.optional();
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Do not accept NaN value
|
|
296
|
+
*/
|
|
167
297
|
notNaN() {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
return NumberSchemaBuilder.create({
|
|
172
|
-
...this._schema,
|
|
298
|
+
return this.createFromProps({
|
|
299
|
+
...this.introspect(),
|
|
173
300
|
ensureNotNaN: true
|
|
174
301
|
});
|
|
175
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Consider NaN value as valid
|
|
305
|
+
*/
|
|
176
306
|
canBeNaN() {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
return NumberSchemaBuilder.create({
|
|
181
|
-
...this._schema,
|
|
307
|
+
return this.createFromProps({
|
|
308
|
+
...this.introspect(),
|
|
182
309
|
ensureNotNaN: false
|
|
183
310
|
});
|
|
184
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Do not accept `Infinity`.
|
|
314
|
+
*/
|
|
185
315
|
isFinite() {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
return NumberSchemaBuilder.create({
|
|
190
|
-
...this._schema,
|
|
316
|
+
return this.createFromProps({
|
|
317
|
+
...this.introspect(),
|
|
191
318
|
ensureIsFinite: true
|
|
192
319
|
});
|
|
193
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Consider `Infinity` as valid.
|
|
323
|
+
*/
|
|
194
324
|
canBeInfinite() {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
return NumberSchemaBuilder.create({
|
|
199
|
-
...this._schema,
|
|
325
|
+
return this.createFromProps({
|
|
326
|
+
...this.introspect(),
|
|
200
327
|
ensureIsFinite: false
|
|
201
328
|
});
|
|
202
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
|
+
});
|
|
203
386
|
}
|
|
204
|
-
exports.NumberSchemaBuilder = NumberSchemaBuilder;
|
|
205
|
-
const number = () => NumberSchemaBuilder.create();
|
|
206
|
-
exports.number = number;
|