@webiny/validation 5.44.1-beta.1 β 5.45.0-beta.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 +7 -405
- package/index.d.ts +4 -4
- package/index.js +50 -68
- package/index.js.map +1 -1
- package/package.json +9 -12
- package/types.d.ts +1 -1
- package/types.js +1 -5
- package/types.js.map +1 -1
- package/validation.d.ts +2 -2
- package/validation.js +13 -20
- package/validation.js.map +1 -1
- package/validationError.js +1 -7
- package/validationError.js.map +1 -1
- package/validators/creditCard.js +5 -12
- package/validators/creditCard.js.map +1 -1
- package/validators/dateGte.js +3 -10
- package/validators/dateGte.js.map +1 -1
- package/validators/dateLte.js +3 -10
- package/validators/dateLte.js.map +1 -1
- package/validators/email.js +3 -12
- package/validators/email.js.map +1 -1
- package/validators/eq.js +4 -11
- package/validators/eq.js.map +1 -1
- package/validators/gt.js +3 -10
- package/validators/gt.js.map +1 -1
- package/validators/gte.js +3 -10
- package/validators/gte.js.map +1 -1
- package/validators/in.js +3 -10
- package/validators/in.js.map +1 -1
- package/validators/integer.js +5 -13
- package/validators/integer.js.map +1 -1
- package/validators/json.js +4 -12
- package/validators/json.js.map +1 -1
- package/validators/lt.js +3 -11
- package/validators/lt.js.map +1 -1
- package/validators/lte.js +3 -11
- package/validators/lte.js.map +1 -1
- package/validators/maxLength.js +12 -20
- package/validators/maxLength.js.map +1 -1
- package/validators/minLength.js +12 -20
- package/validators/minLength.js.map +1 -1
- package/validators/number.js +7 -14
- package/validators/number.js.map +1 -1
- package/validators/numeric.js +6 -13
- package/validators/numeric.js.map +1 -1
- package/validators/password.js +3 -11
- package/validators/password.js.map +1 -1
- package/validators/phone.js +3 -11
- package/validators/phone.js.map +1 -1
- package/validators/required.js +3 -11
- package/validators/required.js.map +1 -1
- package/validators/slug.js +3 -11
- package/validators/slug.js.map +1 -1
- package/validators/time/index.js +9 -17
- package/validators/time/index.js.map +1 -1
- package/validators/timeGte.js +5 -12
- package/validators/timeGte.js.map +1 -1
- package/validators/timeLte.js +5 -12
- package/validators/timeLte.js.map +1 -1
- package/validators/url.js +7 -23
- package/validators/url.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,409 +1,11 @@
|
|
|
1
|
-
#
|
|
2
|
-
[](https://www.npmjs.com/package/@webiny/validation)
|
|
3
|
-
[](https://www.npmjs.com/package/@webiny/validation)
|
|
4
|
-
[](https://github.com/prettier/prettier)
|
|
5
|
-
[](http://makeapullrequest.com)
|
|
1
|
+
# @webiny/validation
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
|
|
5
|
+
> Itβs **included in every Webiny project by default** and is not meant to be used as a standalone package.
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
π **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
---
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- [Installation](#installation)
|
|
16
|
-
- [Get Started](#get-started)
|
|
17
|
-
- [Classes](#classes)
|
|
18
|
-
- [Validation](#validation)
|
|
19
|
-
- [setValidator](#setvalidator)
|
|
20
|
-
- [getValidator](#getvalidator)
|
|
21
|
-
- [validate](#validate)
|
|
22
|
-
- [validateSync](#validatesync)
|
|
23
|
-
- [Flow Types](#flow-types)
|
|
24
|
-
- [ValidationError](#validationerror)
|
|
25
|
-
- [creditCard](#creditcard)
|
|
26
|
-
- [email](#email)
|
|
27
|
-
- [eq](#eq)
|
|
28
|
-
- [gt](#gt)
|
|
29
|
-
- [gte](#gte)
|
|
30
|
-
- [in](#in)
|
|
31
|
-
- [number](#number)
|
|
32
|
-
- [Validator](#validator)
|
|
33
|
-
- [ValidateOptions](#validateoptions)
|
|
34
|
-
|
|
35
|
-
### Installation
|
|
36
|
-
|
|
37
|
-
`yarn add @webiny/validation`
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
### Get Started
|
|
41
|
-
|
|
42
|
-
#### Simple example
|
|
43
|
-
|
|
44
|
-
Validation of data is performed asynchronously by default, like in the following example:
|
|
45
|
-
|
|
46
|
-
import { validation } from '@webiny/validation';
|
|
47
|
-
|
|
48
|
-
validation.validate(123, 'required,number,gt:20').then(() => {
|
|
49
|
-
// Value is valid
|
|
50
|
-
}).catch(e => {
|
|
51
|
-
// Value is invalid
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
Validators are specified by its name and in this case, there are three - `required`, `number` and `gt`.
|
|
55
|
-
Since value is not empty, it is a number and is greater than 20, code execution will proceed regularly.
|
|
56
|
-
On the other hand, if one of the validator was not satisfied, an instance of `ValidationError` would be thrown.
|
|
57
|
-
|
|
58
|
-
import { validation } from '@webiny/validation';
|
|
59
|
-
|
|
60
|
-
validation.validate(10, 'required,number,gt:20').catch(e => {
|
|
61
|
-
// Throws an Error with message "Value needs to be greater than 20.".
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
It is possible to provide as many validators as needed.
|
|
65
|
-
|
|
66
|
-
#### Passing arguments
|
|
67
|
-
|
|
68
|
-
As seen in previous section, validators can accept additional arguments, which are divided by colon.
|
|
69
|
-
The following validator simply states that value must be greater than 20:
|
|
70
|
-
|
|
71
|
-
gt:20
|
|
72
|
-
|
|
73
|
-
Some validators may even accept more than one arguments:
|
|
74
|
-
|
|
75
|
-
in:dog:cat:fish:bird
|
|
76
|
-
|
|
77
|
-
There is no limit on the number of passed arguments.
|
|
78
|
-
|
|
79
|
-
#### Built-in validators
|
|
80
|
-
|
|
81
|
-
The following is a complete list of built-in validators, ready to be used immediately:
|
|
82
|
-
|
|
83
|
-
- `creditCard`
|
|
84
|
-
- `email`
|
|
85
|
-
- `eq`
|
|
86
|
-
- `gt`
|
|
87
|
-
- `gte`
|
|
88
|
-
- `in`
|
|
89
|
-
- `integer`
|
|
90
|
-
- `lt`
|
|
91
|
-
- `lte`
|
|
92
|
-
- `maxLength`
|
|
93
|
-
- `minLength`
|
|
94
|
-
- `number`
|
|
95
|
-
- `password`
|
|
96
|
-
- `phone`
|
|
97
|
-
- `required`
|
|
98
|
-
- `url`
|
|
99
|
-
|
|
100
|
-
More information about each can be found in the following sections.
|
|
101
|
-
|
|
102
|
-
#### Adding custom validators
|
|
103
|
-
|
|
104
|
-
Apart from built-in validators, there are cases where additional validators might be needed. The following example
|
|
105
|
-
shows how to add a custom validator:
|
|
106
|
-
|
|
107
|
-
import { validation, ValidationError } from '@webiny/validation';
|
|
108
|
-
|
|
109
|
-
validation.setValidator('gender', value => {
|
|
110
|
-
if (['male', 'female'].includes(value)) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
throw new ValidationError('Value needs to be "male" or "female".);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// Throws an instance of ValidationError error.
|
|
117
|
-
await validation.validate('none', 'gender');
|
|
118
|
-
|
|
119
|
-
#### Synchronous validation
|
|
120
|
-
|
|
121
|
-
As mentioned, validation by default is performed asynchronously.
|
|
122
|
-
But if more suitable, it can also be performed synchronously:
|
|
123
|
-
|
|
124
|
-
import { validation } from '@webiny/validation';
|
|
125
|
-
|
|
126
|
-
// Throws an instance of ValidationError error.
|
|
127
|
-
validation.validateSync('parrot', 'in:cat:dog:fish:parrot');
|
|
128
|
-
|
|
129
|
-
// Success.
|
|
130
|
-
validation.validateSync('fish', 'in:cat:dog:fish:parrot');
|
|
131
|
-
|
|
132
|
-
#### Returning instead of throwing
|
|
133
|
-
|
|
134
|
-
The following example shows how to force ValidationError to be returned, instead of thrown:
|
|
135
|
-
|
|
136
|
-
import { validation } from '@webiny/validation';
|
|
137
|
-
const error = await validation.validate("", "required", { throw: false });
|
|
138
|
-
|
|
139
|
-
Once executed, an instance of ValidationError will be assigned to the `error` constant.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
### Classes
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
#### Validation
|
|
148
|
-
|
|
149
|
-
[packages-utils/@webiny/validation/src/validation.js:23-148](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validation.js#L23-L148 "Source code on GitHub")
|
|
150
|
-
|
|
151
|
-
Main class of Validation library.
|
|
152
|
-
Exported as a singleton instance, it offers methods for sync/async data validation and overwriting or adding new validators.
|
|
153
|
-
|
|
154
|
-
**Examples**
|
|
155
|
-
|
|
156
|
-
```javascript
|
|
157
|
-
import { validation } from '@webiny/validation';
|
|
158
|
-
|
|
159
|
-
// `validation` is a preconfigured instance of Validation class.
|
|
160
|
-
// From here you can either add new validators or use it as-is.
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
##### setValidator
|
|
164
|
-
|
|
165
|
-
[packages-utils/@webiny/validation/src/validation.js:40-43](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validation.js#L40-L43 "Source code on GitHub")
|
|
166
|
-
|
|
167
|
-
Add new validator.
|
|
168
|
-
|
|
169
|
-
**Parameters**
|
|
170
|
-
|
|
171
|
-
- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Validator name.
|
|
172
|
-
- `callable` **[Validator](#validator)** Validator function which throws a ValidationError if validation fails.
|
|
173
|
-
|
|
174
|
-
Returns **[Validation](#validation)**
|
|
175
|
-
|
|
176
|
-
##### getValidator
|
|
177
|
-
|
|
178
|
-
[packages-utils/@webiny/validation/src/validation.js:50-55](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validation.js#L50-L55 "Source code on GitHub")
|
|
179
|
-
|
|
180
|
-
Get validator function by name.
|
|
181
|
-
|
|
182
|
-
**Parameters**
|
|
183
|
-
|
|
184
|
-
- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Validator name.
|
|
185
|
-
|
|
186
|
-
Returns **[Validator](#validator)** A validator function.
|
|
187
|
-
|
|
188
|
-
##### validate
|
|
189
|
-
|
|
190
|
-
[packages-utils/@webiny/validation/src/validation.js:64-92](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validation.js#L64-L92 "Source code on GitHub")
|
|
191
|
-
|
|
192
|
-
Asynchronously validates value.
|
|
193
|
-
|
|
194
|
-
**Parameters**
|
|
195
|
-
|
|
196
|
-
- `value` **any** Value to validate.
|
|
197
|
-
- `validators` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A list of comma-separated validators (eg. required,number,gt:20).
|
|
198
|
-
- `options` **[ValidateOptions](#validateoptions)** Validation options.
|
|
199
|
-
|
|
200
|
-
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) \| [ValidationError](#validationerror))>**
|
|
201
|
-
|
|
202
|
-
##### validateSync
|
|
203
|
-
|
|
204
|
-
[packages-utils/@webiny/validation/src/validation.js:101-129](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validation.js#L101-L129 "Source code on GitHub")
|
|
205
|
-
|
|
206
|
-
Synchronously validates value.
|
|
207
|
-
|
|
208
|
-
**Parameters**
|
|
209
|
-
|
|
210
|
-
- `value` **any** Value to validate.
|
|
211
|
-
- `validators` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A list of comma-separated validators (eg. required,number,gt:20).
|
|
212
|
-
- `options` **[ValidateOptions](#validateoptions)** Validation options.
|
|
213
|
-
|
|
214
|
-
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) \| [ValidationError](#validationerror))>**
|
|
215
|
-
|
|
216
|
-
### Flow Types
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
### ValidationError
|
|
222
|
-
|
|
223
|
-
[packages-utils/@webiny/validation/src/validationError.js:10-21](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validationError.js#L10-L21 "Source code on GitHub")
|
|
224
|
-
|
|
225
|
-
This class is used by validators to throw an error when value validation fails.
|
|
226
|
-
|
|
227
|
-
**Parameters**
|
|
228
|
-
|
|
229
|
-
- `message` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Error message
|
|
230
|
-
- `validator` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Validator that triggered this error
|
|
231
|
-
- `value` **any** Value that triggered this error
|
|
232
|
-
|
|
233
|
-
### creditCard
|
|
234
|
-
|
|
235
|
-
[packages-utils/@webiny/validation/src/validators/creditCard.js:17-54](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/creditCard.js#L17-L54 "Source code on GitHub")
|
|
236
|
-
|
|
237
|
-
Credit card validator. This validator will check if the given value is a credit card number.
|
|
238
|
-
|
|
239
|
-
**Parameters**
|
|
240
|
-
|
|
241
|
-
- `value` **any** This is the value that will be validated.
|
|
242
|
-
|
|
243
|
-
**Examples**
|
|
244
|
-
|
|
245
|
-
```javascript
|
|
246
|
-
import { validation } from '@webiny/validation';
|
|
247
|
-
validation.validate('notACreditCard', 'creditCard').then(() => {
|
|
248
|
-
// Valid
|
|
249
|
-
}).catch(e => {
|
|
250
|
-
// Invalid
|
|
251
|
-
});
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
- Throws **[ValidationError](#validationerror)**
|
|
255
|
-
|
|
256
|
-
### email
|
|
257
|
-
|
|
258
|
-
[packages-utils/@webiny/validation/src/validators/email.js:17-27](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/email.js#L17-L27 "Source code on GitHub")
|
|
259
|
-
|
|
260
|
-
Email validator. This validator checks if the given value is a valid email address.
|
|
261
|
-
|
|
262
|
-
**Parameters**
|
|
263
|
-
|
|
264
|
-
- `value` **any** This is the value that will be validated.
|
|
265
|
-
|
|
266
|
-
**Examples**
|
|
267
|
-
|
|
268
|
-
```javascript
|
|
269
|
-
import { validation } from '@webiny/validation';
|
|
270
|
-
validation.validate('email@gmail.com', 'email').then(() => {
|
|
271
|
-
// Valid
|
|
272
|
-
}).catch(e => {
|
|
273
|
-
// Invalid
|
|
274
|
-
});
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
- Throws **[ValidationError](#validationerror)**
|
|
278
|
-
|
|
279
|
-
### eq
|
|
280
|
-
|
|
281
|
-
[packages-utils/@webiny/validation/src/validators/eq.js:18-27](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/eq.js#L18-L27 "Source code on GitHub")
|
|
282
|
-
|
|
283
|
-
Equality validator. This validator checks if the given values are equal.
|
|
284
|
-
|
|
285
|
-
**Parameters**
|
|
286
|
-
|
|
287
|
-
- `value` **any** This is the value that will be validated.
|
|
288
|
-
- `params` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** This is the value to validate against. It is passed as a validator parameter: `eq:valueToCompareWith`
|
|
289
|
-
|
|
290
|
-
**Examples**
|
|
291
|
-
|
|
292
|
-
```javascript
|
|
293
|
-
import { validation } from '@webiny/validation';
|
|
294
|
-
validation.validate('email@gmail.com', 'eq:another@gmail.com').then(() => {
|
|
295
|
-
// Valid
|
|
296
|
-
}).catch(e => {
|
|
297
|
-
// Invalid
|
|
298
|
-
});
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
- Throws **[ValidationError](#validationerror)**
|
|
302
|
-
|
|
303
|
-
### gt
|
|
304
|
-
|
|
305
|
-
[packages-utils/@webiny/validation/src/validators/gt.js:18-25](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/gt.js#L18-L25 "Source code on GitHub")
|
|
306
|
-
|
|
307
|
-
"Greater than" validator. This validator checks if the given values is greater than the `min` value.
|
|
308
|
-
|
|
309
|
-
**Parameters**
|
|
310
|
-
|
|
311
|
-
- `value` **any** This is the value that will be validated.
|
|
312
|
-
- `params` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** This is the value to validate against. It is passed as a validator parameter: `gt:valueToCompareAgainst`
|
|
313
|
-
|
|
314
|
-
**Examples**
|
|
315
|
-
|
|
316
|
-
```javascript
|
|
317
|
-
import { validation } from '@webiny/validation';
|
|
318
|
-
validation.validate(10, 'gt:100').then(() => {
|
|
319
|
-
// Valid
|
|
320
|
-
}).catch(e => {
|
|
321
|
-
// Invalid
|
|
322
|
-
});
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
- Throws **[ValidationError](#validationerror)**
|
|
326
|
-
|
|
327
|
-
### gte
|
|
328
|
-
|
|
329
|
-
[packages-utils/@webiny/validation/src/validators/gte.js:18-25](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/gte.js#L18-L25 "Source code on GitHub")
|
|
330
|
-
|
|
331
|
-
"Greater than or equals" validator. This validator checks if the given values is greater than or equal to the `min` value.
|
|
332
|
-
|
|
333
|
-
**Parameters**
|
|
334
|
-
|
|
335
|
-
- `value` **any** This is the value that will be validated.
|
|
336
|
-
- `min` **any** This is the value to validate against. It is passed as a validator parameter: `gte:valueToCompareAgainst`
|
|
337
|
-
|
|
338
|
-
**Examples**
|
|
339
|
-
|
|
340
|
-
```javascript
|
|
341
|
-
import { validation } from '@webiny/validation';
|
|
342
|
-
validation.validate(10, 'gte:100').then(() => {
|
|
343
|
-
// Valid
|
|
344
|
-
}).catch(e => {
|
|
345
|
-
// Invalid
|
|
346
|
-
});
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
- Throws **[ValidationError](#validationerror)**
|
|
350
|
-
|
|
351
|
-
### in
|
|
352
|
-
|
|
353
|
-
[packages-utils/@webiny/validation/src/validators/in.js:18-27](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/in.js#L18-L27 "Source code on GitHub")
|
|
354
|
-
|
|
355
|
-
"In array" validator. This validator checks if the given values is greater than or equal to the `min` value.
|
|
356
|
-
|
|
357
|
-
**Parameters**
|
|
358
|
-
|
|
359
|
-
- `value` **any** This is the value that will be validated.
|
|
360
|
-
- `params` **any** This is the array of values to search in. It is passed as a validator parameter: `in:1:2:3`. Array values are separated by `:`.
|
|
361
|
-
|
|
362
|
-
**Examples**
|
|
363
|
-
|
|
364
|
-
```javascript
|
|
365
|
-
import { validation } from '@webiny/validation';
|
|
366
|
-
validation.validate(10, 'in:10:20:30').then(() => {
|
|
367
|
-
// Valid
|
|
368
|
-
}).catch(e => {
|
|
369
|
-
// Invalid
|
|
370
|
-
});
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
- Throws **[ValidationError](#validationerror)**
|
|
374
|
-
|
|
375
|
-
### number
|
|
376
|
-
|
|
377
|
-
[packages-utils/@webiny/validation/src/validators/number.js:11-19](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/src/validators/number.js#L11-L19 "Source code on GitHub")
|
|
378
|
-
|
|
379
|
-
This validator checks of the given value is a number
|
|
380
|
-
|
|
381
|
-
**Parameters**
|
|
382
|
-
|
|
383
|
-
- `value` **any**
|
|
384
|
-
|
|
385
|
-
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
|
|
386
|
-
|
|
387
|
-
### Validator
|
|
388
|
-
|
|
389
|
-
[packages-utils/@webiny/validation/types.js:9-9](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/types.js#L9-L9 "Source code on GitHub")
|
|
390
|
-
|
|
391
|
-
This type defines the validator function.
|
|
392
|
-
|
|
393
|
-
**Parameters**
|
|
394
|
-
|
|
395
|
-
- `value` **any** This is the value being validated.
|
|
396
|
-
- `parameters` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** (Optional) This represents an array validator parameters.
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
- Throws **[ValidationError](#validationerror)**
|
|
400
|
-
|
|
401
|
-
### ValidateOptions
|
|
402
|
-
|
|
403
|
-
[packages-utils/@webiny/validation/types.js:17-19](https://github.com/webiny/webiny-js/blob/d48d320d653854e2ae18bc85dde4cb083d93d593/packages-utils/@webiny/validation/types.js#L17-L19 "Source code on GitHub")
|
|
404
|
-
|
|
405
|
-
This is an object containing validation options.
|
|
406
|
-
|
|
407
|
-
**Properties**
|
|
408
|
-
|
|
409
|
-
- `throw` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Should validation throw on failure? Default: true.
|
|
11
|
+
_This README file is automatically generated during the publish process._
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Validation from "./validation";
|
|
2
|
-
import ValidationError from "./validationError";
|
|
3
|
-
declare const validation: Validation;
|
|
4
|
-
export {
|
|
1
|
+
import Validation from "./validation.js";
|
|
2
|
+
import ValidationError from "./validationError.js";
|
|
3
|
+
export declare const validation: Validation;
|
|
4
|
+
export { Validation, ValidationError };
|
package/index.js
CHANGED
|
@@ -1,70 +1,52 @@
|
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
validation.setValidator("
|
|
47
|
-
validation.setValidator("
|
|
48
|
-
validation.setValidator("
|
|
49
|
-
validation.setValidator("
|
|
50
|
-
|
|
51
|
-
validation.setValidator("in", _in.default);
|
|
52
|
-
validation.setValidator("integer", _integer.default);
|
|
53
|
-
validation.setValidator("json", _json.default);
|
|
54
|
-
validation.setValidator("lt", _lt.default);
|
|
55
|
-
validation.setValidator("lte", _lte.default);
|
|
56
|
-
validation.setValidator("maxLength", _maxLength.default);
|
|
57
|
-
validation.setValidator("minLength", _minLength.default);
|
|
58
|
-
validation.setValidator("number", _number.default);
|
|
59
|
-
validation.setValidator("numeric", _numeric.default);
|
|
60
|
-
validation.setValidator("password", _password.default);
|
|
61
|
-
validation.setValidator("phone", _phone.default);
|
|
62
|
-
validation.setValidator("required", _required.default);
|
|
63
|
-
validation.setValidator("url", _url.default);
|
|
64
|
-
validation.setValidator("dateGte", _dateGte.default);
|
|
65
|
-
validation.setValidator("dateLte", _dateLte.default);
|
|
66
|
-
validation.setValidator("timeGte", _timeGte.default);
|
|
67
|
-
validation.setValidator("timeLte", _timeLte.default);
|
|
68
|
-
validation.setValidator("slug", _slug.default);
|
|
1
|
+
import Validation from "./validation.js";
|
|
2
|
+
import ValidationError from "./validationError.js";
|
|
3
|
+
import creditCard from "./validators/creditCard.js";
|
|
4
|
+
import email from "./validators/email.js";
|
|
5
|
+
import eq from "./validators/eq.js";
|
|
6
|
+
import gt from "./validators/gt.js";
|
|
7
|
+
import gte from "./validators/gte.js";
|
|
8
|
+
import isIn from "./validators/in.js";
|
|
9
|
+
import integer from "./validators/integer.js";
|
|
10
|
+
import json from "./validators/json.js";
|
|
11
|
+
import lt from "./validators/lt.js";
|
|
12
|
+
import lte from "./validators/lte.js";
|
|
13
|
+
import maxLength from "./validators/maxLength.js";
|
|
14
|
+
import minLength from "./validators/minLength.js";
|
|
15
|
+
import number from "./validators/number.js";
|
|
16
|
+
import numeric from "./validators/numeric.js";
|
|
17
|
+
import password from "./validators/password.js";
|
|
18
|
+
import phone from "./validators/phone.js";
|
|
19
|
+
import required from "./validators/required.js";
|
|
20
|
+
import url from "./validators/url.js";
|
|
21
|
+
import dateGte from "./validators/dateGte.js";
|
|
22
|
+
import dateLte from "./validators/dateLte.js";
|
|
23
|
+
import timeGte from "./validators/timeGte.js";
|
|
24
|
+
import timeLte from "./validators/timeLte.js";
|
|
25
|
+
import slug from "./validators/slug.js";
|
|
26
|
+
export const validation = new Validation();
|
|
27
|
+
validation.setValidator("creditCard", creditCard);
|
|
28
|
+
validation.setValidator("email", email);
|
|
29
|
+
validation.setValidator("eq", eq);
|
|
30
|
+
validation.setValidator("gt", gt);
|
|
31
|
+
validation.setValidator("gte", gte);
|
|
32
|
+
validation.setValidator("in", isIn);
|
|
33
|
+
validation.setValidator("integer", integer);
|
|
34
|
+
validation.setValidator("json", json);
|
|
35
|
+
validation.setValidator("lt", lt);
|
|
36
|
+
validation.setValidator("lte", lte);
|
|
37
|
+
validation.setValidator("maxLength", maxLength);
|
|
38
|
+
validation.setValidator("minLength", minLength);
|
|
39
|
+
validation.setValidator("number", number);
|
|
40
|
+
validation.setValidator("numeric", numeric);
|
|
41
|
+
validation.setValidator("password", password);
|
|
42
|
+
validation.setValidator("phone", phone);
|
|
43
|
+
validation.setValidator("required", required);
|
|
44
|
+
validation.setValidator("url", url);
|
|
45
|
+
validation.setValidator("dateGte", dateGte);
|
|
46
|
+
validation.setValidator("dateLte", dateLte);
|
|
47
|
+
validation.setValidator("timeGte", timeGte);
|
|
48
|
+
validation.setValidator("timeLte", timeLte);
|
|
49
|
+
validation.setValidator("slug", slug);
|
|
50
|
+
export { Validation, ValidationError };
|
|
69
51
|
|
|
70
52
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["Validation","ValidationError","creditCard","email","eq","gt","gte","isIn","integer","json","lt","lte","maxLength","minLength","number","numeric","password","phone","required","url","dateGte","dateLte","timeGte","timeLte","slug","validation","setValidator"],"sources":["index.ts"],"sourcesContent":["import Validation from \"./validation.js\";\nimport ValidationError from \"./validationError.js\";\nimport creditCard from \"./validators/creditCard.js\";\nimport email from \"./validators/email.js\";\nimport eq from \"./validators/eq.js\";\nimport gt from \"./validators/gt.js\";\nimport gte from \"./validators/gte.js\";\nimport isIn from \"./validators/in.js\";\nimport integer from \"./validators/integer.js\";\nimport json from \"./validators/json.js\";\nimport lt from \"./validators/lt.js\";\nimport lte from \"./validators/lte.js\";\nimport maxLength from \"./validators/maxLength.js\";\nimport minLength from \"./validators/minLength.js\";\nimport number from \"./validators/number.js\";\nimport numeric from \"./validators/numeric.js\";\nimport password from \"./validators/password.js\";\nimport phone from \"./validators/phone.js\";\nimport required from \"./validators/required.js\";\nimport url from \"./validators/url.js\";\nimport dateGte from \"./validators/dateGte.js\";\nimport dateLte from \"./validators/dateLte.js\";\nimport timeGte from \"./validators/timeGte.js\";\nimport timeLte from \"./validators/timeLte.js\";\nimport slug from \"./validators/slug.js\";\n\nexport const validation = new Validation();\n\nvalidation.setValidator(\"creditCard\", creditCard);\nvalidation.setValidator(\"email\", email);\nvalidation.setValidator(\"eq\", eq);\nvalidation.setValidator(\"gt\", gt);\nvalidation.setValidator(\"gte\", gte);\nvalidation.setValidator(\"in\", isIn);\nvalidation.setValidator(\"integer\", integer);\nvalidation.setValidator(\"json\", json);\nvalidation.setValidator(\"lt\", lt);\nvalidation.setValidator(\"lte\", lte);\nvalidation.setValidator(\"maxLength\", maxLength);\nvalidation.setValidator(\"minLength\", minLength);\nvalidation.setValidator(\"number\", number);\nvalidation.setValidator(\"numeric\", numeric);\nvalidation.setValidator(\"password\", password);\nvalidation.setValidator(\"phone\", phone);\nvalidation.setValidator(\"required\", required);\nvalidation.setValidator(\"url\", url);\nvalidation.setValidator(\"dateGte\", dateGte);\nvalidation.setValidator(\"dateLte\", dateLte);\nvalidation.setValidator(\"timeGte\", timeGte);\nvalidation.setValidator(\"timeLte\", timeLte);\nvalidation.setValidator(\"slug\", slug);\n\nexport { Validation, ValidationError };\n"],"mappings":"AAAA,OAAOA,UAAU;AACjB,OAAOC,eAAe;AACtB,OAAOC,UAAU;AACjB,OAAOC,KAAK;AACZ,OAAOC,EAAE;AACT,OAAOC,EAAE;AACT,OAAOC,GAAG;AACV,OAAOC,IAAI;AACX,OAAOC,OAAO;AACd,OAAOC,IAAI;AACX,OAAOC,EAAE;AACT,OAAOC,GAAG;AACV,OAAOC,SAAS;AAChB,OAAOC,SAAS;AAChB,OAAOC,MAAM;AACb,OAAOC,OAAO;AACd,OAAOC,QAAQ;AACf,OAAOC,KAAK;AACZ,OAAOC,QAAQ;AACf,OAAOC,GAAG;AACV,OAAOC,OAAO;AACd,OAAOC,OAAO;AACd,OAAOC,OAAO;AACd,OAAOC,OAAO;AACd,OAAOC,IAAI;AAEX,OAAO,MAAMC,UAAU,GAAG,IAAIzB,UAAU,CAAC,CAAC;AAE1CyB,UAAU,CAACC,YAAY,CAAC,YAAY,EAAExB,UAAU,CAAC;AACjDuB,UAAU,CAACC,YAAY,CAAC,OAAO,EAAEvB,KAAK,CAAC;AACvCsB,UAAU,CAACC,YAAY,CAAC,IAAI,EAAEtB,EAAE,CAAC;AACjCqB,UAAU,CAACC,YAAY,CAAC,IAAI,EAAErB,EAAE,CAAC;AACjCoB,UAAU,CAACC,YAAY,CAAC,KAAK,EAAEpB,GAAG,CAAC;AACnCmB,UAAU,CAACC,YAAY,CAAC,IAAI,EAAEnB,IAAI,CAAC;AACnCkB,UAAU,CAACC,YAAY,CAAC,SAAS,EAAElB,OAAO,CAAC;AAC3CiB,UAAU,CAACC,YAAY,CAAC,MAAM,EAAEjB,IAAI,CAAC;AACrCgB,UAAU,CAACC,YAAY,CAAC,IAAI,EAAEhB,EAAE,CAAC;AACjCe,UAAU,CAACC,YAAY,CAAC,KAAK,EAAEf,GAAG,CAAC;AACnCc,UAAU,CAACC,YAAY,CAAC,WAAW,EAAEd,SAAS,CAAC;AAC/Ca,UAAU,CAACC,YAAY,CAAC,WAAW,EAAEb,SAAS,CAAC;AAC/CY,UAAU,CAACC,YAAY,CAAC,QAAQ,EAAEZ,MAAM,CAAC;AACzCW,UAAU,CAACC,YAAY,CAAC,SAAS,EAAEX,OAAO,CAAC;AAC3CU,UAAU,CAACC,YAAY,CAAC,UAAU,EAAEV,QAAQ,CAAC;AAC7CS,UAAU,CAACC,YAAY,CAAC,OAAO,EAAET,KAAK,CAAC;AACvCQ,UAAU,CAACC,YAAY,CAAC,UAAU,EAAER,QAAQ,CAAC;AAC7CO,UAAU,CAACC,YAAY,CAAC,KAAK,EAAEP,GAAG,CAAC;AACnCM,UAAU,CAACC,YAAY,CAAC,SAAS,EAAEN,OAAO,CAAC;AAC3CK,UAAU,CAACC,YAAY,CAAC,SAAS,EAAEL,OAAO,CAAC;AAC3CI,UAAU,CAACC,YAAY,CAAC,SAAS,EAAEJ,OAAO,CAAC;AAC3CG,UAAU,CAACC,YAAY,CAAC,SAAS,EAAEH,OAAO,CAAC;AAC3CE,UAAU,CAACC,YAAY,CAAC,MAAM,EAAEF,IAAI,CAAC;AAErC,SAASxB,UAAU,EAAEC,eAAe","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/validation",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.45.0-beta.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "index.js",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
@@ -15,22 +16,18 @@
|
|
|
15
16
|
"license": "MIT",
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"isnumeric": "0.3.3",
|
|
18
|
-
"lodash": "4.17.
|
|
19
|
+
"lodash": "4.17.23"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
|
-
"@types/lodash": "4.17.
|
|
22
|
-
"@webiny/
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
22
|
+
"@types/lodash": "4.17.23",
|
|
23
|
+
"@webiny/build-tools": "5.45.0-beta.0",
|
|
24
|
+
"rimraf": "6.1.3",
|
|
25
|
+
"typescript": "5.9.3",
|
|
26
|
+
"vitest": "4.0.18"
|
|
26
27
|
},
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public",
|
|
29
30
|
"directory": "dist"
|
|
30
31
|
},
|
|
31
|
-
"
|
|
32
|
-
"build": "node ../cli/bin.js run build",
|
|
33
|
-
"watch": "node ../cli/bin.js run watch"
|
|
34
|
-
},
|
|
35
|
-
"gitHead": "ff5f085c417040291d39fdbe5153067f3a23ed86"
|
|
32
|
+
"gitHead": "b85c33cfbe7c02c130445c918d913ef4b2c09cb2"
|
|
36
33
|
}
|
package/types.d.ts
CHANGED
package/types.js
CHANGED
package/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import ValidationError from \"~/validationError\";\n\ntype ValidatorResult = boolean | ValidationError | void | Promise<boolean | ValidationError | void>;\n/**\n * @typedef Validator\n * @name Validator\n * @description This type defines the validator function.\n * @param {any} value This is the value being validated.\n * @param {Array<string>} parameters (Optional) This represents an array validator parameters.\n * @throws {ValidationError}\n */\nexport interface Validator {\n (value: any, params?: string[]): ValidatorResult;\n validatorName?: string;\n}\n\n/**\n * @typedef ValidateOptions\n * @name ValidateOptions\n * @description This is an object containing validation options.\n * @property {boolean} throw Should validation throw on failure? Default: true.\n */\nexport interface ValidateOptions {\n throw?: boolean;\n}\n\n/**\n * @private\n * @typedef ParsedValidators\n * @name ParsedValidators\n * @description An object containing validators with parameters: `{ [string]: Array<string> }`.\n */\nexport interface ParsedValidators {\n [key: string]: Array<string>;\n}\n"],"mappings":"","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type ValidationError from \"~/validationError.js\";\n\ntype ValidatorResult = boolean | ValidationError | void | Promise<boolean | ValidationError | void>;\n/**\n * @typedef Validator\n * @name Validator\n * @description This type defines the validator function.\n * @param {any} value This is the value being validated.\n * @param {Array<string>} parameters (Optional) This represents an array validator parameters.\n * @throws {ValidationError}\n */\nexport interface Validator {\n (value: any, params?: string[]): ValidatorResult;\n validatorName?: string;\n}\n\n/**\n * @typedef ValidateOptions\n * @name ValidateOptions\n * @description This is an object containing validation options.\n * @property {boolean} throw Should validation throw on failure? Default: true.\n */\nexport interface ValidateOptions {\n throw?: boolean;\n}\n\n/**\n * @private\n * @typedef ParsedValidators\n * @name ParsedValidators\n * @description An object containing validators with parameters: `{ [string]: Array<string> }`.\n */\nexport interface ParsedValidators {\n [key: string]: Array<string>;\n}\n"],"mappings":"","ignoreList":[]}
|