{"id":26668999,"url":"https://github.com/soufantech/supertest-openapi-validator","last_synced_at":"2025-06-11T00:04:57.825Z","repository":{"id":125749914,"uuid":"340159445","full_name":"soufantech/supertest-openapi-validator","owner":"soufantech","description":"Performs OpenAPI contract checks with supertest.","archived":false,"fork":false,"pushed_at":"2021-03-08T21:22:35.000Z","size":387,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T21:45:31.511Z","etag":null,"topics":["contract-testing","openapi","supertest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/soufantech.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-18T19:44:11.000Z","updated_at":"2022-01-13T15:39:07.000Z","dependencies_parsed_at":"2023-08-12T17:47:21.076Z","dependency_job_id":null,"html_url":"https://github.com/soufantech/supertest-openapi-validator","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":"soufantech/node-ts-lib-boilerplate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soufantech%2Fsupertest-openapi-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soufantech%2Fsupertest-openapi-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soufantech%2Fsupertest-openapi-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soufantech%2Fsupertest-openapi-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soufantech","download_url":"https://codeload.github.com/soufantech/supertest-openapi-validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soufantech%2Fsupertest-openapi-validator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259172954,"owners_count":22816556,"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":["contract-testing","openapi","supertest"],"created_at":"2025-03-25T21:36:09.632Z","updated_at":"2025-06-11T00:04:57.817Z","avatar_url":"https://github.com/soufantech.png","language":"TypeScript","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://avatars2.githubusercontent.com/u/61063724?s=200\u0026v=4\" width=\"100px\"\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003e@soufantech/supertest-openapi-validator\u003c/h1\u003e\n  \u003cp\u003ePerforms OpenAPI contract checks with supertest\u003c/p\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![typescript-image]][typescript-url] [![jest-image]][jest-url] [![npm-image]][npm-url]\n\n\u003c/div\u003e\n\n## Install\n\n```console\nyarn add @soufantech/supertest-openapi-validator\n```\n\n❗ This package is currently private. You'll need to set a valid npm token to `SOUFAN_NPM_TOKEN` environment variable in order to install it.\n\n## Usage\n\n### Specification file loading\n\nLoad the specification file (`openapi.yaml` in the example below) **asynchronously**.\n\n```js\n/// file: get-contract.js\n\nimport path from 'path';\nimport {\n  loadOpenapiContract,\n} from '@soufantech/supertest-openapi-validator';\n\n// contract is a promise\nconst contract = loadOpenapiContract(\n  path.resolve(__dirname, 'openapi.yaml'),\n);\n\nexport default (): =\u003e contract;\n```\n\nIt is also possible to create multiple contract objects when there is multiple specification files.\n\nAttention: `loadOpenapiContract` will throw an `Error` exception if the specification file cannot be read or is malformed.\n\nAttention: `loadOpenapiContract` returns a promise that resolves to the contract object.\n\nLoading the contract test files can be done like this:\n\n```js\n/// file: foo.test.js\n\nimport getContract from './get-contract';\n\ntest('Loads a contract.', async () =\u003e {\n  // It's necessary to await, because\n  // getContract returns a promise.\n  const contract = await getContract();\n});\n```\n\n### Validation\n\nCreate a validator using the `createValidator` contract method and call `getChecker` on the returned validator object to get a validator callback for use in supertest's `expect`:\n\n```js\n/// file: app.test.js\n\nimport getContract from './get-contract';\nimport supertest from './supertest';\nimport app from './app'; // express app\n\nconst request = supertest(app);\n\ntest('Compliant request and response pass validation.', async () =\u003e {\n  const contract = await getContract();\n  const validator = contract.createValidator();\n\n  await request.get('/health').expect(validator.getChecker());\n});\n```\n\nOptionally, use the `validate` method to validate the object returned by supertest directly (this approach is useful for inspecting and debugging). The `validate` method returns a [`Result`](https://github.com/soufantech/result) object, so `get`, `getOrThrow` and many other `Result` methods can be used to unwrap the supertest response object or the openapi validation error. \n\nTesting using `validate` with `get` and `getOrThrow`:\n\n```js\n/// file: app.test.js\n\nimport { OpenapiOperationError } from '@soufantech/supertest-openapi-validator';\nimport getContract from './get-contract';\nimport supertest from './supertest';\nimport app from './app'; // express app\n\nconst request = supertest(app);\n\ntest('Using `validate` with `getOrThrow`.', async () =\u003e {\n  const contract = await getContract();\n  const validator = contract.createValidator();\n\n  const response = await request.get('/health');\n\n  // returns the response object untouched or throws an exception if\n  // validation fails.\n  const validatedResponse = validator.validate(response).getOrThrow();\n});\n\ntest('Using `validate` with `get`.', async () =\u003e {\n  const contract = await getContract();\n  const validator = contract.createValidator();\n\n  const response = await request.get('/health');\n\n  // returns either the response or the validation error.\n  const responseOrError = validator.validate(response).get();\n\n  // OpenapiOperationError is the base error for all validation fails.\n  expect(responseOrError).not.toBeInstanceOf(OpenapiOperationError);\n});\n```\n\n#### Bypassing specific validations\n\nIt's possible to bypass `response`, `request` and/or `security` validation. To do so, turn off (setting the corresponding property to `false`) one or more validation in the options object passed to the `createValidator` method:\n\n```js\n// bypasses request validation\nconst validator1 = contract.createValidator({ request: false });\n\n// bypasses response validation\nconst validator2 = contract.createValidator({ response: false });\n\n// bypasses security and request validation\nconst validator3 = contract.createValidator({ security: false });\n\n// bypasses security and request validation\nconst validator4 = contract.createValidator(\n  { \n    security: false,\n    request: false,\n  }\n);\n\n// bypasses all validation (may be useful for debugging)\nconst validator5 = contract.createValidator(\n  { \n    security: false,\n    request: false,\n    response: false,\n  }\n);\n\n// bypasses no validation\nconst validator6 = contract.createValidator();\n\n// bypasses no validation (redundant, but valid)\nconst validator7 = contract.createValidator(\n  { \n    security: true,\n    request: true,\n    response: true,\n  }\n);\n```\n\n### Error hierarchy\n\nIn order to identify the source of the error (if it happened during routing, request, response etc.) and to treat them conditionally, there is the following error hierarchy:\n\n```\nError\n└─ OpenapiOperationError\n   ├─ OpenapiOperationRoutingError\n   └─ OpenapiOperationValidationError\n      ├─ OpenapiRequestOperationValidationError \n      ├─ OpenapiResponseOperationValidationError\n      └─ OpenapiSecurityOperationValidationError\n```\n\n#### OpenapiOperationError\n\nThe base class for all operation (routing or validation) errors.\n\nExtends: `Error`  \nProperties:\n\n* `fulfilledHttpRequest` {{ `object` }} :: a non-enumerable object containing the  properties `req` {{ `IHttpRequest` }} and `res` {{ `IHttpResponse` }}.\n\n#### OpenapiOperationRoutingError\n\nA routing error - occurs when the request or response does not match any endpoints described in the specification file.\n\nExtends: `OpenapiOperationError`  \nProperties:\n\n* `originalError` {{ `Error` }}: the original error thrown inside from prism's router.\n\n#### OpenapiOperationValidationError\n\nThe base class for all validation errors.\n\nExtends: `OpenapiOperationError`  \nProperties:\n\n* `diagnostics` {{ `IPrismDiagnostic[]` }} :: an array of prism diagnostic objects.\n\n#### OpenapiRequestOperationValidationError\n\nA validation error that originated from a **request** validation operation.\n\nExtends: `OpenapiOperationValidationError` \n\n#### OpenapiResponseOperationValidationError\n\nA validation error that originated from a **response** validation operation.\n\nExtends: `OpenapiOperationValidationError` \n\n#### OpenapiResponseOperationValidationError\n\nA validation error that originated from a **security** validation operation.\n\nExtends: `OpenapiSecurityValidationError`\n\n---\n\n\u003cdiv align=\"center\"\u003e\n  \u003csub\u003eBuilt with ❤︎ by \u003ca href=\"https://soufan.com.br\"\u003eSouFan\u003c/a\u003e\n\u003c/div\u003e\n\n[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge\u0026logo=typescript\n[typescript-url]: \"typescript\"\n\n[npm-image]: https://img.shields.io/npm/v/@soufantech/supertest-openapi-validator.svg?style=for-the-badge\u0026logo=npm\n[npm-url]: https://npmjs.org/package/@soufantech/supertest-openapi-validator \"npm\"\n\n[jest-image]: https://img.shields.io/badge/tested_with-jest-99424f.svg?style=for-the-badge\u0026logo=jest\n[jest-url]: https://github.com/facebook/jest \"jest\"\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoufantech%2Fsupertest-openapi-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoufantech%2Fsupertest-openapi-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoufantech%2Fsupertest-openapi-validator/lists"}