{"id":15552318,"url":"https://github.com/bahmutov/validate-by-example","last_synced_at":"2025-06-16T21:40:02.711Z","repository":{"id":57390255,"uuid":"82064301","full_name":"bahmutov/validate-by-example","owner":"bahmutov","description":"Derives a JSON schema from an object and then uses it to validate other objects","archived":false,"fork":false,"pushed_at":"2019-11-07T17:41:01.000Z","size":13,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-27T20:29:32.582Z","etag":null,"topics":["derive","json","json-schema","train","validate"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bahmutov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-15T13:37:20.000Z","updated_at":"2024-02-23T19:57:45.000Z","dependencies_parsed_at":"2022-09-12T19:21:10.843Z","dependency_job_id":null,"html_url":"https://github.com/bahmutov/validate-by-example","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fvalidate-by-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fvalidate-by-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fvalidate-by-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fvalidate-by-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/validate-by-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234576555,"owners_count":18855164,"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":["derive","json","json-schema","train","validate"],"created_at":"2024-10-02T14:15:39.742Z","updated_at":"2025-01-18T23:21:04.953Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validate-by-example\n\n\u003e Derives a JSON schema from an object and then uses it to validate other objects\n\n[![NPM][npm-icon] ][npm-url]\n\n[![Build status][ci-image] ][ci-url]\n[![semantic-release][semantic-image] ][semantic-url]\n[![js-standard-style][standard-image]][standard-url]\n\n## Goal\n\nDerive [json schema][json-schema] from one object and use it to verify every\nobject after that. Uses [generate-schema][generate-schema] and\n[is-my-json-valid][is-my-json-valid].\n\n[json-schema]: http://json-schema.org/\n[generate-schema]: https://github.com/nijikokun/generate-schema\n[is-my-json-valid]: https://github.com/mafintosh/is-my-json-valid\n\n## How to use\n\nInstall with `npm install --save validate-by-example`\n\nIn your code first train on an object (gives you a schema) and then validate\nan object.\n\n```js\nconst {train, validate} = require('validate-by-example')\nconst aPerson = {\n  name: 'gleb',\n  age: 37,\n  married: true,\n  lives: {\n    city: 'Boston'\n  }\n}\nconst schema = train(person)\n// now use schema to validate\nconst someone = {\n  name: 'stranger',\n  age: 'twenty',\n  additional: 'some new property',\n  lives: {\n    state: 'MA'\n  }\n}\nconst result = validate(schema, someone)\n// result is an object\n// if everything goes well result.valid will be true\n// otherwise\nif (!result.valid) {\n  console.log(result.errors)\n}\n```\n\nThe above example prints a few errors, because every property is required,\nand no additional properties is allowed.\n\n```json\n[\n  {\n    \"field\": \"data\",\n    \"message\": \"has additional properties\"\n  },\n  {\n    \"field\": \"data.age\",\n    \"message\": \"is the wrong type\"\n  },\n  {\n    \"field\": \"data.married\",\n    \"message\": \"is required\"\n  },\n  {\n    \"field\": \"data.lives\",\n    \"message\": \"has additional properties\"\n  },\n  {\n    \"field\": \"data.lives.city\",\n    \"message\": \"is required\"\n  }\n]\n```\n\nFor error format, see\n[is-my-json-valid](https://github.com/mafintosh/is-my-json-valid#error-messages)\n\nSee more in [tests](src/validate-by-example-spec.js)\n\n## Inferring formats\n\nFor the top level properties, the `train` function tries to infer\nspecific [JSON schema v4 format][formats]. For example\n\n```js\nconst {train} = require('validate-by-example')\nconst schema = train({created: '2017-02-16T15:30:28.370Z'})\n/*\n  {\n    '$schema': 'http://json-schema.org/draft-04/schema#',\n    type: 'object',\n    properties: {\n      created: {\n        type: 'string', required: true, format: 'date-time'\n      }\n    },\n    additionalProperties: false\n  }\n*/\n```\n\n**Note** only hex RGB strings like `#ff0ff` or `#FF00FF` are inferred as\n`color` format.\n\n## Overriding formats\n\nYou can specify [JSON schema v4 format][formats] for each property.\n\n```js\nconst {train, validate} = require('validate-by-example')\nconst user = {\n  email: 'foo@bar.com'\n}\nconst schema = train(user, {email: 'email'})\n/*\n  schema.properties.email will be\n  {type: 'string', required: true, format: 'email'}\n*/\nvalidate({email: 'unknown'})\n```\n\nThis is equivalent to\n\n```js\nconst user = {\n  email: 'foo@bar.com'\n}\nconst schema = train(user)\nschema.properties.email.format = 'email'\nvalidate(schema, {email: 'unknown'})\n```\n\nand will produce the following error\n\n```json\n{\n  \"valid\": false,\n  \"errors\": [\n    {\n      \"field\": \"data.email\",\n      \"message\": \"must be email format\"\n    }\n  ]\n}\n```\n\n[formats]: http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.3\n\n## JSON schema\n\nRelated projects that can derive JSON schema from an object\n\n* [json-schema-by-example](https://github.com/japsu/json-schema-by-example)\n* [json-schema-trainer](https://github.com/davisml/json-schema-trainer)\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt; \u0026copy; 2017\n\n* [@bahmutov](https://twitter.com/bahmutov)\n* [glebbahmutov.com](http://glebbahmutov.com)\n* [blog](http://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/validate-by-example/issues) on Github\n\n## MIT License\n\nCopyright (c) 2017 Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt;\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 OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[npm-icon]: https://nodei.co/npm/validate-by-example.svg?downloads=true\n[npm-url]: https://npmjs.org/package/validate-by-example\n[ci-image]: https://travis-ci.org/bahmutov/validate-by-example.svg?branch=master\n[ci-url]: https://travis-ci.org/bahmutov/validate-by-example\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard-url]: http://standardjs.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fvalidate-by-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fvalidate-by-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fvalidate-by-example/lists"}