{"id":22735098,"url":"https://github.com/andreip/schema-inspector-testing","last_synced_at":"2025-09-09T05:37:21.556Z","repository":{"id":19057117,"uuid":"22283556","full_name":"andreip/schema-inspector-testing","owner":"andreip","description":"schema-inspector-testing bower","archived":false,"fork":false,"pushed_at":"2014-07-26T10:35:55.000Z","size":348,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-05T05:05:27.572Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-26T10:20:02.000Z","updated_at":"2014-07-26T10:20:34.000Z","dependencies_parsed_at":"2022-09-12T06:11:46.872Z","dependency_job_id":null,"html_url":"https://github.com/andreip/schema-inspector-testing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreip%2Fschema-inspector-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreip%2Fschema-inspector-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreip%2Fschema-inspector-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreip%2Fschema-inspector-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreip","download_url":"https://codeload.github.com/andreip/schema-inspector-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246266859,"owners_count":20749884,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-10T21:07:30.938Z","updated_at":"2025-03-30T02:23:27.238Z","avatar_url":"https://github.com/andreip.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![schema-inspector logo](https://raw2.github.com/Atinux/schema-inspector/master/misc/schema-inspector.png)](http://atinux.github.io/schema-inspector/)\n\nSchema-Inspector is a powerful tool to sanitize and validate JS objects.\nIt's designed to work both client-side and server-side and to be scalable with allowing asynchronous and synchronous calls.\n\n[![Build Status](https://travis-ci.org/Atinux/schema-inspector.png?branch=master)](https://travis-ci.org/Atinux/schema-inspector) [![Dependencies Status](https://david-dm.org/atinux/schema-inspector.png)](https://david-dm.org/atinux/schema-inspector) [![NPM version](https://badge.fury.io/js/schema-inspector.png)](http://badge.fury.io/js/schema-inspector)\n\nDemonstration \u0026 documentation: http://atinux.github.io/schema-inspector/\n\n## Installation\n\n### Node.js\n\u003cpre\u003enpm install schema-inspector\u003c/pre\u003e\n\n### Browser\n\u003cpre\u003ebower install schema-inspector\u003c/pre\u003e\n(Or download [async.js](https://raw.github.com/caolan/async/master/lib/async.js) and [schema-inspetor.js](https://raw.github.com/Atinux/schema-inspector/master/lib/schema-inspector.js) manually).\n```html\n\u003cscript type=\"text/javascript\" src=\"bower_components/async/lib/async.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\" src=\"bower_components/schema-inspector/lib/schema-inspector.js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\n### Synchronous call\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'string', eq: 'ipsum' },\n\t\tdolor: {\n\t\t\ttype: 'array',\n\t\t\titems: { type: 'number' }\n\t\t}\n\t}\n};\n\nvar candidate = {\n\tlorem: 'not_ipsum',\n\tdolor: [ 12, 34, 'ERROR', 45, 'INVALID' ]\n};\nvar result = inspector.validate(schema, candidate); // Candidate is not valid\nconsole.log(result.format());\n/*\n\tProperty @.lorem: must be equal to \"ipsum\", but is equal to \"not_ipsum\"\n\tProperty @.dolor[2]: must be number, but is string\n\tProperty @.dolor[4]: must be number, but is string\n*/\n```\n\n### Asynchronous call\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = { ...\t};\n\nvar candidate = { ... };\n\ninspector.validate(schema, candidate, function (err, result) {\n\tconsole.log(result.format());\n\t/*\n\t\tProperty @.lorem: must be equal to \"ipsum\", but is equal to \"not_ipsum\"\n\t\tProperty @.dolor[2]: must be number, but is string\n\t\tProperty @.dolor[4]: must be number, but is string\n\t*/\n});\n```\n\n### Custom fields\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\titems: { type: 'number', $divisibleBy: 5 }\n};\n\nvar custom = {\n\tdivisibleBy: function (schema, candidate) {\n\t\tvar dvb = schema.$divisibleBy;\n\t\tif (candidate % dvb !== 0) {\n\t\t\tthis.report('must be divisible by ' + dvb);\n\t\t}\n\t}\n};\n\nvar candidate = [ 5, 10, 15, 16 ];\nvar result = inspector.validate(schema, candidate, custom);\nconsole.log(result.format());\n/*\n\tProperty @[3]: must be divisible by 5\n*/\n```\n\n## In the browser\n\n```html\n\u003cscript type=\"text/javascript\" src=\"async.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\" src=\"schema-inspetor.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n\tvar schema = {\n\t\ttype: 'object',\n\t\tproperties: {\n\t\t\tlorem: { type: 'string', eq: 'ipsum' },\n\t\t\tdolor: {\n\t\t\t\ttype: 'array',\n\t\t\t\titems: { type: 'number' }\n\t\t\t}\n\t\t}\n\t};\n\n\tvar candidate = {\n\t\tlorem: 'not_ipsum',\n\t\tdolor: [ 12, 34, 'ERROR', 45, 'INVALID' ]\n\t};\n\tSchemaInspector.validate(schema, candidate, function (err, result) {\n\t\talert(result.format());\n\t});\n\u003c/script\u003e\n```\n\nIn the example below, the `inspector` variable will be used.  For the client-side use `SchemaInspector` instead of `inspector`.\n\n## Documentation\n\n### Validation\n\n* [type](#v_type)\n* [optional](#v_optional)\n* [pattern](#v_pattern)\n* [minLength, maxLength, exactLength](#v_length)\n* [lt, lte, gt, gte, eq, ne](#v_comparators)\n* [someKeys](#v_someKeys)\n* [strict](#v_strict)\n* [exec](#v_exec)\n* [properties](#v_properties)\n* [items](#v_items)\n* [alias](#v_alias)\n* [error](#v_error)\n* [code](#v_code)\n\n### Sanitization\n\n* [type](#s_type)\n* [def](#s_def)\n* [optional](#s_optional)\n* [rules](#s_rules)\n* [min, max](#s_comparators)\n* [minLength, maxLength](#s_length)\n* [strict](#s_strict)\n* [exec](#s_exec)\n* [properties](#s_properties)\n* [items](#s_items)\n\n### Custom fields\n* [punctual use](#cf_punctual)\n* [extension](#cf_extension)\n* [context](#cf_context)\n\n### Asynchronous call\n* [How to](#a_howTo)\n\n### Thanks to:\n* [Benjamin Gressier](https://twitter.com/NikitaJS) (major contributor of this awesome module)\n\n### Roadmap:\n* [V2.0 - With json-schema.org](https://github.com/Atinux/schema-inspector/issues/milestones?with_issues=no)\n\n## Validation\n\n\u003ca name=\"v_type\" /\u003e\n### type\n\n* **type**: string, array of string.\n* **usable on**: any.\n* **possible values**\n\t* `string`\n\t* `number`\n\t* `integer`\n\t* `boolean`\n\t* `null`\n\t* `date` (constructor === Date)\n\t* `object` (constructor === Object)\n\t* `array` (constructor === Array)\n\t* A function (candidate isinstance)\n\t* `any` (it can be anything)\n\nAllow to check property type. If the given value is incorrect, then type is not\nchecked.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nfunction Class() {}\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: {  type: 'number' },\n\t\tipsum: { type: 'any' },\n\t\tdolor: { type: ['number' 'string', 'null'] },\n\t\tsit: { type: Class }\n\t}\n};\n\nvar c1 = {\n\tlorem: 12,\n\tipsum: 'sit amet',\n\tdolor: 23,\n\tsit: new Class()\n};\nvar c2 = {\n\tlorem: 12,\n\tipsum: 34,\n\tdolor: 'sit amet'\n\tsit: new Class();\n};\nvar c3 = {\n\tlorem: 12,\n\tipsum: [ 'sit amet' ],\n\tdolor: null,\n\tsit: new Class();\n};\nvar c4 = {\n\tlorem: '12',\n\tipsum: 'sit amet',\n\tdolor: new Date(),\n\tsit: {}\n};\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Valid\ninspector.validate(schema, c3); // Valid\ninspector.validate(schema, c4); // Invalid: @.lorem must be a number, @dolor must be a number, a string or null, @.sit must be an instance of Class, but is object\n```\n\n---------------------------------------\n\n\u003ca name=\"v_optional\" /\u003e\n### optional\n\n* **type**: boolean.\n* **default**: false.\n* **usable on**: any.\n\nThis field indicates whether or not property has to exist.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'any', optional: true }\n\t}\n};\n\nvar schema2 = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'any', optional: false } // default value\n\t}\n};\n\nvar c1 = { lorem: 'ipsum' };\nvar c2 = { };\n\ninspector.validate(schema1, c1); // Valid\ninspector.validate(schema1, c2); // Valid\ninspector.validate(schema2, c1); // Valid\ninspector.validate(schema2, c2); // Invalid: \"@.lorem\" is missing and not optional\n```\n\n---------------------------------------\n\n\u003ca name=\"v_uniqueness\" /\u003e\n### uniqueness\n\n* **type**: boolean.\n* **default**: false.\n* **usable on**: array, string.\n\nIf true, then we ensure no element in candidate exists more than once.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\tuniqueness: true\n};\n\nvar c1 = [12, 23, 34, 45];\nvar c2 = [12, 23, 34, 12];\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid: 12 exists twice in @.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_pattern\" /\u003e\n### pattern\n\n* **type**: string, RegExp object, array of string and RegExp.\n* **usable on**: string.\n* Possible values as a string: `void`, `url`, `date-time`, `date`,\n`coolDateTime`, `time`, `color`, `email`, `numeric`, `integer`, `decimal`,\n`alpha`, `alphaNumeric`, `alphaDash`, `javascript`, `upperString`, `lowerString`.\n\nAsk Schema-Inspector to check whether or not a given matches provided patterns.\nWhen a pattern is a RegExp, it directly test the string with it. When it's a\nstring, it's an alias of a RegExp.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'array',\n\titems: { type: 'string', pattern: /^[A-C]/ }\n};\n\nvar c1 = ['Alorem', 'Bipsum', 'Cdolor', 'DSit amet'];\n\nvar schema2 = {\n\ttype: 'array',\n\titems: { type: 'string', pattern: 'email' }\n};\n\nvar c2 = ['lorem@ipsum.com', 'dolor@sit.com', 'amet@consectetur'];\n\ninspector.validate(schema1, c1); // Invalid: @[3] ('DSit amet') does not match /^[A-C]/\ninspector.validate(schema2, c2); // Invalid: @[2] ('amet@consectetur') does not match \"email\" pattern.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_length\" /\u003e\n### minLength, maxLength, exactLength\n\n* **type**: integer.\n* **usable on**: array, string.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'string', minLength: 4, maxLength: 8 },\n\t\tipsum: { type: 'array', exactLength: 6 },\n\t}\n};\nvar c1 = {\n\tlorem: '12345',\n\tipsum: [1, 2, 3, 4, 5, 6]\n};\n\nvar c2 = {\n\tlorem: '123456789',\n\tipsum: [1, 2, 3, 4, 5]\n};\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid: @.lorem must have a length between 4 and 8 (here 9)\n// and @.ipsum must have a length of 6 (here 5)\n```\n\n---------------------------------------\n\n\u003ca name=\"v_comparators\" /\u003e\n### lt, lte, gt, gte, eq, ne\n\n* **type**: number (string, number and boolean for eq).\n* **usable on**: number (string, number and boolean for eq).\n\nCheck whether comparison is true:\n\n* lt: `\u003c`\n* lte: `\u003c=`\n* gt: `\u003e`\n* gte: `\u003e=`\n* eq: `===`\n* ne: `!==`\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', gt: 0, lt: 5 }, // Between ]0; 5[\n\t\tipsum: { type: 'number', gte: 0, lte: 5 }, // Between [0; 5]\n\t\tdolor: { type: 'number', eq: [0, 3, 6, 9] }, // Equal to 0, 3, 6 or 9\n\t\tsit: { type: 'number', ne: [0, 3, 6, 9] } // Not equal to 0, 3, 6 nor 9\n\t}\n};\n\nvar c1 = { lorem: 3, ipsum: 0, dolor: 6, sit: 2 };\nvar c2 = { lorem: 0, ipsum: -1, dolor: 5, sit: 3 };\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid\n```\n\n---------------------------------------\n\n\u003ca name=\"v_someKeys\" /\u003e\n### someKeys\n\n* **type**: array of string.\n* **usable on**: object.\n\nCheck whether one of the given keys exists in object (useful when they are\noptional).\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tsomeKeys: ['lorem', 'ipsum']\n\tproperties: {\n\t\tlorem: { type: 'any', optional: true },\n\t\tipsum: { type: 'any', optional: true },\n\t\tdolor: { type: 'any' }\n\t}\n};\n\nvar c1 = { lorem: 0, ipsum: 1, dolor: 2  };\nvar c2 = { lorem: 0, dolor: 2  };\nvar c3 = { dolor: 2  };\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Valid\ninspector.validate(schema, c3); // Invalid: Neither @.lorem nor @.ipsum is in c3.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_strict\" /\u003e\n### strict\n\n* **type**: boolean.\n* **default**: false.\n* **usable on**: object.\n\nOnly key provided in field \"properties\" may exist in object.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tstrict: true,\n\tproperties: {\n\t\tlorem: { type: 'any' },\n\t\tipsum: { type: 'any' },\n\t\tdolor: { type: 'any' }\n\t}\n};\n\nvar c1 = { lorem: 0, ipsum: 1, dolor: 2  };\nvar c2 = { lorem: 0, ipsum: 1, dolor: 2, sit: 3  };\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid: @.sit should not exist.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_exec\" /\u003e\n### exec\n\n* **type**: function, array of function.\n* **usable on**: any.\n\nCustom checker =). \"exec\" functions take two three parameter\n(schema, post [, callback]). To report an error, use `this.report([message], [code])`.\nVery useful to make some custom validation.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: {\n\t\t\ttype: 'number',\n\t\t\texec: function (schema, post) {\n\t\t\t\t// here scheme === schema.properties.lorem and post === @.lorem\n\t\t\t\tif (post === 3) {\n\t\t\t\t\t// As soon as `this.report()` is called, candidate is not valid.\n\t\t\t\t\tthis.report('must not equal 3 =('); // Ok...it's exactly like \"ne: 3\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};\n\nvar c1 = { lorem: 2 };\nvar c2 = { lorem: 3 };\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid: \"@.lorem must not equal 3 =(\".\n```\n\n---------------------------------------\n\n\u003ca name=\"v_properties\" /\u003e\n### properties\n\n* **type**: object.\n* **usable on**: object.\n\nFor each property in the field \"properties\", whose value must be a schema,\nvalidation is called deeper in object.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: {\n\t\t\ttype: 'object',\n\t\t\tproperties: {\n\t\t\t\tipsum: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties: {\n\t\t\t\t\t\tdolor: { type: 'string' }\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\tconsectetur: { type: 'string' }\n\t}\n};\n\nvar c1 = {\n\tlorem: {\n\t\tipsum: {\n\t\t\tdolor: 'sit amet'\n\t\t}\n\t},\n\tconsectetur: 'adipiscing elit'\n};\nvar c2 = {\n\tlorem: {\n\t\tipsum: {\n\t\t\tdolor: 12\n\t\t}\n\t},\n\tconsectetur: 'adipiscing elit'\n};\n\ninspector.validate(schema, c1); // Valid\ninspector.validate(schema, c2); // Invalid: @.lorem.ipsum.dolor must be a string.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_items\" /\u003e\n### items\n\n* **type**: object, array of object.\n* **usable on**: array.\n\nAllow to apply schema validation for each element in an array. If it's an\nobject, then it's a schema which will be used for all the element. If it's an\narray of object, then it's an array of schema and each element in an array will\nbe checked with the schema which has the same position in the array.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'array',\n\titems: { type: 'number'\t}\n};\n\nvar schema2 = {\n\ttype: 'array',\n\titems: [\n\t\t{ type: 'number' },\n\t\t{ type: 'number' },\n\t\t{ type: 'string' }\n\t]\n};\n\nvar c1 = [1, 2, 3];\nvar c2 = [1, 2, 'string!'];\n\n\ninspector.validate(schema1, c1); // Valid\ninspector.validate(schema1, c2); // Invalid: @[2] must be a number.\ninspector.validate(schema2, c1); // Invalid: @[2] must be a string.\ninspector.validate(schema2, c2); // Valid\n```\n\n---------------------------------------\n\n\u003ca name=\"v_alias\" /\u003e\n### alias\n\n* **type**: string.\n* **usable on**: any.\n\nAllow to display a more explicit property name if an error is encounted.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { type: 'string'}\n\t}\n};\n\nvar schema2 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { alias: 'id', type: 'string'}\n\t}\n};\n\nvar c1 = { _id: 1234567890 };\n\nvar r1 = inspector.validate(schema1, c1);\nvar r2 = inspector.validate(schema2, c1);\nconsole.log(r1.format()); // Property @._id: must be string, but is number\nconsole.log(r2.format()); // Property id (@._id): must be string, but is number\n```\n\n---------------------------------------\n\n\u003ca name=\"v_error\" /\u003e\n### error\n\n* **type**: string.\n* **usable on**: any.\n\nThis field contains a user sentence for displaying a more explicit message if\nan error is encounted.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { type: 'string' }\n\t}\n};\n\nvar schema2 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { type: 'string', error: 'must be a valid ID.' }\n\t}\n};\n\nvar c1 = { _id: 1234567890 };\n\nvar r1 = inspector.validate(schema1, c1);\nvar r2 = inspector.validate(schema2, c1);\nconsole.log(r1.format()); // Property @._id: must be string, but is number.\nconsole.log(r2.format()); // Property @._id: must be a valid ID.\n```\n\n---------------------------------------\n\n\u003ca name=\"v_code\" /\u003e\n### code\n\n* **type**: string.\n* **usable on**: any.\n\nThis field contains a user code for displaying a more uniform system to personnalize error message.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema1 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { type: 'string' }\n\t}\n};\n\nvar schema2 = {\n\ttype: 'object',\n\tproperties: {\n\t\t_id: { type: 'string', code: 'id-format' }\n\t}\n};\n\nvar c1 = { _id: 1234567890 };\n\nvar r1 = inspector.validate(schema1, c1);\nvar r2 = inspector.validate(schema2, c1);\nconsole.log(r1.error[0].code); // null\nconsole.log(r2.error[0].code); // 'id-format'\n```\n\n## Sanitization\n\n\u003ca name=\"s_type\" /\u003e\n### type\n\n* **type**: string.\n* **usable on**: any.\n* **possible values**\n\t* `number`\n\t* `integer`\n\t* `string`\n\t* `boolean`\n\t* `date` (constructor === Date)\n\t* `object` (constructor === Object)\n\t* `array` (constructor === Array)\n\nCast property to the given type according to the following description:\n* **to number from**:\n\t* string\n\t\t* \"12.34\" -\u003e 12.34\n\t* date\n\t\t* new Date(\"2014-01-01\") -\u003e 1388534400000)\n* **to integer from**:\n\t* number\n\t\t* 12.34 -\u003e 12\n\t* string\n\t\t* \"12.34\" -\u003e 12\n\t* boolean\n\t\t* true -\u003e 1\n\t\t* false -\u003e 0\n\t* date\n\t\t* new Date(\"2014-01-01\") -\u003e 1388534400000\n* **to string from**:\n\t* boolean\n\t\t* true -\u003e \"true\"\n\t* number\n\t\t* 12.34 -\u003e \"12.34\"\n\t* integer\n\t\t* 12 -\u003e \"12\"\n\t* date\n\t\t* new Date(\"2014-01-01\") -\u003e \"Wed Jan 01 2014 01:00:00 GMT+0100 (CET)\"\n\t* array\n\t\t* [12, 23, 44] -\u003e '12,34,45'\n\t\t* To join with a custom string, use **joinWith** key (example: { type: \"string\", joinWith: \"|\" } will transform [12, 23, 44] to \"12|23|44\").\n* **to date from**:\n\t* number / integer\n\t\t* 1361790386000 -\u003e Wed Jan 01 2014 01:00:00 GMT+0100 (CET)\n\t* string\n\t\t* \"2014-01-01 -\u003e Wed Jan 01 2014 01:00:00 GMT+0100 (CET)\n\t\t* \"Wed Jan 01 2014 01:00:00 GMT+0100 (CET)\" -\u003e Wed Jan 01 2014 01:00:00 GMT+0100 (CET)\n* **to object from**:\n\t* string\n\t\t* '{\"love\":\"open source\"}' -\u003e { love: \"open source\" }\n* **to array from**:\n\t* string \t\t\t(\"one,two,three\" -\u003e [\"one\", \"two\", \"three\"])\n\t* anything except undefined and array \t(23 -\u003e [ 23 ])\n\t* To split with a custom string (other than \",\"), use the key **splitWith** (example: { type: \"array\", splitBy: \"|\"\" } will transform \"one|two|three\" to [\"one\", \"two\", \"three\"]).*\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\titems: { type: 'string' }\n};\n\nvar c = [ 12.23, -34, true, false, 'true', 'false', [123, 234, 345], { obj: \"yes\" } ];\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: [ '12.23', '-34', 'true', 'false', 'true', 'false', '123,234,345', '{\"obj\":\"yes\"}' ]\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_def\" /\u003e\n### def\n* **type**: any.\n* **usable on**: any.\n\nDefine default value if property does not exist, or if type casting is to fail\nbecause entry type is not valid (cf [type](#s_type)).\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', def: 10 },\n\t\tipsum: { type: 'string', def: 'NikitaJS', optional: false },\n\t\tdolor: { type: 'string' }\n\t}\n};\n\nvar c = {\n\tlorem: [12, 23],\t// convertion to number is about to fail\n\t\t\t\t\t\t\t\t\t\t// (array -\u003e number is not possible)\n\t\t\t\t\t\t\t\t\t\t// ipsum is not privided\n\tdolor: 'sit amet' // \"dolor\" is already a string\n};\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: {\n\t\tlorem: 10,\n\t\tipsum: 'NikitaJS',\n\t\tdolor: 'sit amet'\n\t}\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_optional\" /\u003e\n### optional\n\n* **type**: boolean.\n* **default**: true.\n* **usable on**: any.\n\nProperty is set to `schema.def` if not provided and if optional is `false`.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', optional: false, def: 12 },\n\t\tipsum: { type: 'string', optional: true, def: 23 },\n\t\tdolor: { type: 'string', def: 'NikitaJS, def: 34 } // (optional: true)\n\t}\n};\n\nvar c = { };\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: {\n\t\tlorem: 12 // Only lorem is set to 12 because it is not optional.\n\t}\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_rules\" /\u003e\n### rules\n\n* **type**: string, array of string.\n* **usable on**: string.\n* **possible values**:\n\t* `upper`: Every character will be changed to uppercase.\n\t* `lower`: Every character will be changed to lowercase.\n\t* `title`: For each word (/\\S*/g), first letter will be changed to uppercase, and the rest to lowercase.\n\t* `capitalize`: Only the first letter of the string will be changed to uppercase, the rest to lowercase.\n\t* `ucfirst`: Only the first letter of the string will be changed to uppercase, the rest is not modified.\n\t* `trim`: Remove extra spaces.\n\nApply the given rule to a string. If several rules are given (array), then they\nare applied in the same order than in the array.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'string', rules: 'upper' },\n\t\tipsum: { type: 'string', rules: [ 'trim', 'title'] }\n\t}\n};\n\nvar c = {\n\tlorem: ' tHiS is sParTa! ',\n\tipsum: '   tHiS is sParTa!    '\n};\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: {\n\t\tlorem: ' THIS IS SPARTA! ',\n\t\tipsum: 'This Is Sparta!' // has been trimed, then titled\n\t}\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_comparators\" /\u003e\n### min, max\n\n* **type**: string, number.\n* **usable on**: string, number.\n\nDefine minimum and maximum value for a property. If it's less than minimum,\nthen it's set to minimum. If it's greater than maximum, then it's set to\nmaximum.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\titems: { type: 'number', min: 10, max: 20 }\n};\n\nvar c = [5, 10, 15, 20, 25];\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: [10, 10, 15, 20, 20]\n\tc[0] (5) was less than min (10), so it's been set to 10.\n\tc[4] (25) was greater than max (20), so it's been set to 20.\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_length\" /\u003e\n### minLength, maxLength\n\n* **type**: integer.\n* **usable on**: string.\n\nAdjust string length to the given number.\n\n__TODO:__ We must be able to choose which character we want to fill the string with.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\titems: { type: 'string', minLength: 8, maxLength: 11 }\n};\n\nvar c = ['short', 'mediumSize', 'tooLongForThisSchema'];\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: ['short---', 'mediumSize', 'tooLongForT']\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_strict\" /\u003e\n### strict\n\n* **type**: boolean.\n* **default**: false.\n* **usable on**: any.\n\nOnly key provided in field \"properties\" will exist in object, others will be deleted.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tstrict: true,\n\tproperties: {\n\t\tgood: { type: 'string' }\n\t}\n};\n\nvar c = {\n\tgood: 'yes',\n\tbad: 'nope'\n};\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: {\n\t\tgood: 'yes'\n\t}\n*/\n```\n---------------------------------------\n\n\u003ca name=\"s_exec\" /\u003e\n### exec\n\n* **type**: function, array of functions.\n* **usable on**: any.\n\nCustom checker =). \"exec\" functions take two three parameter\n(schema, post [, callback]), and must return the new value. To report an\nsanitization, use `this.report([message])`. Very useful to make some custom\nsanitization.\n\n__NB:__ If you don't want to return a differant value, simply return `post`,\ndo not return nothing (if you do so, the new value will be `undefined`).\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'array',\n\titems: {\n\t\ttype: 'string',\n\t\texec: function (schema, post) {\n\t\t\tif (typeof post === 'string' \u0026\u0026 !/^nikita$/i.test(post)) {\n\t\t\t\tthis.report();\n\t\t\t\treturn '_INVALID_';\n\t\t\t}\n\t\t\treturn post;\n\t\t}\n\t}\n};\n\nvar c = [ 'Nikita', 'lol', 'NIKITA', 'thisIsGonnaBeSanitized!' ];\n\nvar r = inspector.sanitize(schema, c);\n/*\n\tc: [ 'Nikita', '_INVALID_', 'NIKITA', '_INVALID_' ]\n*/\n```\n\n---------------------------------------\n\n\u003ca name=\"s_properties\" /\u003e\n### properties\n\n* **type**: object.\n* **usable on**: object.\n\nWork the same way as [validation \"properties\"](#v_properties).\n\n---------------------------------------\n\n\u003ca name=\"s_items\" /\u003e\n### items\n\n* **type**: object, array of object.\n* **usable on**: array.\n\nWork the same way as [validation \"items\"](#v_items).\n\n## Custom fields\n\n\u003ca name=\"cf_punctual\" /\u003e\n### punctual use\n\nWhen you need to use the same function in `exec` field several time, instead of\nsaving the function and declaring `exec` several times, just use custom field.\nFirst you have to provide a hash containing a function for each custom field you\nwant to inject. Then you can call them in your schema with $\"your field name\".\nFor example if you\nprovide a custom field called \"superiorMod\", you can access it with name\n\"$superiorMod\".\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', $divisibleBy: 5 },\n\t\tipsum: { type: 'number', $divisibleBy: 3 }\n\t}\n};\n\nvar custom = {\n\tdivisibleBy: function (schema, candidate) {\n\t\tvar dvb = schema.$divisibleBy;\n\t\tif (candidate % dvb !== 0) {\n\t\t\tthis.report('must be divisible by ' + dvb);\n\t\t}\n\t}\n};\n\nvar c = {\n\tlorem: 10,\n\tipsum: 8\n};\ninspector.validate(schema, candidate, custom); // Invalid: \"@.ipsum must be divisible by 3\"\n```\n\n---------------------------------------\n\n\u003ca name=\"cf_extension\" /\u003e\n### extension\n\nSometime you want to use a custom field everywhere in your program, so you may\nextend Schema-Inspector to do so. Just call the method\n_inspector.Validation.extend(customFieldObject)_ or\n_inspector.Sanitization.extend(customFieldObject)_. If you want to reset, simply call\n_inspector.Validation.reset()_ or _inspector.Sanitization.reset()_. You also can remove a\nspecific field by calling _inspector.Validation.remove(field)_ or\n_inspector.Sanitization.remove(field)_.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar custom = {\n\tdivisibleBy: function (schema, candidate) {\n\t\tvar dvb = schema.$divisibleBy;\n\t\tif (candidate % dvb !== 0) {\n\t\t\tthis.report('must be divisible by ' + dvb);\n\t\t}\n\t}\n};\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', $divisibleBy: 5 },\n\t\tipsum: { type: 'number', $divisibleBy: 3 }\n\t}\n};\n\ninspector.Validation.extend(custom);\n\nvar candidate = {\n\tlorem: 10,\n\tipsum: 8\n};\n\ninspector.validate(schema, candidate);\n/*\n\tAs you can see, no more object than schema and candidate has been provided.\n\tTherefore we can use `$divisibleBy` everywhere in all schemas, for each\n\tinspector.validate() call.\n*/\n```\n\n\u003ca name=\"cf_context\" /\u003e\n### Context\n\nEvery function you declare as a custom parameter, or with `exec` field will be\ncalled with a context. This context allows you to access properties, like\n`this.report()` function, but also `this.origin`, which is equal to the object\nsent to `inspector.validate()` or `inspector.sanitize()`.\n\n__Example__\n\n```javascript\n// ...\nvar schema = { ... };\nvar custom = {\n\tdivisibleBy: function (schema, candidate) {\n\t\t// this.origin === [12, 23, 34, 45]\n\t\t// ...\n\t}\n};\nvar candidate = [12, 23, 34, 45];\nvar result = inspector.validate(schema, candidate, custom);\n// ...\n```\n\n## Asynchronous call\n\n\u003ca name=\"a_howTo\" /\u003e\n### How to\n\nAll of the examples above used synchronous calls (the simplest). But sometimes you\nwant to call validation or sanitization asynchronously, in particular with\n`exec` and custom fields. It's pretty simple: To do so, just send a callback\nas extra parameter. It takes 2 parameters: error and result. Actually\nSchema-Inspector should send back no error as it should not throw any if called\nsynchronously. But if you want to send back and error in your custom function,\ninspection will be interrupted, and you will be able to retrieve it in your\ncallback.\n\nYou also have to declare a callback in your `exec` or custom function to make\nSchema-Inspector call it asynchronously, else it will be call synchronously.\nThat means you may use `exec` synchronous function normally even during\nand asynchronous call.\n\n__Example__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = { ...\t};\nvar candidate = { ... };\n\ninspector.validate(schema, candidate, function (err, result) {\n\tconsole.log(result.format());\n});\n```\n\n__Example with custom field__\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = { ...\t};\nvar candidate = { ... };\nvar custom = { ... };\n\ninspector.validate(schema, candidate, custom, function (err, result) {\n\tconsole.log(result.format());\n});\n```\n\nHere is a full example where you may have to use it:\n\n```javascript\nvar inspector = require('schema-inspector');\n\nvar schema = {\n\ttype: 'object',\n\tproperties: {\n\t\tlorem: { type: 'number', $divisibleBy: 4 },\n\t\tipsum: { type: 'number', $divisibleBy: 5 },\n\t\tdolor: { type: 'number', $divisibleBy: 0, optional: true }\n\t}\n};\n\nvar custom = {\n\tdivisibleBy: function (schema, candidate, callback) { // Third parameter is declared:\n\t\t// Schema-Inspector will wait this function to call this `callback` to keep running.\n\t\tvar dvb = schema.$divisibleBy;\n\t\tif (typeof dvb !== 'number' || typeof candidate !== 'number') {\n\t\t\treturn callback();\n\t\t}\n\t\tvar self = this;\n\t\tprocess.nextTick(function () {\n\t\t\tif (dvb === 0) {\n\t\t\t\treturn callback(new Error('Schema error: Divisor must not equal 0'));\n\t\t\t}\n\t\t\tvar r = candidate / dvb;\n\t\t\tif ((r | 0) !== r)  {\n\t\t\t\tself.report('should be divisible by ' + dvb);\n\t\t\t}\n\t\t\tcallback();\n\t\t});\n\t}\n};\n\nvar candidate = {\n\tlorem: 12,\n\tipsum: 25\n};\n\ninspector.validate(schema, candidate, custom, function (err, result) {\n\tconsole.log(result.format());\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreip%2Fschema-inspector-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreip%2Fschema-inspector-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreip%2Fschema-inspector-testing/lists"}