@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,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type NumberSchemaBuilderCreateProps<T = number, R extends boolean = true> = Partial<ReturnType<NumberSchemaBuilder<T, R>['introspect']>>;
|
|
1
|
+
import { type BRAND, type PreprocessorEntry, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult, type ValidatorEntry } from './SchemaBuilder.js';
|
|
2
|
+
type NumberSchemaBuilderCreateProps<T = number, R extends boolean = true> = Partial<ReturnType<NumberSchemaBuilder<T, R, any>['introspect']>>;
|
|
3
3
|
/**
|
|
4
4
|
* Number schema builder class. Allows to create Number schemas.
|
|
5
5
|
* Can be required or optional, can be restricted to be equal to a certain value,
|
|
@@ -11,143 +11,255 @@ type NumberSchemaBuilderCreateProps<T = number, R extends boolean = true> = Part
|
|
|
11
11
|
*
|
|
12
12
|
* @example ```ts
|
|
13
13
|
* const schema = number().equals(42);
|
|
14
|
-
* const result =
|
|
14
|
+
* const result = schema.validate(42);
|
|
15
15
|
* // result.valid === true
|
|
16
16
|
* // result.object === 42
|
|
17
17
|
* ```
|
|
18
18
|
* @example ```ts
|
|
19
19
|
* const schema = number();
|
|
20
|
-
* const result =
|
|
20
|
+
* const result = schema.validate('42');
|
|
21
21
|
* // result.valid === false
|
|
22
22
|
* // result.errors[0].message === 'is expected to be a number'
|
|
23
23
|
* ```
|
|
24
24
|
* @example ```ts
|
|
25
25
|
* const schema = number().min(0).max(100);
|
|
26
|
-
* const result =
|
|
26
|
+
* const result = schema.validate(42);
|
|
27
27
|
* // result.valid === true
|
|
28
28
|
* // result.object === 42
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
31
31
|
* @example ```ts
|
|
32
32
|
* const schema = number().min(0).max(100);
|
|
33
|
-
* const result =
|
|
33
|
+
* const result = schema.validate(142.5);
|
|
34
34
|
* // result.valid === false
|
|
35
35
|
* // result.errors[0].message === 'is expected to be less than or equal to 100'
|
|
36
36
|
* ```
|
|
37
37
|
*
|
|
38
38
|
* @see {@link number}
|
|
39
39
|
*/
|
|
40
|
-
export declare class NumberSchemaBuilder<TResult = number, TRequired extends boolean = true> extends SchemaBuilder<TResult, TRequired> {
|
|
40
|
+
export declare class NumberSchemaBuilder<TResult = number, TRequired extends boolean = true, TNullable extends boolean = false, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
41
41
|
#private;
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
/**
|
|
43
|
+
* @hidden
|
|
44
|
+
*/
|
|
45
|
+
static create(props: NumberSchemaBuilderCreateProps): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
46
|
+
protected constructor(props: NumberSchemaBuilderCreateProps);
|
|
44
47
|
introspect(): {
|
|
45
48
|
/**
|
|
46
49
|
* Min valid value (if defined).
|
|
47
50
|
*/
|
|
48
51
|
min: number | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Min valid value error message provider.
|
|
54
|
+
* If not provided, default error message will be used.
|
|
55
|
+
*/
|
|
56
|
+
minValidationErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
49
57
|
/**
|
|
50
58
|
* Max valid value (if defined).
|
|
51
59
|
*/
|
|
52
60
|
max: number | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Max valid value error message provider.
|
|
63
|
+
* If not provided, default error message will be used.
|
|
64
|
+
*/
|
|
65
|
+
maxValidationErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
53
66
|
/**
|
|
54
67
|
* Make sure that object is not `NaN`. `true` by default.
|
|
55
68
|
*/
|
|
56
69
|
ensureNotNaN: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* EnsureNotNaN error message provider.
|
|
72
|
+
* If not provided, default error message will be used.
|
|
73
|
+
*/
|
|
74
|
+
ensureNotNaNErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
57
75
|
/**
|
|
58
76
|
* Make sure that object is not different kinds of `infinity`. `true` by default.
|
|
59
77
|
*/
|
|
60
78
|
ensureIsFinite: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* EnsureIsFinite error message provider.
|
|
81
|
+
*/
|
|
82
|
+
ensureIsFiniteErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
61
83
|
/**
|
|
62
84
|
* If set, restrict object to be equal to a certain value.
|
|
63
85
|
*/
|
|
64
86
|
equalsTo: number | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* EqualsTo error message provider.
|
|
89
|
+
* If not provided, default error message will be used.
|
|
90
|
+
*/
|
|
91
|
+
equalsToValidationErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
65
92
|
/**
|
|
66
93
|
* Allow only integer values (floating point values will be rejected
|
|
67
94
|
* as invalid)
|
|
68
95
|
*/
|
|
69
96
|
isInteger: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* EnsureIsInteger error message provider.
|
|
99
|
+
*/
|
|
100
|
+
ensureIsIntegerErrorMessageProvider: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired, false, false, {}>>;
|
|
70
101
|
/**
|
|
71
102
|
* Array of preprocessor functions
|
|
72
103
|
*/
|
|
73
|
-
preprocessors:
|
|
104
|
+
preprocessors: PreprocessorEntry<TResult>[];
|
|
74
105
|
/**
|
|
75
106
|
* Array of validator functions
|
|
76
107
|
*/
|
|
77
|
-
validators:
|
|
108
|
+
validators: ValidatorEntry<TResult>[];
|
|
78
109
|
type: string;
|
|
79
110
|
isRequired: boolean;
|
|
111
|
+
isNullable: boolean;
|
|
112
|
+
isReadonly: boolean;
|
|
113
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
114
|
+
extensions: {
|
|
115
|
+
[x: string]: unknown;
|
|
116
|
+
};
|
|
117
|
+
hasDefault: boolean;
|
|
118
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
119
|
+
description: string | undefined;
|
|
120
|
+
hasCatch: boolean;
|
|
121
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
80
122
|
};
|
|
81
123
|
/**
|
|
82
|
-
* @
|
|
124
|
+
* @inheritdoc
|
|
83
125
|
*/
|
|
84
|
-
hasType<T>(
|
|
126
|
+
hasType<T>(_notUsed?: T): NumberSchemaBuilder<T, true, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
85
127
|
/**
|
|
86
|
-
* @
|
|
128
|
+
* @inheritdoc
|
|
129
|
+
*/
|
|
130
|
+
clearHasType(): NumberSchemaBuilder<number, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
131
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
132
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
133
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
134
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
135
|
+
/**
|
|
136
|
+
* Performs synchronous validation of number schema over `object`.
|
|
137
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
138
|
+
* @param context Optional `ValidationContext` settings.
|
|
87
139
|
*/
|
|
88
|
-
|
|
140
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
89
141
|
/**
|
|
90
|
-
* Performs
|
|
142
|
+
* Performs async validation of number schema over `object`.
|
|
143
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
91
144
|
* @param context Optional `ValidationContext` settings.
|
|
92
145
|
*/
|
|
93
|
-
|
|
146
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
94
147
|
protected createFromProps<T, TReq extends boolean>(props: NumberSchemaBuilderCreateProps<T, TReq>): this;
|
|
95
148
|
/**
|
|
96
149
|
* Restricts number to be equal to `value`.
|
|
97
150
|
*/
|
|
98
|
-
equals<T extends number>(value: T
|
|
151
|
+
equals<T extends number>(value: T,
|
|
152
|
+
/**
|
|
153
|
+
* Custom error message provider.
|
|
154
|
+
*/
|
|
155
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<T, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
99
156
|
/**
|
|
100
157
|
* Clear `equals()` call.
|
|
101
158
|
*/
|
|
102
|
-
clearEquals(): NumberSchemaBuilder<number, TRequired
|
|
159
|
+
clearEquals(): NumberSchemaBuilder<number, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
103
160
|
/**
|
|
161
|
+
* @deprecated Use {@link clearIsInteger} instead.
|
|
104
162
|
* Float values will be considered as valid after this call.
|
|
105
163
|
*/
|
|
106
|
-
isFloat(): NumberSchemaBuilder<TResult, TRequired
|
|
164
|
+
isFloat(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
165
|
+
/**
|
|
166
|
+
* Clear `isInteger()` call.
|
|
167
|
+
*/
|
|
168
|
+
clearIsInteger(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
107
169
|
/**
|
|
108
170
|
* Only integer values will be considered as valid after this call.
|
|
109
171
|
*/
|
|
110
|
-
isInteger(
|
|
172
|
+
isInteger(
|
|
173
|
+
/**
|
|
174
|
+
* Custom error message provider.
|
|
175
|
+
*/
|
|
176
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
111
177
|
/**
|
|
112
178
|
* @hidden
|
|
113
179
|
*/
|
|
114
|
-
required(): NumberSchemaBuilder<TResult, true
|
|
180
|
+
required(errorMessage?: ValidationErrorMessageProvider): NumberSchemaBuilder<TResult, true, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
115
181
|
/**
|
|
116
182
|
* @hidden
|
|
117
183
|
*/
|
|
118
|
-
optional(): NumberSchemaBuilder<TResult, false
|
|
184
|
+
optional(): NumberSchemaBuilder<TResult, false, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
185
|
+
/**
|
|
186
|
+
* @hidden
|
|
187
|
+
*/
|
|
188
|
+
default(value: TResult | (() => TResult)): NumberSchemaBuilder<TResult, true, TNullable, true, TExtensions> & TExtensions;
|
|
189
|
+
/**
|
|
190
|
+
* @hidden
|
|
191
|
+
*/
|
|
192
|
+
clearDefault(): NumberSchemaBuilder<TResult, TRequired, TNullable, false, TExtensions> & TExtensions;
|
|
193
|
+
/**
|
|
194
|
+
* @hidden
|
|
195
|
+
*/
|
|
196
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): NumberSchemaBuilder<TResult & {
|
|
197
|
+
readonly [K in BRAND]: TBrand;
|
|
198
|
+
}, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
199
|
+
/**
|
|
200
|
+
* Marks the inferred type as `Readonly<number>`. Since numbers are
|
|
201
|
+
* already immutable this is an identity operation, but it sets the
|
|
202
|
+
* `isReadonly` introspection flag for tooling consistency.
|
|
203
|
+
*
|
|
204
|
+
* @see {@link SchemaBuilder.readonly}
|
|
205
|
+
*/
|
|
206
|
+
readonly(): NumberSchemaBuilder<Readonly<TResult>, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
119
207
|
/**
|
|
120
208
|
* Do not accept NaN value
|
|
121
209
|
*/
|
|
122
|
-
notNaN(
|
|
210
|
+
notNaN(
|
|
211
|
+
/**
|
|
212
|
+
* Custom error message provider.
|
|
213
|
+
*/
|
|
214
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
123
215
|
/**
|
|
124
216
|
* Consider NaN value as valid
|
|
125
217
|
*/
|
|
126
|
-
canBeNaN(): NumberSchemaBuilder<TResult, TRequired
|
|
218
|
+
canBeNaN(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
127
219
|
/**
|
|
128
220
|
* Do not accept `Infinity`.
|
|
129
221
|
*/
|
|
130
|
-
isFinite(
|
|
222
|
+
isFinite(
|
|
223
|
+
/**
|
|
224
|
+
* Custom error message provider.
|
|
225
|
+
*/
|
|
226
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
131
227
|
/**
|
|
132
228
|
* Consider `Infinity` as valid.
|
|
133
229
|
*/
|
|
134
|
-
canBeInfinite(): NumberSchemaBuilder<TResult, TRequired
|
|
230
|
+
canBeInfinite(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
135
231
|
/**
|
|
136
232
|
* Restrict number to be at least `minValue`.
|
|
137
233
|
*/
|
|
138
|
-
min(minValue: number
|
|
234
|
+
min(minValue: number,
|
|
235
|
+
/**
|
|
236
|
+
* Custom error message provider.
|
|
237
|
+
*/
|
|
238
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
139
239
|
/**
|
|
140
240
|
* Clear `min()` call.
|
|
141
241
|
*/
|
|
142
|
-
clearMin(): NumberSchemaBuilder<TResult, TRequired
|
|
242
|
+
clearMin(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
143
243
|
/**
|
|
144
244
|
* Restrict number to be no more than `maxValue`.
|
|
145
245
|
*/
|
|
146
|
-
max(maxValue: number
|
|
246
|
+
max(maxValue: number,
|
|
247
|
+
/**
|
|
248
|
+
* Custom error message provider.
|
|
249
|
+
*/
|
|
250
|
+
errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder<TResult, TRequired>>): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
147
251
|
/**
|
|
148
252
|
* Clear `max()` call.
|
|
149
253
|
*/
|
|
150
|
-
clearMax(): NumberSchemaBuilder<TResult, TRequired
|
|
254
|
+
clearMax(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
255
|
+
/**
|
|
256
|
+
* @hidden
|
|
257
|
+
*/
|
|
258
|
+
nullable(): NumberSchemaBuilder<TResult, TRequired, true, THasDefault, TExtensions> & TExtensions;
|
|
259
|
+
/**
|
|
260
|
+
* @hidden
|
|
261
|
+
*/
|
|
262
|
+
notNullable(): NumberSchemaBuilder<TResult, TRequired, false, THasDefault, TExtensions> & TExtensions;
|
|
151
263
|
}
|
|
152
264
|
/**
|
|
153
265
|
* Creates a number schema restricted to `equals` value.
|
|
@@ -1,386 +1,2 @@
|
|
|
1
|
-
import
|
|
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-KESLT5EE.js";import"../chunk-CTP4RHDG.js";export{a as NumberSchemaBuilder,b as number};
|
|
2
|
+
//# sourceMappingURL=NumberSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|