{"id":15677682,"url":"https://github.com/eomm/fastify-schema-constraint","last_synced_at":"2025-08-04T06:39:58.751Z","repository":{"id":34486191,"uuid":"175470702","full_name":"Eomm/fastify-schema-constraint","owner":"Eomm","description":"Choose the right JSON schema to apply to your routes based on your constraints","archived":false,"fork":false,"pushed_at":"2024-11-09T10:06:13.000Z","size":29,"stargazers_count":13,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-02T23:49:44.912Z","etag":null,"topics":["fastify","fastify-plugin","json-schema"],"latest_commit_sha":null,"homepage":null,"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/Eomm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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,"zenodo":null},"funding":{"github":"Eomm"}},"created_at":"2019-03-13T17:44:29.000Z","updated_at":"2024-11-09T10:06:16.000Z","dependencies_parsed_at":"2025-04-30T08:48:07.903Z","dependency_job_id":null,"html_url":"https://github.com/Eomm/fastify-schema-constraint","commit_stats":{"total_commits":20,"total_committers":4,"mean_commits":5.0,"dds":0.5,"last_synced_commit":"dc0d40c2edb8fe3641b9b6119859f59c7006f9d5"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/Eomm/fastify-schema-constraint","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-schema-constraint","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-schema-constraint/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-schema-constraint/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-schema-constraint/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/fastify-schema-constraint/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-schema-constraint/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268658684,"owners_count":24285744,"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","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["fastify","fastify-plugin","json-schema"],"created_at":"2024-10-03T16:10:16.056Z","updated_at":"2025-08-04T06:39:58.723Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Eomm"],"categories":[],"sub_categories":[],"readme":"# fastify-schema-constraint\n\n[![ci](https://github.com/Eomm/fastify-schema-constraint/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Eomm/fastify-schema-constraint/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/fastify-schema-constraint)](https://www.npmjs.com/package/fastify-schema-constraint)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\nChoose the right JSON schema to apply to your routes based on your constraints.\nWith this plugin, you will be able to set multiple schemas per `route` and, programmatically,\nchoose which one applies.\n\nEx: you can choose which JSON schema to apply, based on the `req.headers` values.\n\n\n## Install\n\n```\nnpm install fastify-schema-constraint\n```\n\n### Compatibility\n\n| Plugin version | Fastify version |\n| ------------- |:---------------:|\n| `^4.0.0` | `^5.0.0` |\n| `^3.0.0` | `^4.0.0` |\n| `^2.0.0` | `^3.0.0` |\n| `^1.0.0` | `^2.0.0` |\n\n## Usage\n\nThis plugin will act on `preHandler` hook and will verify if the payload of the `body`, `querystring`,\n`params` or `headers` fulfil the constraint condition.\n\n```js\n// Define the set of your JSON Schema. They MUST be in an array assigned to `oneOf` property\nconst routeSchema = {\n  oneOf: [\n    { $id: '#schema1', type: 'object', properties: { mul5: { type: 'number', multipleOf: 5 } } },\n    { $id: '#schema2', type: 'object', properties: { mul3: { type: 'number', multipleOf: 3 } } },\n    { $id: '#schema3', type: 'object', properties: { mul2: { type: 'number', multipleOf: 2 } } }\n  ]\n}\n\n// Define your constraint logic\nconst constraint = {\n  body: {\n    constraint: function (request) {\n      switch(request.headers.myHeader){\n        case 1: return '#schema1'\n        case 2: return '#schema3'\n        case 3: return '#schema2'\n        default: return null // it means \"don't apply any constraint\"\n      }\n    },\n    statusCode: 412, // Optionally define a custom status code in case of errors\n    errorMessage: 'This constraint return only #schema1' // Optionally define a custom error message\n  },\n  querystring: { ... },\n  params: { ... },\n  headers: { ... }\n}\n\nconst fastify = Fastify()\nfastify.register(require('fastify-schema-constraint'), constraint)\nfastify.route({\n  url: '/:mul5',\n  method: 'POST',\n  handler: (_, reply) =\u003e { reply.send('hi') },\n  schema: {\n    body: routeSchema,\n    querystring: routeSchema,\n    params: routeSchema,\n    headers: routeSchema\n  }\n})\n```\n\n### Options\n\nThe options accept a json in this format:\n\n```js\nconst constraint = {\n  body: { ... }, // constraint to apply on body\n  querystring: { ... }, // constraint to apply on query string\n  params: { ... }, // constraint to apply on path parameters\n  headers: { ... } // constraint to apply on headers\n}\n```\n\nAll the fields are optional, but you must provide **almost one** of these settings.\nIf you provide a constraint for `body`, but the route doesn't have any schema configured\nthe plugin will skip the constraint.\n\nEach constraint field accepts a json in this format:\n\n```js\n{\n  constraint: function (request) {\n    // This field is mandatory and must be set with a function.\n    // The input paramenter is the Fastify request.\n    // This function must be sync and must return a string with the JSON Schema $id to constraint\n    return '#idToApply'\n  },\n  statusCode: 412, // Optionally: set a custom status code in case of errors, default 400\n  errorMessage: 'This constraint return only #idToApply' // Optionally: set a custom error message\n}\n```\n\n**NB:**\n\n+ if the constraint function returns something different than a string, the validation will be skipped!\n+ if the constraint function throws an error, an error will be thrown\n+ if the returned `$id` isn't present in the `oneOf` array an error will be thrown\n+ if the payload verified doesn't match with the returned `$id`, an error will be thrown\n\n\n## License\n\nCopyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Ffastify-schema-constraint","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feomm%2Ffastify-schema-constraint","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Ffastify-schema-constraint/lists"}