{"_id":"o-is-constraint","_rev":"4-9f2546307551f7bde953f694c8be888d","name":"o-is-constraint","dist-tags":{"latest":"0.7.1"},"versions":{"0.6.0":{"name":"o-is-constraint","version":"0.6.0","description":"","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"dependencies":{"lodash.assign":"^4.2.0","lodash.get":"^4.4.2"},"devDependencies":{"o-is":"^0.6.0"},"author":"","license":"ISC","_id":"o-is-constraint@0.6.0","_shasum":"54c69b62b9b3baa5ae68dbb668bc50faee79d10f","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.12.2","_npmUser":{"name":"aghost7","email":"jonathan.boudreau.92@gmail.com"},"dist":{"shasum":"54c69b62b9b3baa5ae68dbb668bc50faee79d10f","tarball":"https://registry.npmjs.org/o-is-constraint/-/o-is-constraint-0.6.0.tgz","integrity":"sha512-RzRO3JbwXS05TZrpNX2K8xUC29qVG3x1PeY1Wq+dAEZ30xdVTVJQQ2aWbEmWKdr8KLq2ov6B5kw6ZP6+7dhEtw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDOHNmLnoELNHqJe8rWRAVspIgPWD67E7rDEmGIsgk4hgIgD4QI/f4PlixDs5wGfqS61ZmDj2EfCRlz1L4BfIHAYmY="}]},"maintainers":[{"name":"aghost7","email":"jonathan.boudreau.92@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/o-is-constraint-0.6.0.tgz_1513039008752_0.20872048381716013"},"directories":{}},"0.7.1":{"name":"o-is-constraint","version":"0.7.1","description":"Serializable constraints with complex conditions (provided by [o-is][1] module).","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"dependencies":{"lodash.assign":"^4.2.0","lodash.get":"^4.4.2","o-is":"^0.7.1"},"author":"","license":"ISC","_id":"o-is-constraint@0.7.1","_shasum":"062714e645eaadcf942e5677c8eb08bb88cae85b","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.12.0","_npmUser":{"name":"aghost7","email":"jonathan.boudreau.92@gmail.com"},"dist":{"shasum":"062714e645eaadcf942e5677c8eb08bb88cae85b","tarball":"https://registry.npmjs.org/o-is-constraint/-/o-is-constraint-0.7.1.tgz","integrity":"sha512-Cqs4tE50SH7zJkgFg+LY/vf2QyZ2Kp+ysBcLiJgiVlCjuA1sNJXP3J/PPdelYVrPThptdUPtlToLM8Fvr6beeA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDVI7dr/YNEFyTacC7PYWymUeoRc1Cjzx3QPW3n3LjnRwIhAJ3+mo8emG4j7tKCYF4sv+1mlHww9U3tCsevFpAy632o"}]},"maintainers":[{"name":"aghost7","email":"jonathan.boudreau.92@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/o-is-constraint-0.7.1.tgz_1515115147801_0.005254951072856784"}}},"readme":"# Object Is: Constraints Module\nSerializable constraints with complex conditions (provided by [o-is][1]\nmodule).\n\nExample:\n```javascript\nconst constraint = require('o-is-constraint')()\n\nconst errors = constraint\n\t// `when()` starts a condition builder which ends the moment you call a\n\t// constraint.\n\t.when()\n\t\t.if().gt('clearance', 4).then()\n\t\t\t.false('admin')\n\t\t\t.false('supervisor')\n\t\t.else()\n\t\t\t.false('admin')\n\t\t.end()\n\t.mandatory(['region'])\n\t// We want the name to always require a minium of 5 characters, hence we\n\t// don't specify a condition here.\n\t.minLength(5, ['name'])\n\t.errors({ admin: false })\n```\nIn this example, the region field is mandatory for supervisors with a clearance\nlower than 5 and any non-administrators. The name must also be 5 characters\nlong at a minimum.\n\n## API\n\n### Built-in Constraints\n\n#### `mandatory(fields)`\nFields must not be null, undefined, or and empty string.\n\n#### `minLength(length, fields)`\nFields must contain a length of at least the specified `length` parameter.\nHandles arrays and strings.\n\n#### `maxLength(length, fields)`\nSame as `minLength`, but for the maximum length.\n\n#### `constant(value, fields)`\nFields must be equal the specified value. This is only really useful when\ncombined with a condition. For example:\n\n```javascript\nconst bug = {\n\tstatus: 'closed',\n\tresolution: 'nofix'\n}\n\nconst errors = constraint\n\t.when()\n\t\t.equal('status', 'closed')\n\t.constant('fixed', ['resolution'])\n\t.errors(bug)\n```\nIn the above example, a bug cannot be closed if it is a \"nofix\".\n\n#### `pattern(regex, fields)`\nA regex constraint.\n\n### Additional methods\n\n#### `fromJSON(data)`\nTakes a parsed json object and returns a constraints instance.\n\n#### `toJSON()`\nReturns a json serializable object.\n\n#### `concat(constraints)`\nTakes two sets of constraints and combines them.\n\n#### `errors(object)`\nReturns an array of errors\n\n#### `assert(object)`\nThrows an exception at the first constaint failure.\n\n### Customization\nYou can extend this module to add methods to the condition builder or the\nconstraints builder. This is done by specifying some options when initializing\nthe module.\n\n#### `options.constraintTypes`\nThis option allows you to specify additional constraint types or even override\nexisting ones. `constraintTypes` is an object where the key is the name of the\nnew constraint and the value is a function.\n\nThe function should accept two arguments, one is the object you want to\ncheck if the constraint fails on and the second are the arguments passed into\nthe builder when calling your constraint type.\n\nA minimal example would be the following:\n```javascript\nconst get = require('lodash.get')\n\nconst constraint = require('o-is-constraint')({\n\tconstraintTypes: {\n\t\tnan: function(context, args) {\n\t\t\tconst errors = []\n\t\t\t// The custom constraint will only accept one argument which is a array\n\t\t\t// of fields, hence the `args[0]`.\n\t\t\tfor(const key of args[0]) {\n\t\t\t\tconst value = get(context, key)\n\t\t\t\tif(!isNaN(value)) {\n\t\t\t\t\terrors.push({\n\t\t\t\t\t\ttype: 'nan',\n\t\t\t\t\t\tvalue: value,\n\t\t\t\t\t\tkey: key\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn errors\n\t\t}\n\t}\n})\n\n// Example usage...\nconstraint\n\t.nan(['name'])\n\t.assert({ name: 'foobar' })\n\n```\nIn this example, we define a constraint which checks if the value of the\nproperty is not a number (NaN).\n\n#### `options.members`\nAllows you to add methods to the [o-is][1] condition builder.\n\n#### `options.assertions`\nAllows you to add new condition types to the [o-is][1] condition builder.\n\n[1]: https://github.com/AGhost-7/o-is/tree/master/packages/o-is\n","maintainers":[{"name":"aghost7","email":"jonathan.boudreau.92@gmail.com"}],"time":{"modified":"2022-06-22T15:06:44.262Z","created":"2017-12-12T00:36:48.828Z","0.6.0":"2017-12-12T00:36:48.828Z","0.7.1":"2018-01-05T01:19:07.885Z"},"license":"ISC","readmeFilename":"readme.md","description":"Serializable constraints with complex conditions (provided by [o-is][1] module)."}