
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
node-validator
Advanced tools
A simple, extensible object property validator for node.js. Supports express.js.
node-validator is a simple, extensible object property validator for node.js
NEW Check out my second generation validator better-validator. It gives more options around usage and formatting as well as typescript support.
It includes direct support for express.js, and can be used as express middleware to automatically validating request body content.
Now also supports koa.js as middleware.
var validator = require('node-validator');
var checkChild = validator.isObject()
.withRequired('prop', validator.isString({ regex: /^[abc]+$/ }));
var check = validator.isObject()
.withRequired('_id', validator.isString({ regex: /^[abc]+$/ }))
.withOptional('date', validator.isIsoDate())
.withOptional('children', validator.isArray(checkChild, {min: 1}));
var toValidate = {
_id: 'abababa',
date: '2013-10-24',
children: [{
prop: 'zxzx'
}]
};
validator.run(check, toValidate, function(errorCount, errors) {
// will return:
// errorCount=1
// errors=[{"parameter":"children[0].prop","value":"zxzx","message":"Invalid value. Value must match required pattern."}]
});
Or using express.js
app.post('/', [validator.express(check), function(req, res) {
// ...
}
Or using koa.js (use either koaBody(), koaQuery or koaParams)
route.post('/', validator.koaBody(check), otherFunction);
If the body content does not pass the given validation check, the validator will return.
400 Bad Request
{
"errors": [
{
"parameter": "children[0].prop",
"value": "zxzx",
"message": "Invalid value. Value must match required pattern."
}
]
}
$ npm install node-validator
Following are the build-in validators. You may also use your own, see section below.
Used to validate that the item under test is an object, and to check it's properties. This is often the root validator.
Property requirements are chained to the isObject validator.
var check = validator.isObject()
.withRequired('requiredProperty', propertyValidator1)
.withOptional('optionalProperty', propertyValidator2);
If any properties are present in the object under test that are not listed, this will fail the validation.
The property validators may be any other validator, including isObject, or may omitted to allow any value.
Similar to isObject, however does not fail for unexpected properties.
Makes sure the item is of type string, also can check the value against a regular expression.
var check = validator.isString();
or
var check = validator.isString({regex: /[0-9A-Fa-f]+/, message: 'Invalid value. Value must be hex.'});
Makes sure the item is of type string or equal to null, also can check the value against a regular expression.
var check = validator.isStringOrNull();
or
var check = validator.isArray(validator.isStringOrNull({regex: /[0-9A-Fa-f]+/, message: 'Invalid value. Value must be hex.'}));
Makes sure the item is a number, also can specify minimum and maximum values.
var check = validator.isNumber();
or
var check = validator.isNumber({min: 0, max: 78});
Makes sure the item is a whole number (integer), also can specify minimum and maximum values.
var check = validator.isInteger();
or
var check = validator.isInteger({min: 0, max: 78});
Checks for a Date object or a string that is moment.js can parse.
var check = validator.isDate();
Optionally, the moment.js format can be passed through to specify a particular format
var check = validator.isDate();
or
var check = validator.isDate({format: 'LT'});
A shortcut for validator.isDate({format: 'YYYY-MM-DD'})
Makes sure that the item is of type array, and validates the items. Also can specify minimum and maximum length of the array.
var check = validator.isArray(validator.isDate());
or
var check = validator.isArray(validator.isDate(), {min: 3});
You may use your own validators. All that is required is a function that meets the below requirements.
function myValidator(value, onError) {
...
}
Where value is the item under test, and onError is a function to call with any validation errors. It has the signature:
function onError(message,propertyName,propertyValue) {
...
}
Example: to make sure that there can not be both properties foo and bar
function validateFooXorBar(value, onError) {
if (value.foo !== undefined && value.bar !== undefined) {
onError('both foo and bar may not be defined', 'foo|bar', null);
}
}
(The MIT License)
Copyright (c) 2013
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A simple, extensible object property validator for node.js. Supports express.js.
The npm package node-validator receives a total of 344 weekly downloads. As such, node-validator popularity was classified as not popular.
We found that node-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.