blob: 3f57acb0b550f796d5cfcaa2dcc32b438b305fda [file] [log] [blame]
{
"name": "revalidator",
"version": "0.1.6",
"description": "A cross-browser / node.js validator used by resourceful",
"author": {
"name": "Nodejitsu Inc.",
"email": "info@nodejitsu.com"
},
"maintainers": [
{
"name": "indexzero",
"email": "charlie@nodejitsu.com"
},
{
"name": "indutny",
"email": "fedor.indutny@gmail.com"
}
],
"repository": {
"type": "git",
"url": "http://github.com/flatiron/revalidator.git"
},
"devDependencies": {
"vows": "0.7.0"
},
"main": "./lib/revalidator",
"scripts": {
"test": "vows test/*-test.js --spec"
},
"engines": {
"node": ">= 0.4.0"
},
"readme": "# revalidator [![Build Status](https://secure.travis-ci.org/flatiron/revalidator.png)](http://travis-ci.org/flatiron/revalidator)\n\nA cross-browser / node.js validator used by resourceful and flatiron.\n\n## Example\nThe core of `revalidator` is simple and succinct: `revalidator.validate(obj, schema)`: \n \n``` js\n var revalidator = require('revalidator');\n \n console.dir(revalidator.validate(someObject, {\n properties: {\n url: {\n description: 'the url the object should be stored at',\n type: 'string',\n pattern: '^/[^#%&*{}\\\\:<>?\\/+]+$',\n required: true\n },\n challenge: {\n description: 'a means of protecting data (insufficient for production, used as example)',\n type: 'string',\n minLength: 5\n },\n body: {\n description: 'what to store at the url',\n type: 'any',\n default: null\n }\n }\n }));\n```\n\nThis will return with a value indicating if the `obj` conforms to the `schema`. If it does not, a descriptive object will be returned containing the errors encountered with validation.\n\n``` js\n {\n valid: true // or false\n errors: [/* Array of errors if valid is false */]\n }\n```\n\nIn the browser, the validation function is exposed on `window.validate` by simply including `revalidator.js`.\n\n## Installation\n\n### Installing npm (node package manager)\n``` bash\n $ curl http://npmjs.org/install.sh | sh\n```\n\n### Installing revalidator\n``` bash \n $ [sudo] npm install revalidator\n```\n\n## Usage\n\n`revalidator` takes json-schema as input to validate objects.\n\n### revalidator.validate (obj, schema, options)\n\nThis will return with a value indicating if the `obj` conforms to the `schema`. If it does not, a descriptive object will be returned containing the errors encountered with validation.\n\n``` js\n{\n valid: true // or false\n errors: [/* Array of errors if valid is false */]\n}\n```\n\n#### Available Options\n\n* __validateFormats__: Enforce format constraints (_default true_)\n* __validateFormatsStrict__: When `validateFormats` is _true_ treat unrecognized formats as validation errors (_default false_)\n* __validateFormatExtensions__: When `validateFormats` is _true_ also validate formats defined in `validate.formatExtensions` (_default true_)\n* __cast__: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. `\"42\" => 42`, but `\"forty2\" => \"forty2\"` for the `integer` type.\n\n### Schema\nFor a property an `value` is that which is given as input for validation where as an `expected value` is the value of the below fields\n\n#### required\nIf true, the value should not be empty\n\n```js\n{ required: true }\n```\n\n#### type\nThe `type of value` should be equal to the expected value\n\n```js\n{ type: 'string' }\n{ type: 'number' }\n{ type: 'integer' }\n{ type: 'array' }\n{ type: 'boolean' }\n{ type: 'object' }\n{ type: 'null' }\n{ type: 'any' }\n{ type: ['boolean', 'string'] }\n```\n\n#### pattern\nThe expected value regex needs to be satisfied by the value\n\n```js\n{ pattern: /^[a-z]+$/ }\n```\n\n#### maxLength\nThe length of value must be greater than or equal to expected value\n\n```js\n{ maxLength: 8 }\n```\n\n#### minLength\nThe length of value must be lesser than or equal to expected value\n\n```js\n{ minLength: 8 }\n```\n\n#### minimum\nValue must be greater than or equal to the expected value\n\n```js\n{ minimum: 10 }\n```\n\n#### maximum\nValue must be lesser than or equal to the expected value\n\n```js\n{ maximum: 10 }\n```\n\n#### exclusiveMinimum\nValue must be greater than expected value\n\n```js\n{ exclusiveMinimum: 9 }\n```\n\n### exclusiveMaximum\nValue must be lesser than expected value\n\n```js\n{ exclusiveMaximum: 11 }\n```\n\n#### divisibleBy\nValue must be divisible by expected value\n\n```js\n{ divisibleBy: 5 }\n{ divisibleBy: 0.5 }\n```\n\n#### minItems\nValue must contain more then expected value number of items\n\n```js\n{ minItems: 2 }\n```\n\n#### maxItems\nValue must contains less then expected value number of items\n\n```js\n{ maxItems: 5 }\n```\n\n#### uniqueItems\nValue must hold a unique set of values\n\n```js\n{ uniqueItems: true }\n```\n\n#### enum\nValue must be present in the array of expected value\n\n```js\n{ enum: ['month', 'year'] }\n```\n\n#### format\nValue must be a valid format\n\n```js\n{ format: 'url' }\n{ format: 'email' }\n{ format: 'ip-address' }\n{ format: 'ipv6' }\n{ format: 'date-time' }\n{ format: 'date' }\n{ format: 'time' }\n{ format: 'color' }\n{ format: 'host-name' }\n{ format: 'utc-millisec' }\n{ format: 'regex' }\n```\n\n#### conform\nValue must conform to constraint denoted by expected value\n\n```js\n{ conform: function (v) {\n if (v%3==1) return true;\n return false;\n }\n}\n```\n\n#### dependencies\nValue is valid only if the dependent value is valid\n\n```js\n{\n town: { required: true, dependencies: 'country' },\n country: { maxLength: 3, required: true }\n}\n```\n\n### Nested Schema\nWe also allow nested schema\n\n```js\n{\n properties: {\n title: {\n type: 'string',\n maxLength: 140,\n required: true\n },\n author: {\n type: 'object',\n required: true,\n properties: {\n name: {\n type: 'string',\n required: true\n },\n email: {\n type: 'string',\n format: 'email'\n }\n }\n }\n }\n}\n```\n\n### Custom Messages\nWe also allow custom message for different constraints\n\n```js\n{\n type: 'string',\n format: 'url'\n messages: {\n type: 'Not a string type',\n format: 'Expected format is a url'\n }\n```\n\n```js\n{\n conform: function () { ... },\n message: 'This can be used as a global message'\n}\n```\n\n## Tests\nAll tests are written with [vows][0] and should be run with [npm][1]:\n\n``` bash\n $ npm test\n```\n\n#### Author: [Charlie Robbins](http://nodejitsu.com), [Alexis Sellier](http://cloudhead.io)\n#### Contributors: [Fedor Indutny](http://github.com/indutny), [Bradley Meck](http://github.com/bmeck), [Laurie Harper](http://laurie.holoweb.net/)\n#### License: Apache 2.0\n\n[0]: http://vowsjs.org\n[1]: http://npmjs.org\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/flatiron/revalidator/issues"
},
"_id": "revalidator@0.1.6",
"_from": "revalidator@0.1.x"
}