@webiny/validation 5.15.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/LICENSE +21 -0
- package/README.md +409 -0
- package/index.d.ts +4 -0
- package/index.js +94 -0
- package/index.js.map +1 -0
- package/package.json +43 -0
- package/types.d.ts +27 -0
- package/types.js +6 -0
- package/types.js.map +1 -0
- package/validation.d.ts +62 -0
- package/validation.js +195 -0
- package/validation.js.map +1 -0
- package/validationError.d.ts +10 -0
- package/validationError.js +30 -0
- package/validationError.js.map +1 -0
- package/validators/creditCard.d.ts +15 -0
- package/validators/creditCard.js +70 -0
- package/validators/creditCard.js.map +1 -0
- package/validators/dateGte.d.ts +5 -0
- package/validators/dateGte.js +38 -0
- package/validators/dateGte.js.map +1 -0
- package/validators/dateLte.d.ts +5 -0
- package/validators/dateLte.js +38 -0
- package/validators/dateLte.js.map +1 -0
- package/validators/email.d.ts +15 -0
- package/validators/email.js +42 -0
- package/validators/email.js.map +1 -0
- package/validators/eq.d.ts +16 -0
- package/validators/eq.js +42 -0
- package/validators/eq.js.map +1 -0
- package/validators/gt.d.ts +16 -0
- package/validators/gt.js +39 -0
- package/validators/gt.js.map +1 -0
- package/validators/gte.d.ts +16 -0
- package/validators/gte.js +39 -0
- package/validators/gte.js.map +1 -0
- package/validators/in.d.ts +2 -0
- package/validators/in.js +28 -0
- package/validators/in.js.map +1 -0
- package/validators/integer.d.ts +2 -0
- package/validators/integer.js +27 -0
- package/validators/integer.js.map +1 -0
- package/validators/json.d.ts +2 -0
- package/validators/json.js +25 -0
- package/validators/json.js.map +1 -0
- package/validators/lt.d.ts +2 -0
- package/validators/lt.js +27 -0
- package/validators/lt.js.map +1 -0
- package/validators/lte.d.ts +2 -0
- package/validators/lte.js +27 -0
- package/validators/lte.js.map +1 -0
- package/validators/maxLength.d.ts +2 -0
- package/validators/maxLength.js +45 -0
- package/validators/maxLength.js.map +1 -0
- package/validators/minLength.d.ts +2 -0
- package/validators/minLength.js +45 -0
- package/validators/minLength.js.map +1 -0
- package/validators/number.d.ts +8 -0
- package/validators/number.js +35 -0
- package/validators/number.js.map +1 -0
- package/validators/numeric.d.ts +8 -0
- package/validators/numeric.js +38 -0
- package/validators/numeric.js.map +1 -0
- package/validators/password.d.ts +2 -0
- package/validators/password.js +27 -0
- package/validators/password.js.map +1 -0
- package/validators/phone.d.ts +2 -0
- package/validators/phone.js +27 -0
- package/validators/phone.js.map +1 -0
- package/validators/required.d.ts +2 -0
- package/validators/required.js +23 -0
- package/validators/required.js.map +1 -0
- package/validators/time/index.d.ts +6 -0
- package/validators/time/index.js +80 -0
- package/validators/time/index.js.map +1 -0
- package/validators/timeGte.d.ts +5 -0
- package/validators/timeGte.js +39 -0
- package/validators/timeGte.js.map +1 -0
- package/validators/timeLte.d.ts +5 -0
- package/validators/timeLte.js +39 -0
- package/validators/timeLte.js.map +1 -0
- package/validators/url.d.ts +2 -0
- package/validators/url.js +60 -0
- package/validators/url.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Webiny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# Validation
|
|
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)
|
|
6
|
+
|
|
7
|
+
A simple data validation library, packed with frequently used validators like `required`, `email`, `url`, `creditCard` etc.
|
|
8
|
+
|
|
9
|
+
## Documentation
|
|
10
|
+
|
|
11
|
+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
12
|
+
|
|
13
|
+
#### Table of Contents
|
|
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.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports, "Validation", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () {
|
|
11
|
+
return _validation.default;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(exports, "ValidationError", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () {
|
|
17
|
+
return _validationError.default;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
exports.validation = void 0;
|
|
21
|
+
|
|
22
|
+
var _validation = _interopRequireDefault(require("./validation"));
|
|
23
|
+
|
|
24
|
+
var _validationError = _interopRequireDefault(require("./validationError"));
|
|
25
|
+
|
|
26
|
+
var _creditCard = _interopRequireDefault(require("./validators/creditCard"));
|
|
27
|
+
|
|
28
|
+
var _email = _interopRequireDefault(require("./validators/email"));
|
|
29
|
+
|
|
30
|
+
var _eq = _interopRequireDefault(require("./validators/eq"));
|
|
31
|
+
|
|
32
|
+
var _gt = _interopRequireDefault(require("./validators/gt"));
|
|
33
|
+
|
|
34
|
+
var _gte = _interopRequireDefault(require("./validators/gte"));
|
|
35
|
+
|
|
36
|
+
var _in = _interopRequireDefault(require("./validators/in"));
|
|
37
|
+
|
|
38
|
+
var _integer = _interopRequireDefault(require("./validators/integer"));
|
|
39
|
+
|
|
40
|
+
var _json = _interopRequireDefault(require("./validators/json"));
|
|
41
|
+
|
|
42
|
+
var _lt = _interopRequireDefault(require("./validators/lt"));
|
|
43
|
+
|
|
44
|
+
var _lte = _interopRequireDefault(require("./validators/lte"));
|
|
45
|
+
|
|
46
|
+
var _maxLength = _interopRequireDefault(require("./validators/maxLength"));
|
|
47
|
+
|
|
48
|
+
var _minLength = _interopRequireDefault(require("./validators/minLength"));
|
|
49
|
+
|
|
50
|
+
var _number = _interopRequireDefault(require("./validators/number"));
|
|
51
|
+
|
|
52
|
+
var _numeric = _interopRequireDefault(require("./validators/numeric"));
|
|
53
|
+
|
|
54
|
+
var _password = _interopRequireDefault(require("./validators/password"));
|
|
55
|
+
|
|
56
|
+
var _phone = _interopRequireDefault(require("./validators/phone"));
|
|
57
|
+
|
|
58
|
+
var _required = _interopRequireDefault(require("./validators/required"));
|
|
59
|
+
|
|
60
|
+
var _url = _interopRequireDefault(require("./validators/url"));
|
|
61
|
+
|
|
62
|
+
var _dateGte = _interopRequireDefault(require("./validators/dateGte"));
|
|
63
|
+
|
|
64
|
+
var _dateLte = _interopRequireDefault(require("./validators/dateLte"));
|
|
65
|
+
|
|
66
|
+
var _timeGte = _interopRequireDefault(require("./validators/timeGte"));
|
|
67
|
+
|
|
68
|
+
var _timeLte = _interopRequireDefault(require("./validators/timeLte"));
|
|
69
|
+
|
|
70
|
+
const validation = new _validation.default();
|
|
71
|
+
exports.validation = validation;
|
|
72
|
+
validation.setValidator("creditCard", _creditCard.default);
|
|
73
|
+
validation.setValidator("email", _email.default);
|
|
74
|
+
validation.setValidator("eq", _eq.default);
|
|
75
|
+
validation.setValidator("gt", _gt.default);
|
|
76
|
+
validation.setValidator("gte", _gte.default);
|
|
77
|
+
validation.setValidator("in", _in.default);
|
|
78
|
+
validation.setValidator("integer", _integer.default);
|
|
79
|
+
validation.setValidator("json", _json.default);
|
|
80
|
+
validation.setValidator("lt", _lt.default);
|
|
81
|
+
validation.setValidator("lte", _lte.default);
|
|
82
|
+
validation.setValidator("maxLength", _maxLength.default);
|
|
83
|
+
validation.setValidator("minLength", _minLength.default);
|
|
84
|
+
validation.setValidator("number", _number.default);
|
|
85
|
+
validation.setValidator("numeric", _numeric.default);
|
|
86
|
+
validation.setValidator("password", _password.default);
|
|
87
|
+
validation.setValidator("phone", _phone.default);
|
|
88
|
+
validation.setValidator("required", _required.default);
|
|
89
|
+
validation.setValidator("url", _url.default);
|
|
90
|
+
validation.setValidator("dateGte", _dateGte.default);
|
|
91
|
+
validation.setValidator("dateLte", _dateLte.default);
|
|
92
|
+
validation.setValidator("timeGte", _timeGte.default);
|
|
93
|
+
validation.setValidator("timeLte", _timeLte.default);
|
|
94
|
+
//# sourceMappingURL=index.js.map
|