{"_id":"validations","_rev":"13-dfb1fc9e7a8c7c957972be7cc704799e","name":"validations","description":"A validation library for JavaScript objects modeled loosely on ActiveRecord validations.","dist-tags":{"latest":"0.1.3"},"versions":{"0.1.0":{"name":"validations","description":"A validation library for JavaScript objects modeled loosely on ActiveRecord validations.","homepage":"http://danieldkim.github.com/validations-js/","keywords":["validation"],"author":{"name":"Daniel Kim","email":"danieldkimster@gmail.com"},"contributors":[],"dependencies":{"underscore":">=1.1.7","underscore.string":">=1.1.6","nodeunit":">=0.5.5"},"repository":{"type":"git","url":"git@github.com:danieldkim/validations-js.git"},"main":"validations.js","version":"0.1.0","_npmJsonOpts":{"file":"/Users/danieldkimster/.npm/validations/0.1.0/package/package.json","wscript":false,"contributors":false,"serverjs":false},"_id":"validations@0.1.0","devDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.0.25","_nodeVersion":"v0.4.10","_defaultsLoaded":true,"dist":{"shasum":"26aeaab768299fe707978a3cafe90f9437f9af90","tarball":"https://registry.npmjs.org/validations/-/validations-0.1.0.tgz","integrity":"sha512-PHKOmZxzpYfOjPrMncLHcuXvk/d1txrxZSwENWD1zvD2WEboNoLmEapTsoxrq8emPrV4DyLAhcKVGzUd/pfT4w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC7M5JK8PRzlSZAgT9fe2ZoOfc8Io2k4gdJAtKkg3JFwwIhAJmrKbvVlze4kDH3topH9b35TWW1hFWWR0Zb95SnTvpu"}]},"scripts":{},"maintainers":[{"name":"danieldkim","email":"danieldkimster@gmail.com"}]},"0.1.1":{"name":"validations","description":"A validation library for JavaScript objects modeled loosely on ActiveRecord validations.","homepage":"http://danieldkim.github.com/validations-js/","keywords":["validation"],"author":{"name":"Daniel Kim","email":"danieldkimster@gmail.com"},"contributors":[],"dependencies":{"underscore":">=1.1.7","underscore.string":">=1.1.6","nodeunit":">=0.5.5"},"repository":{"type":"git","url":"git@github.com:danieldkim/validations-js.git"},"main":"validations.js","version":"0.1.1","_npmJsonOpts":{"file":"/Users/danieldkimster/.npm/validations/0.1.1/package/package.json","wscript":false,"contributors":false,"serverjs":false},"_id":"validations@0.1.1","devDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.0.25","_nodeVersion":"v0.4.10","_defaultsLoaded":true,"dist":{"shasum":"5949cd8dae78f754946164b47b801068033594e8","tarball":"https://registry.npmjs.org/validations/-/validations-0.1.1.tgz","integrity":"sha512-SIIZn5LV8HBSJIzUjorhLORYcZaTetrG+nBq4BddQdMB0g7Aeh5N8dEN2CNKFV8pQnrCWDY3NsAjPk/PEBpYoA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIECj/u6WlI1kOdSw0IwRkTpvhCwonNMHZXunof1WrBnsAiEAkEhKkxJ3L8vhu1vAx8KqNzDCv8y2tqpD1XeOAhuBk2A="}]},"scripts":{},"maintainers":[{"name":"danieldkim","email":"danieldkimster@gmail.com"}]},"0.1.2":{"name":"validations","description":"A validation library for JavaScript objects modeled loosely on ActiveRecord validations.","homepage":"http://danieldkim.github.com/validations-js/","keywords":["validation"],"author":{"name":"Daniel Kim","email":"danieldkimster@gmail.com"},"contributors":[],"dependencies":{"underscore":">=1.1.7","underscore.string":">=1.1.6","nodeunit":">=0.5.5"},"repository":{"type":"git","url":"git@github.com:danieldkim/validations-js.git"},"main":"validations.js","version":"0.1.2","_npmUser":{"name":"danieldkim","email":"danieldkimster@gmail.com"},"_id":"validations@0.1.2","devDependencies":{},"optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.0","_nodeVersion":"v0.6.7","_defaultsLoaded":true,"dist":{"shasum":"f8cf7003c8bb2846dd938a06a9645f48a8eb0268","tarball":"https://registry.npmjs.org/validations/-/validations-0.1.2.tgz","integrity":"sha512-WZswY+T7J6LcMdEq0qv5ayqZW1aaSAwkylfcB18rXuKyW6vsneKDd8UHrw/FCVhNyct/03EKqPRzCaC5p4R53g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFB/LrlFKWdMJhcQ7kdUG+aPUFA+XWZuDg5BoJvGHHtiAiEA2TJkU/5pbFBv7QGh+Hs14rIpode8DJIHHcRdxnbz4iI="}]},"maintainers":[{"name":"danieldkim","email":"danieldkimster@gmail.com"}]},"0.1.3":{"name":"validations","description":"A validation library for JavaScript objects modeled loosely on ActiveRecord validations.","homepage":"http://danieldkim.github.com/validations-js/","keywords":["validation"],"author":{"name":"Daniel Kim","email":"danieldkimster@gmail.com"},"contributors":[{"name":"Pavlo Voznenko","email":"p.voznenko@gmail.com","url":"https://twitter.com/pvoznenko/"}],"dependencies":{"underscore":">=1.1.7","underscore.string":">=1.1.6","nodeunit":">=0.5.5","async_testing":"0.3.2"},"repository":{"type":"git","url":"git@github.com:danieldkim/validations-js.git"},"main":"validations.js","version":"0.1.3","scripts":{"build":"npm install","test":"./node_modules/.bin/nodeunit test"},"bugs":{"url":"https://github.com/danieldkim/validations-js/issues"},"_id":"validations@0.1.3","dist":{"shasum":"3503ebf96bbd24bf3f47cd1386a0e1181d3f2c03","tarball":"https://registry.npmjs.org/validations/-/validations-0.1.3.tgz","integrity":"sha512-wsjg8wIXugxVRXyE9e7mcQoZqdapcnWcCFOLotUYTEE2UWJ8QLa7fj25Poh+wzCfYjqpfSDfsuPKhoIK0B1+Bw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIErlSsUWZjgRYWKeYZK0ef0E7i8xugsn41o/31WPO9jwAiEAu+k3/3D+ZcBNsr5a19y8HIcoLcx8s1GH2Y2r3X1dnq0="}]},"_from":".","_npmVersion":"1.3.5","_npmUser":{"name":"danieldkim","email":"danieldkimster@gmail.com"},"maintainers":[{"name":"danieldkim","email":"danieldkimster@gmail.com"}]}},"maintainers":[{"name":"danieldkim","email":"danieldkimster@gmail.com"}],"time":{"modified":"2022-06-28T08:39:59.424Z","created":"2011-09-22T04:29:21.583Z","0.1.0":"2011-09-22T04:29:22.125Z","0.1.1":"2011-09-23T02:53:40.653Z","0.1.2":"2012-03-15T23:00:06.545Z","0.1.3":"2014-08-05T03:35:42.301Z"},"author":{"name":"Daniel Kim","email":"danieldkimster@gmail.com"},"repository":{"type":"git","url":"git@github.com:danieldkim/validations-js.git"},"readme":"# validations-js\n\n[![Build Status](https://travis-ci.org/danieldkim/validations-js.png)](https://travis-ci.org/danieldkim/validations-js)\n\nvalidations-js is a validation library for JavaScript objects modeled loosely on\n[ActiveRecord validations](http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html).\nCurrently supports options to validate requirements, length, numericality, and\nformat.\n\n## Requirements\n\n* [Underscore.js](http://documentcloud.github.com/underscore/)\n\n* [inflections-js](http://code.google.com/p/inflection-js/) (only when running in browser)\n\n* [Underscore.string](https://github.com/edtsech/underscore.string) (only when running on Node.js)\n\n* JSON.stringify() support\n\n* Node.js and [nodeunit](https://github.com/caolan/nodeunit) (optional, only\n  needed to run tests)\n\n## Usage\n\nYou can install using npm:\n\n```\n$ npm install validations\n```\n\nOr download the `validations.js` file from github.\n\nTo use the validations library in Node.js require validations.js and call the\nvalidations function, passing it the object to be validated and a validation\nconfiguration, like so:\n\n```javascript\nvar validations = require('validations');\nvar errors = validations.validate(my_object, my_validation_config);\n```\n\nIf using in the browser include underscore.js, inflection.js, validations.js\nand, if necessary,\n[json2.js](https://github.com/douglascrockford/JSON-js/blob/master/json2.js) in\nyour page instead of using <code>require()</code>;\n\nThe various configuration options and the api for the errors object that is\nreturned are discussed below.\n\n### Validation configuration\n\nTo specify the rules for the properties of an object, set the *properties*\nproperty of the validation configuration with a hash that contains the\nconfiguration for each property. For example, validating that an object has a\nrequired property can be done like so:\n\n```javascript\nvar errors = validations.validate(my_object, {\n  properties: {\n    my_prop: {\n      required: true\n    }\n  }\n});\n```\n\nTo validate the length of property use the *length* option:\n\n```javascript\nvar errors = validations.validate(my_object, {\n  properties: {\n    my_prop_a: {\n      length: {\n        is: 1\n      }\n    },\n    my_prop_b: {\n      length: {\n        is: 2\n      }\n    },\n    my_prop_b: {\n      length: {\n        min: 1\n      }\n    }\n  }\n});\n```\n\nYou can specify multiple options on a single property:\n\n```javascript\nvar errors = validations.validate(my_object, {\n  properties: {\n    my_prop_a: {\n      required: true,\n      length: {\n        min: 1,\n        max: 10\n      }\n    }\n  }\n});\n```\n\n**Note: if a property is not required and it's blank (undefined or null), any\nother validation options defined for that property will not be applied.**\n\nThese are all the validation options:\n\n* required\n* length\n    * is\n    * min\n    * max\n* numericality\n    * onlyInteger\n    * greaterThan\n    * greaterThanOrEqualTo\n    * equalTo\n    * lessThan\n    * lessThanOrEqualTo\n    * odd\n    * even\n* format\n    * pattern\n\n#### Message configuration\n\nvalidations-js comes packaged with a default set of error messages for each of the validation options:\n\n```javascript\n{\n  required: \"{{name}} is required.\",\n  length: {\n    is: \"{{name}} must be exactly {{compare_to}} characters.\",\n    min: \"{{name}} must be at least {{compare_to}} characters.\",\n    max: \"{{name}} must not exceed {{compare_to}} characters.\"\n  },\n  numericality: {\n    onlyInteger: \"{{name}} must be an integer.\",\n    greaterThan: \"{{name}} must be greater than {{compare_to}}\",\n    greaterThanOrEqualTo: \"{{name}} must be greater than or equal to {{compare_to}}.\",\n    equalTo: \"{{name}} must be equal to {{compare_to}}.\",\n    lessThan: \"{{name}} must be less than {{compare_to}}.\",\n    lessThanOrEqualTo: \"{{name}} must be less than or equal to {{compare_to}}.\",\n    odd: \"{{name}} must be an odd number.\",\n    even: \"{{name}} must be an even number.\"\n  },\n  format: {\n    pattern: \"{{name}} is not formatted correctly.\"\n  }\n}\n```\n\n*{{name}}* gets replaced with the name of the property in error, and\n*{{compare\\_to}}* gets replaced with the value that the object property was\ncompared to, if relevant.\n\nAny of these messages can be overridden by specifying a message option on the\nproperty configuration like so:\n\n```javascript\nvar errors = validations.validate(my_object, {\n  properties: {\n    my_prop: {\n      required: true,\n      message: \"Where the hell is {{name}}?\"\n    }\n  }\n});\n```\n\nIf you would like to override the message for a particular option for all\nproperties in a configuration, you can pass a *defaultMessages* configuration\nlike so:\n\n```javascript\nvar errors = validations.validate(my_object, {\n  defaultMessages: {\n    length: {\n      is: \"{{name}} must be {{compare_to}} characters in length, no more, no less.\"\n    }\n  },\n  properties: {\n    my_prop_a: {\n      length: {\n        is: 1\n      }\n    },\n    my_prop_b: {\n      length: {\n        is: 2\n      }\n    }\n  }\n});\n```\n\n### Errors object\n\nThe errors object returned by the *validate* function acts like the ActiveRecord\nValidations errors objects.\n\nHere are the methods available:\n\n* count() - returns the total number of errors found.  alias for *size()*.\n\n* each(callback) - iterates through all the properties for which an error was\n  found, passing errors and name of property to *callback*.  e.g.:\n  <pre>\n  errors\\_result.each(function(errors, name) {\n    util.puts(\"Errors on \" + name + \":\" + errors.join(\" \"));\n  })\n  </pre>\n  *errors* is an normally an array of error messages.  When using recursion,\n  *errors* may be a child errors object on which all of these methods can be\n  called.\n\n* isEmpty() - return true if there no errors.\n\n* messages() - returns all error messages in an array.\n\n* isInvalid(name) - returns true if an error was found with *name*.\n\n* length() - returns the total number of errors found.  alias for *size()*.\n\n* on(name) - returns all of the error messages for *name* as an array.\n\n* size() - returns the total number of errors found.\n\n### Recursion\n\nvalidations-js can validate recursively. A property of an object can be an object\nthat has its own configuration. Use the *object* option to specify a\nsub-configuration for an object property. For example, let's say you had an\nobject representing a place, with a *location* property that is itself an object\nwith a *lat* and *lon* property. You might validate it like so:\n\n```javascript\nvar errors = validations.validate(place, {\n  properties: {\n    name: {\n      required: true,\n      length: {min: 1, max: 80}\n    },\n    location: {\n      object: {\n        properties: {\n          lat: {\n            required: true,\n            numericality: {greaterThanOrEqualTo: -90, lessThanOrEqualTo: 90}\n          },\n          lon: {\n            required: true,\n            numericality: {greaterThanOrEqualTo: -180, lessThanOrEqualTo: 180}\n          }\n        }\n      }\n    }\n  }\n});\n```\n\nThe errors object returned by *validate* is also recursive. You would detect and\ndisplay an error with the *lat* property like so:\n\n```javascript\nif (!isBlank(errors)\n    && errors.isInvalid(\"location\")\n    && errors.on(\"location\").isInvalid(\"lat\"))\n  util.puts(\"Problem with lat: \" + errors.on(\"location\").on(\"lat\").join(\" \"));\n```\n\n**Note:** all nodes in a path must exist.\n\nFor example if the object you're validating looks like:\n\n```javascript\nvar obj = {\n    firstNode: {\n      wrongNode: {\n        thirdNode: true\n      }\n    }\n  };\n```\n\nAnd your validation pattern looks like:\n\n```javascript\nvar errors = validations.validate(obj, {\n  properties: {\n    firstNode: {\n      object: {\n        properties: {\n          secondNode: {\n            object: {\n              properties: {\n                thirdNode: true\n              }\n            }\n          }\n        }\n      }\n    }\n  }\n});\n```\n\nYou will get an error message that one of the nodes in the path is missing.\n\n## License\n\nCopyright (c) 2010 Daniel Kim and other [contributors](https://github.com/danieldkim/validations-js/graphs/contributors).\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\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\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.\n","homepage":"http://danieldkim.github.com/validations-js/","keywords":["validation"],"contributors":[{"name":"Pavlo Voznenko","email":"p.voznenko@gmail.com","url":"https://twitter.com/pvoznenko/"}],"readmeFilename":"README.md","bugs":{"url":"https://github.com/danieldkim/validations-js/issues"}}