You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// resolve if value does not exists, value existence
77
+
// should be taken care by required rule.
78
+
if(!fieldValue) {
79
+
returnresolve('validation skipped');
80
+
}
81
+
82
+
// checking for username inside database
83
+
User
84
+
.where('username', fieldValue)
85
+
.then(function (result) {
86
+
if(result){
87
+
reject(message);
88
+
}else{
89
+
resolve('username does not exists');
90
+
}
91
+
});
92
+
.catch(resolve);
93
+
94
+
});
95
+
96
+
};
97
+
```
98
+
99
+
- data - It is the actual data object passed to validate method.
100
+
- field - Field is a string value of field under validation.
101
+
- message - Error message to return.
102
+
- args - An array of values your rule is expecting, it may be empty depending upon your rule expectations. For example min:4 will have args array as [4].
103
+
104
+
```javascript
105
+
inputCheck.extend('unique', unique, 'Field should be unique')
106
+
```
107
+
108
+
109
+
110
+
10
111
## Available Validation Rules
11
112
Below is a list of all available validation rules and their function:
12
113
@@ -23,7 +124,7 @@ The field under validation must have a valid A or AAAA record.
23
124
after:(date|time)
24
125
----
25
126
The field under validation must be a value after a given date or time.
26
-
```
127
+
```javascript
27
128
construles= {
28
129
'createdAt':'date|after:2016-11-11',
29
130
'time':'time|after:14:00:00'
@@ -58,7 +159,7 @@ The field under validation must be a array.
58
159
before:date
59
160
----
60
161
The field under validation must be a value preceding the given date or time.
61
-
```
162
+
```javascript
62
163
construles= {
63
164
'createdAt':'date|before:2016-11-11',
64
165
'time':'time|before:14:00:00'
@@ -128,14 +229,14 @@ in:foo,bar,... (in_array)
128
229
----
129
230
The field under validation must be included in the given list of values. Since this rule often requires you to implode an array.
130
231
131
-
```
232
+
```javascript
132
233
construles= {
133
234
'company':'string|in:google,yahoo,facebook',
134
235
}
135
236
```
136
237
137
238
in array example:
138
-
```
239
+
```javascript
139
240
construles= {
140
241
'company.*':'in:google,yahoo,facebook',
141
242
}
@@ -246,7 +347,7 @@ The field under validation must be present and not empty only when all of the ot
246
347
The given field must match the field under validation.
247
348
248
349
249
-
~~size:value~~
350
+
size:value
250
351
----
251
352
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. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.
252
353
@@ -314,30 +415,37 @@ you may be interested in the following modules:
When you using above modules, you could get the file or image information.
316
417
Then, you must to transfer information below before use file validation:
317
-
```
318
-
const file = {
319
-
mimetype: 'image/png', // for mime type or other valiations
320
-
path: '/var/tmp/xxxx.png', // for dimensions
418
+
```javascript
419
+
construles= {
420
+
file:'file|image'
321
421
}
322
-
```
323
422
324
-
#### Image
325
-
Before using validation, you need to download and install [GraphicsMagick](http://www.graphicsmagick.org/) or [ImageMagick](http://www.imagemagick.org/). In Mac OS X, you can simply use [Homebrew](http://mxcl.github.io/homebrew/) and do:
326
-
```
327
-
brew install imagemagick
328
-
brew install graphicsmagick
329
-
```
330
-
More details: https://github.com/aheckmann/gm#getting-started
423
+
constdata= {
424
+
file: {
425
+
mimetype:'image/png', // for mime type validation
426
+
path:'/var/tmp/xxxx.png', // for dimensions
427
+
}
428
+
}
331
429
430
+
inputCheck
431
+
.validate(data, rules)
432
+
.then(function () {
433
+
// validation passed
434
+
})
435
+
.catch(function (errors) {
436
+
// validation failed
437
+
})
332
438
333
-
~~file~~
439
+
```
440
+
441
+
file
334
442
----
335
443
The field under validation must be a successfully uploaded file.
336
444
337
-
~~image~~
445
+
image
338
446
----
339
447
The file under validation must be an image (jpeg, png, bmp, gif, or svg)
340
-
```
448
+
```javascript
341
449
construles= {
342
450
'file':'image',
343
451
}
@@ -354,75 +462,14 @@ The file under validation must have a MIME type corresponding to one of the list
354
462
dimensions
355
463
----
356
464
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.
364
472
365
-
## Custom messages
366
-
```
367
-
const messages = {
368
-
required: 'This field is required to complete the registration process.'
369
-
}
370
-
371
-
inputCheck
372
-
.validate(data, rules, messages)
373
-
.then(function () {
374
-
// validation passed
375
-
})
376
-
.catch(function (errors) {
377
-
// validation failed
378
-
})
379
-
```
380
-
381
-
382
-
383
-
## Custom Validation
384
-
```
385
-
const _ = require('lodash');
386
-
387
-
const unique = function (data, field, message, args) {
388
-
389
-
return new Promise(function (resolve, reject) {
390
-
391
-
// get value of field under validation
392
-
const fieldValue = _.get(data, field);
393
-
394
-
// resolve if value does not exists, value existence
395
-
// should be taken care by required rule.
396
-
if(!fieldValue) {
397
-
return resolve('validation skipped');
398
-
}
399
-
400
-
// checking for username inside database
401
-
User
402
-
.where('username', fieldValue)
403
-
.then(function (result) {
404
-
if(result){
405
-
reject(message);
406
-
}else{
407
-
resolve('username does not exists');
408
-
}
409
-
});
410
-
.catch(resolve);
411
-
412
-
});
413
-
414
-
};
415
-
```
416
-
417
-
- data - It is the actual data object passed to validate method.
418
-
- field - Field is a string value of field under validation.
419
-
- message - Error message to return.
420
-
- args - An array of values your rule is expecting, it may be empty depending upon your rule expectations. For example min:4 will have args array as [4].
421
-
422
-
```
423
-
inputCheck.extend('unique', unique, 'Field should be unique')
424
-
```
425
-
426
473
## License
427
474
428
475
[The MIT License](http://opensource.org/licenses/MIT)
0 commit comments