blob: 71675fec2de8f8816813438063ed8b638ff2ef35 [file] [log] [blame]
{
"name": "validator",
"description": "Data validation, filtering and sanitization for node.js",
"version": "0.4.1",
"homepage": "http://github.com/chriso/node-validator",
"keywords": [
"validator",
"validation",
"assert",
"params",
"sanitization",
"xss",
"entities",
"sanitize",
"sanitisation",
"input"
],
"author": {
"name": "Chris O'Hara",
"email": "cohara87@gmail.com"
},
"main": "./lib",
"directories": {
"lib": "./lib"
},
"bugs": {
"url": "http://github.com/chriso/node-validator/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/chriso/node-validator.git"
},
"contributors": [
{
"name": "PING"
},
{
"name": "Dan VerWeire"
},
{
"name": "Branko Vukelic"
},
{
"name": "Mark Engel"
}
],
"engines": {
"node": ">=0.2.2"
},
"licenses": [
{
"type": "MIT",
"url": "http://github.com/chriso/node-validator/raw/master/LICENSE"
}
],
"readme": "**node-validator is a library of string validation, filtering and sanitization methods.**\n\nTo install node-validator, use [npm](http://github.com/isaacs/npm):\n\n```bash\n$ npm install validator\n```\n\nTo use the library in the browser, include `validator-min.js`\n\n## Example\n\n```javascript\nvar check = require('validator').check,\n sanitize = require('validator').sanitize\n\n//Validate\ncheck('test@email.com').len(6, 64).isEmail(); //Methods are chainable\ncheck('abc').isInt(); //Throws 'Invalid integer'\ncheck('abc', 'Please enter a number').isInt(); //Throws 'Please enter a number'\ncheck('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);\n\n//Sanitize / Filter\nvar int = sanitize('0123').toInt(); //123\nvar bool = sanitize('true').toBoolean(); //true\nvar str = sanitize(' \\s\\t\\r hello \\n').trim(); //'hello'\nvar str = sanitize('aaaaaaaaab').ltrim('a'); //'b'\nvar str = sanitize(large_input_str).xss();\nvar str = sanitize('&lt;a&gt;').entityDecode(); //'<a>'\n```\n\n## Web development\n\nOften it's more desirable to check or automatically sanitize parameters by name (rather than the actual string). See [this gist](https://gist.github.com/752126) for instructions on binding the library to the `request` prototype.\n\nIf you are using the [express.js framework](https://github.com/visionmedia/express) you can use the [express-validator middleware](https://github.com/ctavan/express-validator) to seamlessly integrate node-validator.\n\nExample `http://localhost:8080/?zip=12345&foo=1&textarea=large_string`\n\n```javascript\nget('/', function (req, res) {\n req.onValidationError(function (msg) {\n //Redirect the user with error 'msg'\n });\n\n //Validate user input\n req.check('zip', 'Please enter a valid ZIP code').len(4,5).isInt();\n req.check('email', 'Please enter a valid email').len(6,64).isEmail();\n req.checkHeader('referer').contains('localhost');\n\n //Sanitize user input\n req.sanitize('textarea').xss();\n req.sanitize('foo').toBoolean();\n\n //etc.\n});\n```\n\n## List of validation methods\n\n```javascript\nis() //Alias for regex()\nnot() //Alias for notRegex()\nisEmail()\nisUrl() //Accepts http, https, ftp\nisIP()\nisAlpha()\nisAlphanumeric()\nisNumeric()\nisInt() //isNumeric accepts zero padded numbers, e.g. '001', isInt doesn't\nisLowercase()\nisUppercase()\nisDecimal()\nisFloat() //Alias for isDecimal\nnotNull()\nisNull()\nnotEmpty() //i.e. not just whitespace\nequals(equals)\ncontains(str)\nnotContains(str)\nregex(pattern, modifiers) //Usage: regex(/[a-z]/i) or regex('[a-z]','i')\nnotRegex(pattern, modifiers)\nlen(min, max) //max is optional\nisUUID(version) //Version can be 3 or 4 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier\nisDate() //Uses Date.parse() - regex is probably a better choice\nisAfter(date) //Argument is optional and defaults to today\nisBefore(date) //Argument is optional and defaults to today\nisIn(options) //Accepts an array or string\nnotIn(options)\nmax(val)\nmin(val)\nisArray()\nisCreditCard() //Will work against Visa, MasterCard, American Express, Discover, Diners Club, and JCB card numbering formats\n```\n\n## List of sanitization / filter methods\n\n```javascript\ntrim(chars) //Trim optional `chars`, default is to trim whitespace (\\r\\n\\t\\s)\nltrim(chars)\nrtrim(chars)\nifNull(replace)\ntoFloat()\ntoInt()\ntoBoolean() //True unless str = '0', 'false', or str.length == 0\ntoBooleanStrict() //False unless str = '1' or 'true'\nentityDecode() //Decode HTML entities\nentityEncode()\nxss() //Remove common XSS attack vectors from text (default)\nxss(true) //Remove common XSS attack vectors from images\n```\n\n## Extending the library\n\nWhen adding to the Validator prototype, use `this.str` to access the string and `this.error(this.msg || default_msg)` when the string is invalid\n\n```javascript\nvar Validator = require('validator').Validator;\nValidator.prototype.contains = function(str) {\n if (!~this.str.indexOf(str)) {\n this.error(this.msg || this.str + ' does not contain ' + str);\n }\n return this; //Allow method chaining\n}\n```\n\nWhen adding to the Filter (sanitize) prototype, use `this.str` to access the string and `this.modify(new_str)` to update it\n\n```javascript\nvar Filter = require('filter').Filter;\nFilter.prototype.removeNumbers = function() {\n this.modify(this.str.replace(/[0-9]+/g, ''));\n return this.str;\n}\n```\n\n## Error handling\n\nBy default, the validation methods throw an exception when a check fails\n\n```javascript\ntry {\n check('abc').notNull().isInt()\n} catch (e) {\n console.log(e.message); //Invalid integer\n}\n```\n\nTo set a custom error message, set the second param of `check()`\n\n```javascript\ntry {\n check('abc', 'Please enter a valid integer').notNull().isInt()\n} catch (e) {\n console.log(e.message); //Please enter a valid integer\n}\n```\n\nTo attach a custom error handler, set the `error` method of the validator instance\n\n```javascript\nvar Validator = require('validator').Validator;\nvar v = new Validator();\nv.error = function(msg) {\n console.log('Fail');\n}\nv.check('abc').isInt(); //'Fail'\n```\n\nYou might want to collect errors instead of throwing each time\n\n```javascript\nValidator.prototype.error = function (msg) {\n this._errors.push(msg);\n}\n\nValidator.prototype.getErrors = function () {\n return this._errors;\n}\n\nvar validator = new Validator();\n\nvalidator.check('abc').isEmail();\nvalidator.check('hello').len(10,30);\n\nvar errors = validator.getErrors(); // ['Invalid email', 'String is too small']\n```\n\n## Contributors\n\n- [PING](https://github.com/PlNG) - Fixed entity encoding\n- [Dan VerWeire](https://github.com/wankdanker) - Modified the behaviour of the error handler\n- [ctavan](https://github.com/ctavan) - Added isArray and isUUID()\n- [foxbunny](https://github.com/foxbunny) - Added min(), max(), isAfter(), isBefore(), and improved isDate()\n- [oris](https://github.com/orls) - Added in()\n- [mren](https://github.com/mren) - Decoupled rules\n\n## LICENSE\n\n(MIT License)\n\nCopyright (c) 2010 Chris O'Hara <cohara87@gmail.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
"readmeFilename": "README.md",
"_id": "validator@0.4.1",
"_from": "validator@0.4.1"
}