Skip to content

Commit 78ec024

Browse files
author
Mark
committed
Update content & version
1 parent 68c86a6 commit 78ec024

File tree

2 files changed

+130
-83
lines changed

2 files changed

+130
-83
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "input-check",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"description": "input validator for nodejs",
55
"main": "index.js",
66
"scripts": {

readme.md

Lines changed: 129 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,107 @@ Validator for nodejs and web.
77
[![NPM](https://nodei.co/npm/input-check.png?downloads=true)](https://nodei.co/npm/input-check/)
88

99

10+
11+
## Installation
12+
13+
Installing requires node 4.0 or greater with npm installed.
14+
15+
```javascript
16+
npm install --save input-check
17+
```
18+
19+
## Basics
20+
21+
```javascript
22+
const inputCheck = require('input-check')
23+
```
24+
25+
#### validate (data, rules, [messages])
26+
Validate method will run the validation cycle, which gets terminated on the first error.
27+
28+
```javascript
29+
30+
const rules = {
31+
username: 'required'
32+
}
33+
34+
const data = {
35+
username: null
36+
}
37+
38+
inputCheck
39+
.validate(data, rules)
40+
.then(function () {
41+
// validation passed
42+
})
43+
.catch(function (errors) {
44+
// validation failed
45+
})
46+
```
47+
48+
49+
## Custom messages
50+
```javascript
51+
const messages = {
52+
required: 'This field is required to complete the registration process.'
53+
}
54+
55+
inputCheck
56+
.validate(data, rules, messages)
57+
.then(function () {
58+
// validation passed
59+
})
60+
.catch(function (errors) {
61+
// validation failed
62+
})
63+
```
64+
65+
## Custom Validation
66+
```javascript
67+
const _ = require('lodash');
68+
69+
const unique = function (data, field, message, args) {
70+
71+
return new Promise(function (resolve, reject) {
72+
73+
// get value of field under validation
74+
const fieldValue = _.get(data, field);
75+
76+
// resolve if value does not exists, value existence
77+
// should be taken care by required rule.
78+
if(!fieldValue) {
79+
return resolve('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+
10111
## Available Validation Rules
11112
Below is a list of all available validation rules and their function:
12113

@@ -23,7 +124,7 @@ The field under validation must have a valid A or AAAA record.
23124
after:(date|time)
24125
----
25126
The field under validation must be a value after a given date or time.
26-
```
127+
```javascript
27128
const rules = {
28129
'createdAt' : 'date|after:2016-11-11',
29130
'time' : 'time|after:14:00:00'
@@ -58,7 +159,7 @@ The field under validation must be a array.
58159
before:date
59160
----
60161
The field under validation must be a value preceding the given date or time.
61-
```
162+
```javascript
62163
const rules = {
63164
'createdAt' : 'date|before:2016-11-11',
64165
'time' : 'time|before:14:00:00'
@@ -128,14 +229,14 @@ in:foo,bar,... (in_array)
128229
----
129230
The field under validation must be included in the given list of values. Since this rule often requires you to implode an array.
130231

131-
```
232+
```javascript
132233
const rules = {
133234
'company' : 'string|in:google,yahoo,facebook',
134235
}
135236
```
136237

137238
in array example:
138-
```
239+
```javascript
139240
const rules = {
140241
'company.*' : 'in:google,yahoo,facebook',
141242
}
@@ -246,7 +347,7 @@ The field under validation must be present and not empty only when all of the ot
246347
The given field must match the field under validation.
247348

248349

249-
~~size:value~~
350+
size:value
250351
----
251352
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.
252353

@@ -314,30 +415,37 @@ you may be interested in the following modules:
314415
* [multer](https://www.npmjs.org/package/multer#readme)
315416
When you using above modules, you could get the file or image information.
316417
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+
const rules = {
420+
file: 'file|image'
321421
}
322-
```
323422

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+
const data = {
424+
file: {
425+
mimetype: 'image/png', // for mime type validation
426+
path: '/var/tmp/xxxx.png', // for dimensions
427+
}
428+
}
331429

430+
inputCheck
431+
.validate(data, rules)
432+
.then(function () {
433+
// validation passed
434+
})
435+
.catch(function (errors) {
436+
// validation failed
437+
})
332438

333-
~~file~~
439+
```
440+
441+
file
334442
----
335443
The field under validation must be a successfully uploaded file.
336444

337-
~~image~~
445+
image
338446
----
339447
The file under validation must be an image (jpeg, png, bmp, gif, or svg)
340-
```
448+
```javascript
341449
const rules = {
342450
'file' : 'image',
343451
}
@@ -354,75 +462,14 @@ The file under validation must have a MIME type corresponding to one of the list
354462
dimensions
355463
----
356464
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
357-
```
465+
```javascript
358466
const rules = {
359467
'image' : 'dimensions:min_width=100,min_height=200,ratio=3/2',
360468
}
361469
```
362470

363471
Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.
364472

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-
426473
## License
427474

428475
[The MIT License](http://opensource.org/licenses/MIT)

0 commit comments

Comments
 (0)