@impelsys/validatorjs 3.22.2
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/.eslintrc.js +27 -0
- package/CONTRIBUTE.md +34 -0
- package/LICENSE +20 -0
- package/README.md +607 -0
- package/dist/lang/ar.js +66 -0
- package/dist/lang/az.js +104 -0
- package/dist/lang/be.js +105 -0
- package/dist/lang/bg.js +105 -0
- package/dist/lang/bs.js +106 -0
- package/dist/lang/ca.js +40 -0
- package/dist/lang/cs.js +107 -0
- package/dist/lang/cy.js +106 -0
- package/dist/lang/da.js +51 -0
- package/dist/lang/de.js +46 -0
- package/dist/lang/el.js +43 -0
- package/dist/lang/en.js +54 -0
- package/dist/lang/es.js +40 -0
- package/dist/lang/et.js +106 -0
- package/dist/lang/eu.js +106 -0
- package/dist/lang/fa.js +42 -0
- package/dist/lang/fi.js +51 -0
- package/dist/lang/fr.js +40 -0
- package/dist/lang/hr.js +106 -0
- package/dist/lang/hu.js +106 -0
- package/dist/lang/id.js +51 -0
- package/dist/lang/it.js +41 -0
- package/dist/lang/ja.js +54 -0
- package/dist/lang/ka.js +106 -0
- package/dist/lang/ko.js +106 -0
- package/dist/lang/lt.js +106 -0
- package/dist/lang/lv.js +106 -0
- package/dist/lang/mk.js +106 -0
- package/dist/lang/mn.js +106 -0
- package/dist/lang/ms.js +106 -0
- package/dist/lang/nb_NO.js +42 -0
- package/dist/lang/nl.js +51 -0
- package/dist/lang/pl.js +42 -0
- package/dist/lang/pt.js +110 -0
- package/dist/lang/pt_BR.js +105 -0
- package/dist/lang/ro.js +51 -0
- package/dist/lang/ru.js +40 -0
- package/dist/lang/se.js +106 -0
- package/dist/lang/sl.js +106 -0
- package/dist/lang/sq.js +106 -0
- package/dist/lang/sr.js +106 -0
- package/dist/lang/sv.js +105 -0
- package/dist/lang/tr.js +51 -0
- package/dist/lang/ua.js +40 -0
- package/dist/lang/uk.js +106 -0
- package/dist/lang/vi.js +42 -0
- package/dist/lang/zh.js +42 -0
- package/dist/lang/zh_TW.js +42 -0
- package/dist/validator.js +2183 -0
- package/package.json +78 -0
- package/src/async.js +81 -0
- package/src/attributes.js +199 -0
- package/src/errors.js +77 -0
- package/src/lang/ar.js +63 -0
- package/src/lang/az.js +101 -0
- package/src/lang/be.js +102 -0
- package/src/lang/bg.js +102 -0
- package/src/lang/bs.js +103 -0
- package/src/lang/ca.js +37 -0
- package/src/lang/cs.js +104 -0
- package/src/lang/cy.js +103 -0
- package/src/lang/da.js +48 -0
- package/src/lang/de.js +43 -0
- package/src/lang/el.js +40 -0
- package/src/lang/en.js +51 -0
- package/src/lang/es.js +37 -0
- package/src/lang/et.js +103 -0
- package/src/lang/eu.js +103 -0
- package/src/lang/fa.js +39 -0
- package/src/lang/fi.js +48 -0
- package/src/lang/fr.js +37 -0
- package/src/lang/hr.js +103 -0
- package/src/lang/hu.js +103 -0
- package/src/lang/id.js +48 -0
- package/src/lang/it.js +38 -0
- package/src/lang/ja.js +51 -0
- package/src/lang/ka.js +103 -0
- package/src/lang/ko.js +103 -0
- package/src/lang/lt.js +103 -0
- package/src/lang/lv.js +103 -0
- package/src/lang/mk.js +103 -0
- package/src/lang/mn.js +103 -0
- package/src/lang/ms.js +103 -0
- package/src/lang/nb_NO.js +39 -0
- package/src/lang/nl.js +48 -0
- package/src/lang/pl.js +39 -0
- package/src/lang/pt.js +107 -0
- package/src/lang/pt_BR.js +102 -0
- package/src/lang/ro.js +48 -0
- package/src/lang/ru.js +37 -0
- package/src/lang/se.js +47 -0
- package/src/lang/sl.js +103 -0
- package/src/lang/sq.js +103 -0
- package/src/lang/sr.js +103 -0
- package/src/lang/sv.js +102 -0
- package/src/lang/tr.js +48 -0
- package/src/lang/ua.js +37 -0
- package/src/lang/uk.js +103 -0
- package/src/lang/vi.js +39 -0
- package/src/lang/zh.js +39 -0
- package/src/lang/zh_TW.js +39 -0
- package/src/lang.js +78 -0
- package/src/messages.js +152 -0
- package/src/rules.js +864 -0
- package/src/validator.js +662 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
env: {
|
|
3
|
+
es6: true,
|
|
4
|
+
node: true,
|
|
5
|
+
jest: true
|
|
6
|
+
},
|
|
7
|
+
extends: "eslint:recommended",
|
|
8
|
+
parserOptions: {
|
|
9
|
+
ecmaVersion: 2017
|
|
10
|
+
},
|
|
11
|
+
rules: {
|
|
12
|
+
"no-empty": "off",
|
|
13
|
+
indent: ["error", 2],
|
|
14
|
+
// quotes: ['error', 'single'],
|
|
15
|
+
semi: ["error", "always"],
|
|
16
|
+
"no-useless-escape": "off",
|
|
17
|
+
"no-unused-vars": "off",
|
|
18
|
+
"no-undef": "off",
|
|
19
|
+
"no-control-regex": "off",
|
|
20
|
+
"no-console": [
|
|
21
|
+
"error",
|
|
22
|
+
{
|
|
23
|
+
allow: ["log"]
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
};
|
package/CONTRIBUTE.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Contributing
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
Before making a PR, please open an issue so we can discuss the feature/change.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
Clone the repository:
|
|
9
|
+
|
|
10
|
+
```shell
|
|
11
|
+
git clone https://github.com/skaterdav85/validatorjs.git && cd validatorjs
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Install dependencies:
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
npm install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Now you're ready to develop.
|
|
21
|
+
|
|
22
|
+
## Workflow
|
|
23
|
+
|
|
24
|
+
Source files are located in `src/`. Tests are located in `spec/`, and can be run with:
|
|
25
|
+
|
|
26
|
+
```shell
|
|
27
|
+
npm test
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Please use spaces instead of tabs.
|
|
31
|
+
|
|
32
|
+
## Language Support
|
|
33
|
+
|
|
34
|
+
If you'd like to add support for another language, create the appropriate module in `src/lang/`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2012-2020 Mike Erickson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
# validatorjs
|
|
2
|
+
|
|
3
|
+
The validatorjs library makes data validation in JavaScript very easy in both the browser and Node.js.
|
|
4
|
+
This library was inspired by the [Laravel framework's Validator](http://laravel.com/docs/validation).
|
|
5
|
+
|
|
6
|
+
## This fork!
|
|
7
|
+
|
|
8
|
+
Removes all languages except en for the small bundle.
|
|
9
|
+
|
|
10
|
+
## Why use validatorjs?
|
|
11
|
+
|
|
12
|
+
* Works in both the browser and Node.
|
|
13
|
+
* Readable and declarative validation rules.
|
|
14
|
+
* Error messages with multilingual support.
|
|
15
|
+
* CommonJS/Browserify support.
|
|
16
|
+
* ES6 support.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
### Using npm
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install validatorjs
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Using yarn
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
yarn add validatorjs
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Browser
|
|
33
|
+
|
|
34
|
+
```html
|
|
35
|
+
<script src="validator.js"></script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Node.js / Browserify
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
// ES5
|
|
42
|
+
let Validator = require('validatorjs');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
// ES6
|
|
47
|
+
import * as Validator from 'validatorjs';
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Basic Usage
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
let validation = new Validator(data, rules [, customErrorMessages]);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
__data__ {Object} - The data you want to validate
|
|
57
|
+
|
|
58
|
+
__rules__ {Object} - Validation rules
|
|
59
|
+
|
|
60
|
+
__customErrorMessages__ {Object} - Optional custom error messages to return
|
|
61
|
+
|
|
62
|
+
#### Example 1 - Passing Validation
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
let data = {
|
|
66
|
+
name: 'John',
|
|
67
|
+
email: 'johndoe@gmail.com',
|
|
68
|
+
age: 28
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
let rules = {
|
|
72
|
+
name: 'required',
|
|
73
|
+
email: 'required|email',
|
|
74
|
+
age: 'min:18'
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
let validation = new Validator(data, rules);
|
|
78
|
+
|
|
79
|
+
validation.passes(); // true
|
|
80
|
+
validation.fails(); // false
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
To apply validation rules to the _data_ object, use the same object key names for the _rules_ object.
|
|
84
|
+
|
|
85
|
+
#### Example 2 - Failing Validation
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
let validation = new Validator({
|
|
89
|
+
name: 'D',
|
|
90
|
+
email: 'not an email address.com'
|
|
91
|
+
}, {
|
|
92
|
+
name: 'size:3',
|
|
93
|
+
email: 'required|email'
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
validation.fails(); // true
|
|
97
|
+
validation.passes(); // false
|
|
98
|
+
|
|
99
|
+
// Error messages
|
|
100
|
+
validation.errors.first('email'); // 'The email format is invalid.'
|
|
101
|
+
validation.errors.get('email'); // returns an array of all email error messages
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Nested Rules
|
|
105
|
+
|
|
106
|
+
Nested objects can also be validated. There are two ways to declare validation rules for nested objects. The first way is to declare the validation rules with a corresponding nested object structure that reflects the data. The second way is to declare validation rules with flattened key names. For example, to validate the following data:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
let data = {
|
|
110
|
+
name: 'John',
|
|
111
|
+
bio: {
|
|
112
|
+
age: 28,
|
|
113
|
+
education: {
|
|
114
|
+
primary: 'Elementary School',
|
|
115
|
+
secondary: 'Secondary School'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
We could declare our validation rules as follows:
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
let nested = {
|
|
125
|
+
name: 'required',
|
|
126
|
+
bio: {
|
|
127
|
+
age: 'min:18',
|
|
128
|
+
education: {
|
|
129
|
+
primary: 'string',
|
|
130
|
+
secondary: 'string'
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// OR
|
|
136
|
+
|
|
137
|
+
let flattened = {
|
|
138
|
+
'name': 'required',
|
|
139
|
+
'bio.age': 'min:18',
|
|
140
|
+
'bio.education.primary': 'string',
|
|
141
|
+
'bio.education.secondary': 'string'
|
|
142
|
+
};
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### WildCards Rules
|
|
146
|
+
|
|
147
|
+
WildCards can also be validated.
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
let data = {
|
|
151
|
+
users: [{
|
|
152
|
+
name: 'John',
|
|
153
|
+
bio: {
|
|
154
|
+
age: 28,
|
|
155
|
+
education: {
|
|
156
|
+
primary: 'Elementary School',
|
|
157
|
+
secondary: 'Secondary School'
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}]
|
|
161
|
+
};
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
We could declare our validation rules as follows:
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
let rules = {
|
|
168
|
+
'users.*.name': 'required',
|
|
169
|
+
'users.*.bio.age': 'min:18',
|
|
170
|
+
'users.*.bio.education.primary': 'string',
|
|
171
|
+
'users.*.bio.education.secondary': 'string'
|
|
172
|
+
};
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Available Rules
|
|
176
|
+
|
|
177
|
+
Validation rules do not have an implicit 'required'. If a field is _undefined_ or an empty string, it will pass validation. If you want a validation to fail for undefined or '', use the _required_ rule.
|
|
178
|
+
|
|
179
|
+
#### accepted
|
|
180
|
+
|
|
181
|
+
The field under validation must be yes, on, 1 or true. This is useful for validating "Terms of Service" acceptance.
|
|
182
|
+
|
|
183
|
+
#### after:date
|
|
184
|
+
|
|
185
|
+
The field under validation must be after the given date.
|
|
186
|
+
|
|
187
|
+
#### after_or_equal:date
|
|
188
|
+
|
|
189
|
+
The field unter validation must be after or equal to the given field
|
|
190
|
+
|
|
191
|
+
#### alpha
|
|
192
|
+
|
|
193
|
+
The field under validation must be entirely alphabetic characters.
|
|
194
|
+
|
|
195
|
+
#### alpha_dash
|
|
196
|
+
|
|
197
|
+
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
|
|
198
|
+
|
|
199
|
+
#### alpha_num
|
|
200
|
+
|
|
201
|
+
The field under validation must be entirely alpha-numeric characters.
|
|
202
|
+
|
|
203
|
+
#### array
|
|
204
|
+
|
|
205
|
+
The field under validation must be an array.
|
|
206
|
+
|
|
207
|
+
#### before:date
|
|
208
|
+
|
|
209
|
+
The field under validation must be before the given date.
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
#### before_or_equal:date
|
|
213
|
+
|
|
214
|
+
The field under validation must be before or equal to the given date.
|
|
215
|
+
|
|
216
|
+
#### between:min,max
|
|
217
|
+
|
|
218
|
+
The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule.
|
|
219
|
+
|
|
220
|
+
#### boolean
|
|
221
|
+
|
|
222
|
+
The field under validation must be a boolean value of the form `true`, `false`, `0`, `1`, `'true'`, `'false'`, `'0'`, `'1'`,
|
|
223
|
+
|
|
224
|
+
#### confirmed
|
|
225
|
+
|
|
226
|
+
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
|
|
227
|
+
|
|
228
|
+
#### date
|
|
229
|
+
|
|
230
|
+
The field under validation must be a valid date format which is acceptable by Javascript's `Date` object.
|
|
231
|
+
|
|
232
|
+
#### digits:value
|
|
233
|
+
|
|
234
|
+
The field under validation must be numeric and must have an exact length of value.
|
|
235
|
+
|
|
236
|
+
#### digits_between:min,max
|
|
237
|
+
|
|
238
|
+
The field under validation must be numeric and must have length between given min and max.
|
|
239
|
+
|
|
240
|
+
#### different:attribute
|
|
241
|
+
|
|
242
|
+
The given field must be different than the field under validation.
|
|
243
|
+
|
|
244
|
+
#### email
|
|
245
|
+
|
|
246
|
+
The field under validation must be formatted as an e-mail address.
|
|
247
|
+
|
|
248
|
+
#### hex
|
|
249
|
+
The field under validation should be a hexadecimal format. Useful in combination with other rules, like `hex|size:6` for hex color code validation.
|
|
250
|
+
|
|
251
|
+
#### in:foo,bar,...
|
|
252
|
+
|
|
253
|
+
The field under validation must be included in the given list of values. The field can be an array or string.
|
|
254
|
+
|
|
255
|
+
#### integer
|
|
256
|
+
|
|
257
|
+
The field under validation must have an integer value.
|
|
258
|
+
|
|
259
|
+
#### max:value
|
|
260
|
+
|
|
261
|
+
Validate that an attribute is no greater than a given size
|
|
262
|
+
|
|
263
|
+
_Note: Maximum checks are inclusive._
|
|
264
|
+
|
|
265
|
+
#### min:value
|
|
266
|
+
|
|
267
|
+
Validate that an attribute is at least a given size.
|
|
268
|
+
|
|
269
|
+
_Note: Minimum checks are inclusive._
|
|
270
|
+
|
|
271
|
+
#### not_in:foo,bar,...
|
|
272
|
+
|
|
273
|
+
The field under validation must not be included in the given list of values.
|
|
274
|
+
|
|
275
|
+
#### numeric
|
|
276
|
+
|
|
277
|
+
Validate that an attribute is numeric. The string representation of a number will pass.
|
|
278
|
+
|
|
279
|
+
#### present
|
|
280
|
+
The field under validation must be present in the input data but can be empty.
|
|
281
|
+
|
|
282
|
+
#### required
|
|
283
|
+
|
|
284
|
+
Checks if the length of the String representation of the value is >
|
|
285
|
+
|
|
286
|
+
#### required_if:anotherfield,value
|
|
287
|
+
|
|
288
|
+
The field under validation must be present and not empty if the anotherfield field is equal to any value.
|
|
289
|
+
|
|
290
|
+
#### required_unless:anotherfield,value
|
|
291
|
+
|
|
292
|
+
The field under validation must be present and not empty unless the anotherfield field is equal to any value.
|
|
293
|
+
|
|
294
|
+
#### required_with:foo,bar,...
|
|
295
|
+
|
|
296
|
+
The field under validation must be present and not empty only if any of the other specified fields are present.
|
|
297
|
+
|
|
298
|
+
#### required_with_all:foo,bar,...
|
|
299
|
+
|
|
300
|
+
The field under validation must be present and not empty only if all of the other specified fields are present.
|
|
301
|
+
|
|
302
|
+
#### required_without:foo,bar,...
|
|
303
|
+
|
|
304
|
+
The field under validation must be present and not empty only when any of the other specified fields are not present.
|
|
305
|
+
|
|
306
|
+
#### required_without_all:foo,bar,...
|
|
307
|
+
|
|
308
|
+
The field under validation must be present and not empty only when all of the other specified fields are not present.
|
|
309
|
+
|
|
310
|
+
#### same:attribute
|
|
311
|
+
|
|
312
|
+
The given field must match the field under validation.
|
|
313
|
+
|
|
314
|
+
#### size:value
|
|
315
|
+
|
|
316
|
+
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value.
|
|
317
|
+
|
|
318
|
+
#### string
|
|
319
|
+
|
|
320
|
+
The field under validation must be a string.
|
|
321
|
+
|
|
322
|
+
#### url
|
|
323
|
+
|
|
324
|
+
Validate that an attribute has a valid URL format
|
|
325
|
+
|
|
326
|
+
#### regex:pattern
|
|
327
|
+
|
|
328
|
+
The field under validation must match the given regular expression.
|
|
329
|
+
|
|
330
|
+
**Note**: When using the ``regex`` pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
|
|
331
|
+
For each backward slash that you used in your regex pattern, you must escape each one with another backward slash.
|
|
332
|
+
|
|
333
|
+
#### Example 3 - Regex validation
|
|
334
|
+
|
|
335
|
+
```js
|
|
336
|
+
let validation = new Validator({
|
|
337
|
+
name: 'Doe',
|
|
338
|
+
salary: '10,000.00',
|
|
339
|
+
yearOfBirth: '1980'
|
|
340
|
+
}, {
|
|
341
|
+
name: 'required|size:3',
|
|
342
|
+
salary: ['required', 'regex:/^(?!0\\.00)\\d{1,3}(,\\d{3})*(\\.\\d\\d)?$/'],
|
|
343
|
+
yearOfBirth: ['required', 'regex:/^(19|20)[\\d]{2,2}$/']
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
validation.fails(); // false
|
|
347
|
+
validation.passes(); // true
|
|
348
|
+
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### Example 4 - Type Checking Validation
|
|
352
|
+
|
|
353
|
+
```js
|
|
354
|
+
let validation = new Validator({
|
|
355
|
+
age: 30,
|
|
356
|
+
name: ''
|
|
357
|
+
}, {
|
|
358
|
+
age: ['required', { 'in': [29, 30] }],
|
|
359
|
+
name: [{ required_if: ['age', 30] }]
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
validation.fails(); // true
|
|
363
|
+
validation.passes(); // false
|
|
364
|
+
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### Register Custom Validation Rules
|
|
368
|
+
|
|
369
|
+
```js
|
|
370
|
+
Validator.register(name, callbackFn, errorMessage);
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
__name__ {String} - The name of the rule.
|
|
374
|
+
|
|
375
|
+
__callbackFn__ {Function} - Returns a boolean to represent a successful or failed validation.
|
|
376
|
+
|
|
377
|
+
__errorMessage__ {String} - An optional string where you can specify a custom error message. _:attribute_ inside errorMessage will be replaced with the attribute name.
|
|
378
|
+
|
|
379
|
+
```js
|
|
380
|
+
Validator.register('telephone', function(value, requirement, attribute) { // requirement parameter defaults to null
|
|
381
|
+
return value.match(/^\d{3}-\d{3}-\d{4}$/);
|
|
382
|
+
}, 'The :attribute phone number is not in the format XXX-XXX-XXXX.');
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
### Asynchronous Validation
|
|
386
|
+
|
|
387
|
+
Register an asynchronous rule which accepts a `passes` callback:
|
|
388
|
+
|
|
389
|
+
```js
|
|
390
|
+
Validator.registerAsync('username_available', function(username, attribute, req, passes) {
|
|
391
|
+
// do your database/api checks here etc
|
|
392
|
+
// then call the `passes` method where appropriate:
|
|
393
|
+
passes(); // if username is available
|
|
394
|
+
passes(false, 'Username has already been taken.'); // if username is not available
|
|
395
|
+
});
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
Then call your validator using `checkAsync` passing `fails` and `passes` callbacks like so:
|
|
399
|
+
|
|
400
|
+
```js
|
|
401
|
+
let validator = new Validator({
|
|
402
|
+
username: 'test123'
|
|
403
|
+
}, {
|
|
404
|
+
username: 'required|min:3|username_available'
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
function passes() {
|
|
408
|
+
// Validation passed
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function fails() {
|
|
412
|
+
validator.errors.first('username');
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
validator.checkAsync(passes, fails);
|
|
416
|
+
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### Error Messages
|
|
420
|
+
|
|
421
|
+
This constructor will automatically generate error messages for validation rules that failed.
|
|
422
|
+
|
|
423
|
+
If there are errors, the Validator instance will have its __errors__ property object populated with the error messages for all failing attributes. The methods and properties on the __errors__ property object are:
|
|
424
|
+
|
|
425
|
+
#### .first(attribute)
|
|
426
|
+
|
|
427
|
+
returns the first error message for an attribute, false otherwise
|
|
428
|
+
|
|
429
|
+
#### .get(attribute)
|
|
430
|
+
|
|
431
|
+
returns an array of error messages for an attribute, or an empty array if there are no errors
|
|
432
|
+
|
|
433
|
+
#### .all()
|
|
434
|
+
|
|
435
|
+
returns an object containing all error messages for all failing attributes
|
|
436
|
+
|
|
437
|
+
#### .has(attribute)
|
|
438
|
+
|
|
439
|
+
returns true if error messages exist for an attribute, false otherwise
|
|
440
|
+
|
|
441
|
+
#### .errorCount
|
|
442
|
+
|
|
443
|
+
the number of validation errors
|
|
444
|
+
|
|
445
|
+
```js
|
|
446
|
+
let validation = new Validator(input, rules);
|
|
447
|
+
validation.errors.first('email'); // returns first error message for email attribute
|
|
448
|
+
validator.errors.get('email'); // returns an array of error messages for the email attribute
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### Custom Error Messages
|
|
452
|
+
|
|
453
|
+
If you need a specific error message and you don't want to override the default one, you can pass an override as the third argument to the Validator object, just like with [Laravel](http://laravel.com/docs/validation#custom-error-messages).
|
|
454
|
+
|
|
455
|
+
```js
|
|
456
|
+
let input = {
|
|
457
|
+
name: ''
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
let rules = {
|
|
461
|
+
name : 'required'
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
let validation = new Validator(input, rules, { required: 'You forgot to give a :attribute' });
|
|
465
|
+
validation.passes();
|
|
466
|
+
validation.errors.first('name'); // returns 'You forgot to give a name'
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
Some of the validators have string and numeric versions. You can change them too.
|
|
470
|
+
|
|
471
|
+
```js
|
|
472
|
+
let input = {
|
|
473
|
+
username: 'myusernameistoolong'
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
let rules = {
|
|
477
|
+
username : 'max:16'
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
let validation = new Validator(input, rules, {
|
|
481
|
+
max: {
|
|
482
|
+
string: 'The :attribute is too long. Max length is :max.'
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
validation.passes();
|
|
486
|
+
validation.errors.first('username'); // returns 'The username is too long. Max length is 16.'
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
You can even provide error messages on a per attribute basis! Just set the message's key to 'validator.attribute'
|
|
490
|
+
|
|
491
|
+
```js
|
|
492
|
+
let input = { name: '', email: '' };
|
|
493
|
+
let rules = { name : 'required', email : 'required' };
|
|
494
|
+
|
|
495
|
+
let validation = new Validator(input, rules, {
|
|
496
|
+
"required.email": "Without an :attribute we can't reach you!"
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
validation.passes();
|
|
500
|
+
validation.errors.first('name'); // returns 'The name field is required.'
|
|
501
|
+
validation.errors.first('email'); // returns 'Without an email we can\'t reach you!'
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
### Custom Attribute Names
|
|
505
|
+
|
|
506
|
+
To display a custom "friendly" attribute name in error messages, use `.setAttributeNames()`
|
|
507
|
+
|
|
508
|
+
```js
|
|
509
|
+
let validator = new Validator({ name: '' }, { name: 'required' });
|
|
510
|
+
validator.setAttributeNames({ name: 'custom_name' });
|
|
511
|
+
if (validator.fails()) {
|
|
512
|
+
validator.errors.first('name'); // "The custom_name field is required."
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
Alternatively you can supply global custom attribute names in your lang with the `attributes` property.
|
|
517
|
+
|
|
518
|
+
You can also configure a custom attribute formatter:
|
|
519
|
+
|
|
520
|
+
```js
|
|
521
|
+
// Configure global formatter.
|
|
522
|
+
Validator.setAttributeFormatter(function(attribute) {
|
|
523
|
+
return attribute.replace(/_/g, ' ');
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// Or configure formatter for particular instance.
|
|
527
|
+
let validator = new Validator({ first_name: '' }, { first_name: 'required' });
|
|
528
|
+
validator.setAttributeFormatter(function(attribute) {
|
|
529
|
+
return attribute.replace(/_/g, ' ');
|
|
530
|
+
});
|
|
531
|
+
if (validator.fails()) {
|
|
532
|
+
console.log(validator.errors.first('first_name')); // The first name field is required.
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
Note: by default all _ characters will be replaced with spaces.
|
|
537
|
+
|
|
538
|
+
### Language Support
|
|
539
|
+
|
|
540
|
+
Error messages are in English by default. To include another language in the browser, reference the language file in a script tag and call `Validator.useLang('lang_code')`.
|
|
541
|
+
|
|
542
|
+
```html
|
|
543
|
+
<script src="dist/validator.js"></script>
|
|
544
|
+
<script src="dist/lang/ru.js"></script>
|
|
545
|
+
<script>
|
|
546
|
+
Validator.useLang('es');
|
|
547
|
+
</script>
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
In Node, it will automatically pickup on the language source files.
|
|
551
|
+
|
|
552
|
+
```js
|
|
553
|
+
let Validator = require('validatorjs');
|
|
554
|
+
Validator.useLang('ru');
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
If you don't see support for your language, please add one to `src/lang`!
|
|
558
|
+
|
|
559
|
+
You can also add your own custom language by calling `setMessages`:
|
|
560
|
+
|
|
561
|
+
```js
|
|
562
|
+
Validator.setMessages('lang_code', {
|
|
563
|
+
required: 'The :attribute field is required.'
|
|
564
|
+
});
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
Get the raw object of messages for the given language:
|
|
568
|
+
|
|
569
|
+
```js
|
|
570
|
+
Validator.getMessages('lang_code');
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
Switch the default language used by the validator:
|
|
574
|
+
|
|
575
|
+
```js
|
|
576
|
+
Validator.useLang('lang_code');
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
Get the default language being used:
|
|
580
|
+
|
|
581
|
+
```js
|
|
582
|
+
Validator.getDefaultLang(); // returns e.g. 'en'
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
Override default messages for language:
|
|
586
|
+
|
|
587
|
+
```js
|
|
588
|
+
let messages = Validator.getMessages('en');
|
|
589
|
+
messages.required = 'Whoops, :attribute field is required.';
|
|
590
|
+
Validator.setMessages('en', messages);
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
### License
|
|
594
|
+
|
|
595
|
+
Copyright © 2012-2019 David Tang
|
|
596
|
+
Released under the MIT license
|
|
597
|
+
|
|
598
|
+
### Credits
|
|
599
|
+
|
|
600
|
+
validatorjs created by David Tang
|
|
601
|
+
validatorjs maintained by Mike Erickson and Contributors
|
|
602
|
+
|
|
603
|
+
E-Mail: [codedungeon@gmail.com](mailto:codedungeon@gmail.com)
|
|
604
|
+
|
|
605
|
+
Twitter: [@codedungeon](http://twitter.com/codedungeon)
|
|
606
|
+
|
|
607
|
+
Website: [codedungeon.io](http://codedungeon.io)
|