https://github.com/scopdrag/express-validators
Express framework json object(req) validator.
https://github.com/scopdrag/express-validators
bluebird-promise expressjs json promise
Last synced: 5 months ago
JSON representation
Express framework json object(req) validator.
- Host: GitHub
- URL: https://github.com/scopdrag/express-validators
- Owner: scopdrag
- License: mit
- Created: 2016-09-18T15:58:11.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2025-06-12T05:47:31.000Z (about 1 year ago)
- Last Synced: 2025-09-23T23:53:04.199Z (9 months ago)
- Topics: bluebird-promise, expressjs, json, promise
- Language: JavaScript
- Size: 28.3 KB
- Stars: 5
- Watchers: 0
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# validator-for-express
[](https://www.npmjs.com/package/express-validators)
[](https://www.npmjs.com/package/express-validators)
[](https://www.npmjs.com/package/express-validators)
[](https://www.npmjs.com/package/express-validators)
Nodejs (Express js) server side Validator.
This package offer very simple and easy way to validate any type of json object (request json object) for expressjs.
This validator also work with bluebird promise as well as async method callback.
Expressjs framework json object(request) validator package.
Install using NPM
```bash
npm i validator-for-express --save
```
require in your (expressjs) route file.
```javascript
var Validators = require('validator-for-express')
```
or if you using bluebird promise
```javascript
var Promise = require('bluebird');
var Validators = require('validator-for-express');
Promise.promisifyAll(Validators);
```
Or
```javascript
var Validators = Promise.promisifyAll(require('validator-for-express'));
```
## Usage
e.g: Json object need to be validated.
```javascript
var Obj = {
currentDate: '2016-09-16',
afterDate: '2016-09-17',
beforeDateValue: '2016-09-18',
alphaValue: 12112,
alpha_numValue: 'test_1-2',
arrayValue: [],
betweenValue: 40,
booleanValue: true,
confirmedValue: "233232",
confirmedValue1: "233232ss",
dateValue: "2015-03-25ss",
notEqualValue: 12112,
numericValue: "12112ss",
lengthValue: "12112ss",
emailValue: "12112ss@gmail.com",
inValue: "23",
ipValue: "23",
notInValue: "23",
regexValue: "",
requiredValue: "",
requiredIfValue: "",
stringValue: 7797,
urlValue: 7797,
amountValue: "7797.00ss"
};
```
You can specify the rules for each index in json object. also you can specify multiple rules for a particular index by adding "|" after first rule.
```javascript
var rules = {
"currentDate": "after:afterDate|date",
"beforeDateValue": "before:afterDate",
"alphaValue": "alpha",
"alpha_numValue": "alpha_num",
"arrayValue": "array",
"betweenValue": "between:10,30",
"booleanValue": "boolean",
"confirmedValue": "confirmed:confirmedValue1",
"dateValue": "date",
"notEqualValue": "notEqual:alphaValue",
"numericValue": "numeric",
"lengthValue": "length:10,30",
"inValue": "in:23,34",
"ipValue": "ip",
"notInValue": "notIn:23,35",
"issetValue": "isset",
"regexValue": "regex:pattern",
"requiredValue": "required",
"requiredIfValue": "requiredIf:alphaValue,12112",
"stringValue": "string",
"urlValue": "url",
"amountValue": "amount"
};
```
You can define messages for each rule of an index. if don't specify the messages it will return default messages. It is not mendatory.
```javascript
var messages = {
emailValue:{
email: "Please enter a valid email address."
}
};
```
Call to validator meathod
```javascript
Validators.validator(Obj, rules, messages, function (err, validated) {
if (validated.fails) {
console.log(validated.getErrors)
}
})
```
Or If using promise.
```javascript
Validators.validatorAsync(Obj, rules, messages)
.then(function (validated) {
if (validated.fails) {
console.log(validated.getErrors)
}
})
.catch(function(err){
console.error(err);
})
```
`fails` index will be true if there is an error. Otherwise it will be false.
`getErrors` index contains error field as a key and array of error messages for that field.
e.g:
```javascript
{ currentDate: [ 'CurrentDate must be later date then afterDate' ],
beforeDateValue: [ 'BeforeDateValue must be before date then afterDate' ],
alphaValue: [ 'AlphaValue must be entirely alphabetic characters.' ],
alpha_numValue: [ 'Alpha numValue must be alpha-numeric characters.' ],
betweenValue: [ 'BetweenValue value must be in 10,30' ],
confirmedValue: [ 'ConfirmedValue value must be same as ConfirmedValue1' ],
dateValue: [ 'DateValue must be a valid date.' ],
notEqualValue: [ 'NotEqualValue value must not be equal to AlphaValue' ],
numericValue: [ 'NumericValue must be a numeric value.' ],
lengthValue: [ 'LengthValue should be 10 to 30 character long.' ],
emailValue: [ 'Please enter a valid email address.' ],
ipValue: [ 'IpValue must be a valid ip address.' ],
notInValue: [ 'NotInValue must not be in 23,35.' ],
issetValue: [ 'IssetValue does not exists.' ],
regexValue: [ 'RegexValue found to be in invalid pattern.' ],
requiredValue: [ 'RequiredValue field is required.' ],
requiredIfValue: [ 'RequiredIfValue field is required.' ],
stringValue: [ 'StringValue must be a string.' ],
urlValue: [ 'UrlValue must be a valid url.' ],
amountValue: [ 'AmountValue must be an amount value.' ] }
```
