{"id":15984495,"url":"https://github.com/metcoder95/fastify-split-validator","last_synced_at":"2025-03-16T07:32:00.682Z","repository":{"id":45568414,"uuid":"419110910","full_name":"metcoder95/fastify-split-validator","owner":"metcoder95","description":"fastify-split-validator is a plugin which allows you to setup, granularly, different validator per HTTP part of the request. This works at a route level, doing a fallback into a default validator using the default server config from the instance where the plugin is being installed.","archived":false,"fork":false,"pushed_at":"2025-01-09T19:19:45.000Z","size":88,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-27T05:49:29.416Z","etag":null,"topics":["ajv","fastify","fastify-plugin","fastify-split-validator","node","validator"],"latest_commit_sha":null,"homepage":"","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/metcoder95.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yaml","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},"funding":{"github":"metcoder95"}},"created_at":"2021-10-19T22:36:07.000Z","updated_at":"2025-01-09T19:18:40.000Z","dependencies_parsed_at":"2024-01-01T23:40:52.633Z","dependency_job_id":"31c9a6e2-23cf-4bdb-a1db-e35fab0eabb1","html_url":"https://github.com/metcoder95/fastify-split-validator","commit_stats":{"total_commits":47,"total_committers":5,"mean_commits":9.4,"dds":0.5106382978723405,"last_synced_commit":"1c1dafdca076db2c031673ab87faf41db3553303"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metcoder95%2Ffastify-split-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metcoder95%2Ffastify-split-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metcoder95%2Ffastify-split-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metcoder95%2Ffastify-split-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metcoder95","download_url":"https://codeload.github.com/metcoder95/fastify-split-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243806045,"owners_count":20350775,"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":["ajv","fastify","fastify-plugin","fastify-split-validator","node","validator"],"created_at":"2024-10-08T02:09:05.026Z","updated_at":"2025-03-16T07:32:00.257Z","avatar_url":"https://github.com/metcoder95.png","language":"JavaScript","funding_links":["https://github.com/sponsors/metcoder95"],"categories":["JavaScript"],"sub_categories":[],"readme":"# fastify-split-validator\n\n[![CI](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/ci.yml) [![CodeQL](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/codeql-analysis.yml/badge.svg?branch=main)](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/codeql-analysis.yml) ![npm](https://img.shields.io/npm/v/fastify-split-validator)\n\n---\n\n`fastify-split-validator` is a plugin which allows you to setup, granularly, different validates per HTTP part of the request. This works at a route level, doing a fallback into a default validator using the default server config from the instance where the plugin is being installed.\n\nYou can provide your own default validator to act as fallback in case this is not defined within the definition of the route (_by default uses Ajv@8 as default fallback_).\n\n## Setup\n\nInstall by running `npm install fastify-split-validator`.\n\n### Options\n\n**Instance**\n\n- `defaultValidator`: default validator to be used as fallback in case nothing is provided at a route level definition\n\nExample:\n\n```js\nconst fastify = require('fastify');\nconst splitValidator = require('fastify-split-validator');\nconst Ajv = require('ajv');\n\nconst app = fastify();\nconst validator = new Ajv({});\n\nawait app.register(splitValidator, { defaultValidator: validator });\n```\n\n\u003e**Note**:\n\u003e It is important to advice that with the new fastify@v4, all the route registration now happens asynchronously.\n\u003e This change translates in a way that if any plugin is meant to set logic into the `onRoute` hook for manipulating\n\u003e routes after registration, it is necessary to await until the plugin is fully loaded before proceeding with the next parts\n\u003e of your route definition. Otherwise, this can lead to non-deterministic behaviours when the plugin will not the expected\n\u003e effect on your fastify application.\n\n**On Route**\n\n- `schemaValidators`: an object with the HTTP parts as keys and the validators to be used for that part as values\n  - `schemaValidators.body`: validator to be used for the body of the request\n  - `schemaValidators.params`: validator to be used for the params of the request\n  - `schemaValidators.headers`: validator to be used for the headers of the request\n  - `schemaValidators.querystring`: validator to be used for the querystring of the request\n  - `schemaValidators.query`: alias for `schemaValidators.querystring`\n\n### TypeScript\n\n```ts\nimport fastify from 'fastify';\nimport splitValidator from 'fastify-split-validator';\nimport Ajv from 'ajv';\n\nconst app = fastify();\nconst validator = new Ajv({});\nconst bodyValidator = new Ajv({});\nconst headersValidator = new Ajv({});\n\nawait app.register(splitValidator, { defaultValidator: validator });\n\napp.post('/', {\n  config: {\n    schemaValidators: {\n      body: bodyValidator,\n      headers: headersValidator,\n    },\n  },\n}, (req, reply) =\u003e {\n  // ...\n});\n```\n\n### JavaScript\n```js\nconst fastify = require('fastify');\nconst splitValidator = require('fastify-split-validator');\nconst Ajv = require('ajv');\n\nconst app = fastify();\nconst validator = new Ajv({});\nconst bodyValidator = new Ajv({});\nconst headersValidator = new Ajv({});\n\nawait app.register(splitValidator, { defaultValidator: validator });\n\napp.post('/', {\n  config: {\n    schemaValidators: {\n      body: bodyValidator,\n      headers: headersValidator,\n    },\n  },\n}, (req, reply) =\u003e {\n  // ...\n});\n```\n\nSee [test](test/index.test.js) for more examples.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetcoder95%2Ffastify-split-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetcoder95%2Ffastify-split-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetcoder95%2Ffastify-split-validator/lists"}