{"id":13783969,"url":"https://github.com/korzio/djv","last_synced_at":"2025-05-16T11:04:28.311Z","repository":{"id":55492584,"uuid":"50596294","full_name":"korzio/djv","owner":"korzio","description":"Dynamic JSON Schema Validator -  Supports draft-04/06","archived":false,"fork":false,"pushed_at":"2024-02-12T20:03:39.000Z","size":1123,"stargazers_count":288,"open_issues_count":30,"forks_count":29,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-14T09:03:35.348Z","etag":null,"topics":["json-schema","validator"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/djv","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/korzio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-28T16:26:01.000Z","updated_at":"2025-03-17T15:15:25.000Z","dependencies_parsed_at":"2024-06-18T12:38:33.659Z","dependency_job_id":"00c692a4-656b-4d41-bc6c-d5a404d45ead","html_url":"https://github.com/korzio/djv","commit_stats":{"total_commits":211,"total_committers":10,"mean_commits":21.1,"dds":0.09004739336492895,"last_synced_commit":"cf1cce98ba9d45d2d36bfd0c6e91e8d26f2d2f60"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korzio%2Fdjv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korzio%2Fdjv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korzio%2Fdjv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korzio%2Fdjv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korzio","download_url":"https://codeload.github.com/korzio/djv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518384,"owners_count":22084374,"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":["json-schema","validator"],"created_at":"2024-08-03T19:00:33.782Z","updated_at":"2025-05-16T11:04:28.292Z","avatar_url":"https://github.com/korzio.png","language":"JavaScript","funding_links":[],"categories":["Who Uses the Test Suite","others"],"sub_categories":["JavaScript"],"readme":"[![Build Status](https://travis-ci.org/korzio/djv.svg?branch=master)](https://travis-ci.org/korzio/djv)\n[![Join the chat at https://gitter.im/korzio/djv](https://badges.gitter.im/korzio/djv.svg)](https://gitter.im/korzio/djv)\n\n# djv \u003ca name=\"title\"\u003e\u003c/a\u003e\n\nDynamic JSON-Schema Validator\n\n[Official Documentation](http://korzio.github.io/djv/)\n\nCurrent package supports **JSON-Schema v6 and v4**. It contains utils to validate objects against schemas.\nThis is a part of **djv** packages aimed to work with json-schema.\n\n- [djv](https://github.com/korzio/djv) validate object against schemas\n- [djvi](https://github.com/korzio/djvi) instantiate objects by schema definition\n- [jvu](https://github.com/korzio/jvu) utilities for declarative, FP usage\n- [@djv/draft-04](https://github.com/korzio/@djv/draft-04) environment updates to support *draft-04*\n\nAny contributions are welcome. Check the [contribution guide](./CONTRIBUTING.md).\nSince version **1.2.0** *djv* package supports `draft-06`. Version **2.0.0** makes `draft-06` the default schema version. To use other versions check the [environment section](#environment).\n\n## Table of contents \u003ca name=\"content\"\u003e\u003c/a\u003e\n\n* [djv](#title)\n* [Table of contents](#content)\n* [Install](#install)\n* [Usage](#usage)\n* [API](#api)\n  * [Environment](#environment)\n  * [addSchema](#addSchema)\n  * [validate](#validate)\n  * [removeSchema](#removeSchema)\n  * [resolve](#resolve)\n  * [export](#export)\n  * [import](#import)\n  * [addFormat](#addFormat)\n  * [setErrorHandler](#errorHandler)\n  * [useVersion](#useVersion)\n* [Tests](#tests)\n* [References](#references)\n\n## Install \u003ca name=\"install\"\u003e\u003c/a\u003e\n\n```bash\nnpm install djv\n```\n\nor\n\n```html\n\u003cscript src=\"djv.js\"\u003e\u003c/script\u003e\n```\n\nThere are 2 versions of validator\n\n- `./lib/djv.js` a default one, not uglified and not transpiled\n- `./djv.js` a built one with a webpack, babel and uglify (preferable for frontend)\n\n## Usage \u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n```javascript\nconst env = new djv();\nconst jsonSchema = {\n  \"common\": {\n    \"properties\": {\n      \"type\": {\n        \"enum\": [\"common\"]\n      }\n    },\n    \"required\": [\n      \"type\"\n    ]\n  }\n};\n\n// Use `addSchema` to add json-schema\nenv.addSchema('test', jsonSchema);\nenv.validate('test#/common', { type: 'common' });\n// =\u003e undefined\n\nenv.validate('test#/common', { type: 'custom' });\n// =\u003e 'required: data'\n```\n\n## API \u003ca name=\"api\"\u003e\u003c/a\u003e\n\n### Environment \u003ca name=\"environment\"\u003e\u003c/a\u003e\n\nTo instantiate *djv* environment\n\n```javascript\nconst djv = require('djv');\nconst env = djv({\n  version: 'draft-06', // use json-schema draft-06\n  formats: { /*...*/ }, // custom formats @see #addFormat\n  errorHandler: () =\u003e { /*...*/ }, // custom error handler, @see #setErrorHandler\n});\n```\n\nTo use a previous version of JSON-Schema draft, use a [`draft-04` plugin](https://www.npmjs.com/package/@korzio/djv-draft-04), specified in [*optionalDependencies*](https://docs.npmjs.com/files/package.json#optionaldependencies) of *djv* package.\n\n```javascript\nconst env = new djv({ version: 'draft-04' });\n```\n\n### addSchema(name: string, schema: object?) -\u003e resolved: object \u003ca name=\"addSchema\"\u003e\u003c/a\u003e\n\nAdd a schema to a current djv environment,\n\n```javascript\nenv.addSchema('test', jsonSchema);\n/* =\u003e {\n  fn: function f0(data){...}\n  name: 'test'\n  schema: ...\n} */\n```\n\n### validate(name: string, object: object) -\u003e error: string \u003ca name=\"validate\"\u003e\u003c/a\u003e\n\nCheck if object is valid against the schema\n\n```javascript\nenv.validate('test#/common', { type: 'common' });\n// =\u003e undefined\n\nenv.validate('test#/common', { type: 'custom' });\n// =\u003e 'required: data'\n```\n\nwhere\n\n* *name* - schema path in current environment\n* *object* - object to validate\n* *error* - undefined if it is valid\n\n### removeSchema(name: string) \u003ca name=\"removeSchema\"\u003e\u003c/a\u003e\n\nRemove a schema or the whole structure from the djv environment\n\n```javascript\nenv.removeSchema('test');\n```\n\n### resolve(name: string?) \u003ca name=\"resolve\"\u003e\u003c/a\u003e\n\nResolve the name by existing environment\n\n```javascript\nenv.resolve('test');\n// =\u003e { name: 'test', schema: {} }, fn: ... }\n```\n\n### export(name: string?) -\u003e state: object \u003ca name=\"export\"\u003e\u003c/a\u003e\n\nExport the whole structure object from environment or resolved by a given name\n\n```javascript\nenv.export();\n// =\u003e { test: { name: 'test', schema: {}, ... } }\n```\n\nwhere **state** is an internal structure or only resolved schema object\n\n### import(config: object) \u003ca name=\"import\"\u003e\u003c/a\u003e\n\nImport all found structure objects to internal environment structure\n\n```javascript\nenv.import(config);\n```\n\n### addFormat(name: string, formatter: string/function) \u003ca name=\"addFormat\"\u003e\u003c/a\u003e\n\nAdd formatter to djv environment. When a string is passed it is interpreted as an expression which when returns `true` goes with an error, when returns `false` then a property is valid. When a function is passed it will be executed during schema compilation with a current schema and template helper arguments.\n\n```javascript\nenv.addFormat('UpperCase', '%s !== %s.toUpperCase()');\n// or\nenv.addFormat('isOk', function(schema, tpl){\n  return `!${schema.isOk}`;\n});\nenv.validate('ok', 'valid') // =\u003e undefined if schema contains isOk property\n```\n\n### setErrorHandler(errorHandler: function) \u003ca name=\"errorHandler\"\u003e\u003c/a\u003e\n\nSpecify custom error handler which will be used in generated functions when problem found.\nThe function should return a string expression, which will be executed when generated validator function is executed. The simplist use case is the default one @see `template/defaultErrorHandler`\n```javascript\n function defaultErrorHandler(errorType) {\n   return `return \"${errorType}: ${tpl.data}\";`;\n }\n```\nIt returns an expression 'return ...', so the output is an error string.\n\n```javascript\ndjv({ errorHandler: () =\u003e 'return { error: true };' }) // =\u003e returns an object\ndjv({\n  errorHandler(type) {\n    return `errors.push({\n      type: '${type}',\n      schema: '${this.schema[this.schema.length - 1]}',\n      data: '${this.data[this.data.length - 1]}'\n    });`;\n  }\n});\n```\nWhen a custom error handler is used, the template body function adds a `error` variable inside a generated validator, which can be used to put error information. `errorType` is always passed to error handler function. Some validate utilities put extra argument, like f.e. currently processed property value. Inside the handler context is a templater instance, which contains `this.schema`, `this.data` paths arrays to identify validator position.\n@see test/index/setErrorHandler for more examples\n\n### useVersion(version: string, configure: function) \u003ca name=\"useVersion\"\u003e\u003c/a\u003e\n\nTo customize environment provide a `configure` function which will update configuration for *djv* instance.\n\n```javascript\nenv.useVersion('draft-04')\n// or\nenv.useVersion('custom', configure)\n```\n\n`Configure` will get internal properties as an argument. Check the [@korzio/djv-draft-04 code](https://github.com/korzio/-djv-draft-04/blob/master/index.js).\n\n```javascript\nexposed = {\n  properties,\n  keywords,\n  validators,\n  formats,\n  keys,\n  transformation,\n}\n```\n\n**!Important** Modifying them will affect all *djv* instances in an application.\n\n## Tests \u003ca name=\"tests\"\u003e\u003c/a\u003e\n\n```\nnpm test\n```\n\n## References \u003ca name=\"references\"\u003e\u003c/a\u003e\n\n* [JSON-Schema Specification](http://json-schema.org/)\n* [JSON-Schema Benchmark](https://github.com/ebdrup/json-schema-benchmark)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorzio%2Fdjv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorzio%2Fdjv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorzio%2Fdjv/lists"}