{"id":13783979,"url":"https://github.com/json-schema-org/json-schema-test-suite-npm","last_synced_at":"2025-09-27T00:31:36.615Z","repository":{"id":34881496,"uuid":"170530587","full_name":"json-schema-org/json-schema-test-suite-npm","owner":"json-schema-org","description":"NPM / node.js-specific support for json-schema-org/JSON-Schema-Test-Suite","archived":true,"fork":false,"pushed_at":"2022-07-21T12:17:02.000Z","size":294,"stargazers_count":5,"open_issues_count":1,"forks_count":5,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-04-14T10:15:57.411Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/json-schema-org.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":"2019-02-13T15:25:54.000Z","updated_at":"2023-01-27T22:55:03.000Z","dependencies_parsed_at":"2022-08-08T02:15:29.554Z","dependency_job_id":null,"html_url":"https://github.com/json-schema-org/json-schema-test-suite-npm","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/json-schema-org%2Fjson-schema-test-suite-npm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json-schema-org%2Fjson-schema-test-suite-npm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json-schema-org%2Fjson-schema-test-suite-npm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json-schema-org%2Fjson-schema-test-suite-npm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/json-schema-org","download_url":"https://codeload.github.com/json-schema-org/json-schema-test-suite-npm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219867856,"owners_count":16554371,"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-08-03T19:00:34.005Z","updated_at":"2025-09-27T00:31:31.357Z","avatar_url":"https://github.com/json-schema-org.png","language":"JavaScript","funding_links":["https://opencollective.com/json-schema"],"categories":["Who Uses the Test Suite"],"sub_categories":["Node.js"],"readme":"@json-schema-org/tests\n======================\n[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](https://github.com/json-schema-org/.github/blob/main/CODE_OF_CONDUCT.md)\n[![Project Status: Inactive – The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows.](https://www.repostatus.org/badges/latest/inactive.svg)](https://www.repostatus.org/#inactive)\n[![Financial Contributors on Open Collective](https://opencollective.com/json-schema/all/badge.svg?label=financial+contributors)](https://opencollective.com/json-schema) \n\n**This repository is archived due to needing maintainers who can help keep it up to date. If you are interested in helping, please reach out (via an issue or Slack).**\n\nNPM / node.js-specific support for the [JSON Schema test suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite)\n\n[![Build Status](https://travis-ci.org/json-schema-org/json-schema-test-suite-npm.svg?branch=master)](https://travis-ci.org/json-schema-org/json-schema-test-suite-npm)\n\nThe JSON Schema Test Suite is meant to be a language agnostic test suite for testing JSON Schema validation libraries.\nIt is generally added to projects as a git submodule. However, to simplify things for Node.js developers, the test suite has also\nbeen made available as an npm package.\n\n```sh\nnpm install @json-schema-org/tests\n```\n\n### Usage:\n\nThere are a number of ways to load tests from the suite:\n\n```js\nconst testSuite = require('@json-schema-org/tests');\n\n// this will load all (required and optional) draft6 tests\nconst tests = testSuite.loadSync();\n\n// optional `filter` is a function that takes 3 arguments (filename, parent, optional)\n// and returns true if the test should be included. The optional argument is true\n// for all files under the `\u003cdraft\u003e/optional` directory.\n// optional `draft` should be either `'draft3'`, `'draft4'` or `'draft6'`\n\nconst tests = testSuite.loadSync(filter, draft);\n\n// convenience functions:\n\n// The following take an optional `filter` as described previously (undefined will load all tests)\nconst draft3 = testSuite.draft3();\nconst draft4 = testSuite.draft4();\nconst draft6 = testSuite.draft6();\n\n// The following take an optional `draft` argument (defaults to 'draft6')\nconst all = testSuite.loadAllSync();\nconst required = testSuite.loadRequiredSync();\nconst optional = testSuite.loadOptionalSync();\n```\n\n\nThe return value of these functions is an array of objects that correspond to each file under `tests/\u003cdraft\u003e` that\npassed the filter (the default is all, so the array will also include all the optional files).\n\nEach object has the following structure (using `tests/draft4/additionalItems.json` as an example):\n\n```js\n{\n  name:    'additionalItems',\n  file:     'additionalItems.json',\n  optional: false,  // true if a file under the optional directory\n  schemas:  []\n}\n```\n\nThe `schemas` property contains the array of objects loaded from the test file.\nEach object consists of a schema and description, along with a number of tests used for validation. Using the first schema object in the array from `tests/draft4/additionalItems.json` as an example:\n\n```js\n{\n  description: 'additionalItems as schema',\n  schema: {\n    items: [{}],\n    additionalItems: { type: \"integer\" }\n  },\n  tests: [\n    {\n      description: \"additional items match schema\",\n      data: [ null, 2, 3, 4 ],\n      valid: true\n    },\n    {\n      description: \"additional items do not match schema\",\n      data: [ null, 2, 3, \"foo\" ],\n      valid: false\n    }\n  ]\n}\n```\n\n### Testing a JSON Validator\n\nYou can apply a validator against all the tests. You need to create a validator factory function that takes a JSON schema and an options argument, and returns an object with a validate method. The validate function should take a JSON object to be validated against the schema. It should return an object with a valid property set to true or false, and if not valid, an errors property that is an array of one or more validation errors.\n\nThe following are examples of `Tiny Validator (tv4)` and `z-schema` validator factories used by the unit test.\n\n\n#### tv4\n\n```js\nconst tv4 = require('tv4');\n\nconst tv4Factory = function (schema, options) {\n  return {\n    validate: function (json) {\n      try {\n        const valid = tv4.validate(json, schema);\n        return valid ? { valid: true } : { valid: false, errors: [ tv4.error ] };\n      } catch (err) {\n        return { valid: false, errors: [err.message] };\n      }\n    }\n  };\n};\n```\n\n#### ZSchema\n\n```js\nconst ZSchema = require('z-schema');\n\nconst zschemaFactory = function (schema, options) {\n  const zschema = new ZSchema(options);\n\n  return {\n    validate: function (json) {\n      try {\n        const valid = zschema.validate(json, schema);\n        return valid ? { valid: true } : { valid: false, errors: zschema.getLastErrors() };\n      } catch (err) {\n        return { valid: false, errors: [err.message] };\n      }\n    }\n  };\n};\n```\n\n#### Testing the Validator\n\nUsing a validator factory as described above, you can test it as follows.\n\n```js\nconst testSuite = require('json-schema-test-suite');\n\nconst tests = testSuite.testSync(factory);\n```\n\nThe `tests` return value is as described previously in the Usage section, with an additional property for each test object that corresponds to the test result:\n\n```js\n{\n  description: 'additionalItems as schema',\n  schema: {\n    items: [{}],\n    additionalItems: { type: \"integer\" }\n  },\n  tests: [\n    {\n      description: \"additional items match schema\",\n      data: [ null, 2, 3, 4 ],\n      valid: true,\n      result: {\n        valid: false,\n        errors: [ ... ]\n      }\n    },\n    {\n      description: \"additional items do not match schema\",\n      data: [ null, 2, 3, \"foo\" ],\n      valid: false,\n      result: {\n        true\n      }\n    }\n  ]\n}\n```\n\n### Tests\n\nYou can run the tests by doing:\n\n```sh\nnpm test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson-schema-org%2Fjson-schema-test-suite-npm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjson-schema-org%2Fjson-schema-test-suite-npm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson-schema-org%2Fjson-schema-test-suite-npm/lists"}