Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
validator
Advanced tools
The validator npm package is a library of string validators and sanitizers. It provides a variety of functions to validate and sanitize strings, such as checking if a string is in a certain format (e.g., email, URL) or transforming strings to ensure they are safe for use in different contexts.
Email Validation
Checks if the input string is an email.
const validator = require('validator');
console.log(validator.isEmail('test@example.com')); // true
URL Validation
Checks if the input string is a URL.
const validator = require('validator');
console.log(validator.isURL('https://www.example.com')); // true
Sanitizing Strings
Escapes HTML characters in the input string to prevent XSS attacks.
const validator = require('validator');
console.log(validator.escape('<script>alert("xss")</script>')); // '<script>alert("xss")</script>'
Checking String Length
Checks if the input string's length falls within a specified range.
const validator = require('validator');
console.log(validator.isLength('Hello', {min: 2, max: 10})); // true
Blacklisting Characters
Removes specified characters from the input string.
const validator = require('validator');
console.log(validator.blacklist('abc123', '123')); // 'abc'
Joi is a powerful schema description language and data validator for JavaScript. It allows for a more detailed and structured validation process compared to validator, including the ability to create custom validation schemas.
Yup is a JavaScript schema builder for value parsing and validation. It uses a schema-based approach similar to Joi and can be integrated with form libraries like Formik. It is more focused on object schema validation.
Class-validator works with classes and decorators to validate that the properties of an object conform to specified rules. It is typically used with TypeScript and integrates well with class-based frameworks like TypeORM.
Express-validator is a set of express.js middlewares that wraps validator.js functions. It is specifically designed for use with the Express web application framework and allows for easy integration of validation into the request processing pipeline.
node-validator is a library of string validation, filtering and sanitization methods.
To install node-validator, use npm:
$ npm install validator
To use the library in the browser, include validator-min.js
var check = require('validator').check,
sanitize = require('validator').sanitize
//Validate
check('test@email.com').len(6, 64).isEmail(); //Methods are chainable
check('abc').isInt(); //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt(); //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);
//Set a message per validator
check('foo', {
isNumeric: 'This is not a number',
contains: 'The value doesn\'t have a 0 in it'
}).isNumeric().contains('0');
//Referencing validator args from the message
check('foo', 'The message needs to be between %1 and %2 characters long (you passed "%0")').len(2, 6);
//Sanitize / Filter
var int = sanitize('0123').toInt(); //123
var bool = sanitize('true').toBoolean(); //true
var str = sanitize(' \t\r hello \n').trim(); //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a'); //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode(); //'<a>'
Often it's more desirable to check or automatically sanitize parameters by name (rather than the actual string). See this gist for instructions on binding the library to the request
prototype.
If you are using the express.js framework you can use the express-validator middleware to seamlessly integrate node-validator.
Example http://localhost:8080/?zip=12345&foo=1&textarea=large_string
get('/', function (req, res) {
req.onValidationError(function (msg) {
//Redirect the user with error 'msg'
});
//Validate user input
req.check('zip', 'Please enter a valid ZIP code').len(4,5).isInt();
req.check('email', 'Please enter a valid email').len(6,64).isEmail();
req.checkHeader('referer').contains('localhost');
//Sanitize user input
req.sanitize('textarea').xss();
req.sanitize('foo').toBoolean();
//etc.
});
This library validates strings only. If you pass something that's not a string as input it will be coerced to a string using the following rules:
toString
property? Call input.toString()
null
, undefined
, or NaN
? Replace with an empty string'' + input
is() //Alias for regex()
not() //Alias for notRegex()
isEmail()
isUrl() //Accepts http, https, ftp
isIP() //Combines isIPv4 and isIPv6
isIPv4()
isIPv6()
isAlpha()
isAlphanumeric()
isNumeric()
isHexadecimal()
isHexColor() //Accepts valid hexcolors with or without # prefix
isInt() //isNumeric accepts zero padded numbers, e.g. '001', isInt doesn't
isLowercase()
isUppercase()
isDecimal()
isFloat() //Alias for isDecimal
notNull() //Check if length is 0
isNull()
notEmpty() //Not just whitespace (input.trim().length !== 0)
equals(equals)
contains(str)
notContains(str)
regex(pattern, modifiers) //Usage: regex(/[a-z]/i) or regex('[a-z]','i')
notRegex(pattern, modifiers)
len(min, max) //max is optional
isUUID(version) //Version can be 3, 4 or 5 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier
isUUIDv3() //Alias for isUUID(3)
isUUIDv4() //Alias for isUUID(4)
isUUIDv5() //Alias for isUUID(5)
isDate() //Uses Date.parse() - regex is probably a better choice
isAfter(date) //Argument is optional and defaults to today. Comparison is non-inclusive
isBefore(date) //Argument is optional and defaults to today. Comparison is non-inclusive
isIn(options) //Accepts an array or string
notIn(options)
max(val)
min(val)
isCreditCard() //Will work against Visa, MasterCard, American Express, Discover, Diners Club, and JCB card numbering formats
trim(chars) //Trim optional `chars`, default is to trim whitespace (\r\n\t )
ltrim(chars)
rtrim(chars)
ifNull(replace)
toFloat()
toInt()
toBoolean() //True unless str = '0', 'false', or str.length == 0
toBooleanStrict() //False unless str = '1' or 'true'
entityDecode() //Decode HTML entities
entityEncode()
escape() //Escape &, <, >, and "
xss() //Remove common XSS attack vectors from user-supplied HTML
xss(true) //Remove common XSS attack vectors from images
When adding to the Validator prototype, use this.str
to access the string and this.error(this.msg || default_msg)
when the string is invalid
var Validator = require('validator').Validator;
Validator.prototype.contains = function(str) {
if (!~this.str.indexOf(str)) {
this.error(this.msg || this.str + ' does not contain ' + str);
}
return this; //Allow method chaining
}
When adding to the Filter (sanitize) prototype, use this.str
to access the string and this.modify(new_str)
to update it
var Filter = require('validator').Filter;
Filter.prototype.removeNumbers = function() {
this.modify(this.str.replace(/[0-9]+/g, ''));
return this.str;
}
By default, the validation methods throw an exception when a check fails
try {
check('abc').notNull().isInt()
} catch (e) {
console.log(e.message); //Invalid integer
}
To set a custom error message, set the second param of check()
try {
check('abc', 'Please enter a valid integer').notNull().isInt()
} catch (e) {
console.log(e.message); //Please enter a valid integer
}
To attach a custom error handler, set the error
method of the validator instance
var Validator = require('validator').Validator;
var v = new Validator();
v.error = function(msg) {
console.log('Fail');
}
v.check('abc').isInt(); //'Fail'
You might want to collect errors instead of throwing each time
Validator.prototype.error = function (msg) {
this._errors.push(msg);
return this;
}
Validator.prototype.getErrors = function () {
return this._errors;
}
var validator = new Validator();
validator.check('abc').isEmail();
validator.check('hello').len(10,30);
var errors = validator.getErrors(); // ['Invalid email', 'String is too small']
(MIT License)
Copyright (c) 2010 Chris O'Hara cohara87@gmail.com
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
String validation and sanitization
The npm package validator receives a total of 9,357,766 weekly downloads. As such, validator popularity was classified as popular.
We found that validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.