{"id":13516695,"url":"https://github.com/americanexpress/jest-json-schema","last_synced_at":"2025-04-12T23:42:13.755Z","repository":{"id":21936008,"uuid":"93082274","full_name":"americanexpress/jest-json-schema","owner":"americanexpress","description":"✨ JSON schema matcher for Jest","archived":false,"fork":false,"pushed_at":"2024-07-22T11:39:21.000Z","size":2024,"stargazers_count":170,"open_issues_count":2,"forks_count":14,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-04-12T23:42:07.445Z","etag":null,"topics":["jest","json","json-schema","matchers","one-app","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/americanexpress.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-01T17:24:49.000Z","updated_at":"2025-02-11T21:57:39.000Z","dependencies_parsed_at":"2023-01-13T21:45:36.026Z","dependency_job_id":"85631610-8e86-4139-93ed-50556cdbbf77","html_url":"https://github.com/americanexpress/jest-json-schema","commit_stats":{"total_commits":75,"total_committers":21,"mean_commits":"3.5714285714285716","dds":0.8,"last_synced_commit":"c5b80cd6637d5fde041254d60ed40860365cd0a6"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/americanexpress%2Fjest-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/americanexpress%2Fjest-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/americanexpress%2Fjest-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/americanexpress%2Fjest-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/americanexpress","download_url":"https://codeload.github.com/americanexpress/jest-json-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"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":["jest","json","json-schema","matchers","one-app","testing"],"created_at":"2024-08-01T05:01:24.967Z","updated_at":"2025-04-12T23:42:13.726Z","avatar_url":"https://github.com/americanexpress.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Packages"],"sub_categories":["Matchers"],"readme":"# jest-json-schema\n\n[![npm version](https://badge.fury.io/js/jest-json-schema.svg)](https://badge.fury.io/js/jest-json-schema)\n[![Build Status](https://travis-ci.org/americanexpress/jest-json-schema.svg?branch=master)](https://travis-ci.org/americanexpress/jest-json-schema)\n[![Mentioned in Awesome Jest](https://awesome.re/mentioned-badge.svg)](https://github.com/jest-community/awesome-jest)\n\n\u003e JSON schema matcher for [jest](https://www.npmjs.com/package/jest)\n\n\u003e Want to get paid for your contributions to `jest-json-schema`?\n\u003e Send your resume to oneamex.careers@aexp.com\n\n## Matchers included\n\n### `toMatchSchema(schema)`\n\nValidates that an object matches the given [JSON schema](http://json-schema.org/)\n\n```js\nit('validates my json', () =\u003e {\n  const schema = {\n    properties: {\n      hello: { type: 'string' },\n    },\n    required: ['hello'],\n  };\n  expect({ hello: 'world' }).toMatchSchema(schema);\n});\n```\n\n## Installation\n\n```bash\n$ npm install --save-dev jest-json-schema\n```\n\n## Usage\n\nIn any test file:\n\n```js\nimport { matchers } from 'jest-json-schema';\nexpect.extend(matchers);\n```\n\nOr if you want it available for all test files then set it up the same way in a\n[test framework script file](http://facebook.github.io/jest/docs/configuration.html#setuptestframeworkscriptfile-string)\n\nYou can pass [Ajv options](https://ajv.js.org/options.html) using\n`matchersWithOptions` and passing it your options object. The only option passed\nby default is `allErrors: true`.\n\n```js\nimport { matchersWithOptions } from 'jest-json-schema';\n\nconst formats = {\n  bcp47: /^[a-z]{2}-[A-Z]{2}$/,\n}\n\nexpect.extend(matchersWithOptions({ formats }));\n```\n\nAdditionally you can also use a callback to further configure and extend\nthe Ajv instance used by the matchers:\n\n```js\nimport ajvKeywords from 'ajv-keywords';\nimport { matchersWithOptions } from 'jest-json-schema';\n\nconst formats = {\n  bcp47: /^[a-z]{2}-[A-Z]{2}$/,\n}\n\nexpect.extend(matchersWithOptions({ formats }, (ajv) =\u003e {\n  // This uses the `ajv-keywords` library to add pre-made\n  // custom keywords to the Ajv instance.\n  ajvKeywords(ajv, ['typeof', 'instanceof']);\n}));\n```\n\nYou can also customize the `Ajv` class with the `AjvClass` option:\n\n```js\nimport Ajv2020 from 'ajv/dist/2020'\nimport { matchersWithOptions } from 'jest-json-schema';\n\nexpect.extend(matchersWithOptions({ AjvClass: Ajv2020 }));\n```\n\n### Verbose errors\n\nAjv supports a verbose option flag which enables more information about individual\nerrors. This extra information can mean that we can output to Jest more meaningful\nerrors that can help the development process:\n\n```js\nconst { matchersWithOptions } = require('jest-json-schema');\n\nexpect.extend(matchersWithOptions({\n  verbose: true\n}));\n\ntest('check that property errors are outputted', () =\u003e {\n  const schema = {\n    $id: 'testSchema',\n    type: 'object',\n    properties: {\n      name: {\n        type: 'string',\n      },\n      dob: {\n        type: 'string',\n        format: 'date',\n      },\n    },\n  };\n\n  const invalidData = {\n    name: null,\n    dob: '02-29-2000',\n  };\n\n  expect(() =\u003e {\n    expect(invalidData).toMatchSchema(schema)\n  }).toThrowErrorMatchingInlineSnapshot(`\n\"expect(received).toMatchSchema(schema)\n\nReceived:\n  .name should be string\n    Received: \u003cnull\u003e\n    Path: testSchema#/properties/name/type\n  .dob should match format \\\\\"date\\\\\"\n    Received: \u003cstring\u003e 02-29-2000\n    Path: testSchema#/properties/dob/format\n\"\n`);\n});\n```\n\n### Example using multiple schema files\n\nIf you organise your schemas into separate files and use refs which point to the\nvarious different schemas, it will be important to include those dependent\nschema files when extending Jest's `expect` handler, using the `matchersWithOptions`\ninterface:\n\n#### schemaA.json\n\n```json\n{\n  \"$id\": \"schemaA\",\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"description\": \"Example of a definition schema.\",\n  \"definitions\": {\n    \"testA\": {\n      \"type\": \"number\",\n      \"const\": 1\n    },\n    \"testB\": {\n      \"type\": [\"null\", \"string\"]\n    }\n  }\n}\n```\n\n#### schemaB.json\n\n```json\n{\n  \"$id\": \"schemaB\",\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"description\": \"Example of a schema that references another schema.\",\n  \"$ref\": \"schemaA#/definitions/testB\"\n}\n```\n\n#### schemaA.test.js\n\n```js\nconst { matchersWithOptions } = require('jest-json-schema');\n\n// Local schema files are imported like normal. If you use TypeScript you\n// will need to ensure `--resolveJsonModule` is enabled.\nconst schemaA = require('./schemaA.json');\nconst schemaB = require('./schemaB.json');\n\nexpect.extend(matchersWithOptions({\n  // Loading in a schema which is comprised only of definitions,\n  // which means specific test schemas need to be created.\n  // This is good for testing specific conditions for definition schemas.\n  schemas: [schemaA]\n});\n\ntest('schemaA is valid', () =\u003e {\n  expect(schemaA).toBeValidSchema();\n});\n\ntest('using schemaA to build a test schema to test a specific definition', () =\u003e {\n  // This is a test schema which references a definition in one of the\n  // pre-loaded schemas. This can allow us to write tests for specific\n  // definitions.\n  const testSchema = {\n    $ref: 'schemaA#/definitions/testA'\n  };\n\n  expect(testSchema).toBeValidSchema();\n\n  // Valid\n  expect(1).toMatchSchema(testSchema);\n\n  // This example runs through a number of values that we know don't match\n  // the schema, ensuring that any future changes to the schema will require\n  // the test to be updated.\n  ['1', true, false, null, [], {}].forEach(value =\u003e {\n     expect(value).not.toMatchSchema(testSchema);\n  });\n});\n\ntest('using schemaB which already references a definition in schemaA', () =\u003e {\n  expect(schemaB).toBeValidSchema();\n\n  // Valid\n  ['', '1', null].forEach(value =\u003e {\n    expect(value).toMatchSchema(schemaB);\n  });\n\n  // Invalid\n  ['1', true, false, [], {}].forEach(value =\u003e {\n     expect(value).not.toMatchSchema(schemaB);\n  });\n});\n```\n## TypeScript support\n\nIf you would like to use `jest-json-schema` library in your TypeScript project, remember to install type definitions from `@types/jest-json-schema` package.\n\n```\nnpm install --save-dev @types/jest-json-schema\n```\n\nOr if `yarn` is your package manager of choice:\n\n```\nyarn add @types/jest-json-schema --dev\n```\n\n## Contributing\n\nWe welcome Your interest in the American Express Open Source Community on Github.\nAny Contributor to any Open Source Project managed by the American Express Open\nSource Community must accept and sign an Agreement indicating agreement to the\nterms below. Except for the rights granted in this Agreement to American Express\nand to recipients of software distributed by American Express, You reserve all\nright, title, and interest, if any, in and to Your Contributions. Please [fill\nout the Agreement](https://cla-assistant.io/americanexpress/).\n\nPlease feel free to open pull requests and see [CONTRIBUTING.md](./CONTRIBUTING.md) to learn how to get started contributing.\n\n## License\n\nAny contributions made under this project will be governed by the [Apache License\n 2.0](https://github.com/americanexpress/jest-json-schema/blob/master/LICENSE.txt).\n\n## Code of Conduct\n\nThis project adheres to the [American Express Community Guidelines](https://github.com/americanexpress/jest-json-schema/wiki/Code-of-Conduct).\nBy participating, you are expected to honor these guidelines.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famericanexpress%2Fjest-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famericanexpress%2Fjest-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famericanexpress%2Fjest-json-schema/lists"}