|
1 | 1 | # fastify-split-validator
|
| 2 | + |
| 3 | +[](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +[](https://github.com/MetCoder95/fastify-split-validator/actions/workflows/codeql-analysis.yml) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +`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. |
| 10 | + |
| 11 | +You 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_). |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +Install by running `npm install fastify-split-validator`. |
| 16 | + |
| 17 | +### Options |
| 18 | + |
| 19 | +**Instance** |
| 20 | + |
| 21 | +- `defaultValidator`: default validator to be used as fallback in case nothing is provided at a route level definition |
| 22 | + |
| 23 | +Example: |
| 24 | + |
| 25 | +```js |
| 26 | +const fastify = require('fastify'); |
| 27 | +const splitValidator = require('fastify-split-validator'); |
| 28 | +const Ajv = require('ajv'); |
| 29 | + |
| 30 | +const app = fastify(); |
| 31 | +const validator = new Ajv({}); |
| 32 | + |
| 33 | +app.register(splitValidator, { defaultValidator: validator }); |
| 34 | +``` |
| 35 | + |
| 36 | +**On Route** |
| 37 | + |
| 38 | +- `schemaValidators`: an object with the HTTP parts as keys and the validators to be used for that part as values |
| 39 | + - `schemaValidators.body`: validator to be used for the body of the request |
| 40 | + - `schemaValidators.params`: validator to be used for the params of the request |
| 41 | + - `schemaValidators.headers`: validator to be used for the headers of the request |
| 42 | + - `schemaValidators.querystring`: validator to be used for the querystring of the request |
| 43 | + - `schemaValidators.query`: alias for `schemaValidators.querystring` |
| 44 | + |
| 45 | +### TypeScript |
| 46 | + |
| 47 | +```ts |
| 48 | +import fastify from 'fastify'; |
| 49 | +import splitValidator from 'fastify-split-validator'; |
| 50 | +import Ajv from 'ajv'; |
| 51 | + |
| 52 | +const app = fastify(); |
| 53 | +const validator = new Ajv({}); |
| 54 | +const bodyValidator = new Ajv({}); |
| 55 | +const headersValidator = new Ajv({}); |
| 56 | + |
| 57 | +app.register(splitValidator, { defaultValidator: validator }); |
| 58 | + |
| 59 | +app.post('/', { |
| 60 | + config: { |
| 61 | + schemaValidators: { |
| 62 | + body: bodyValidator, |
| 63 | + headers: headersValidator, |
| 64 | + }, |
| 65 | + }, |
| 66 | +}, (req, reply) => { |
| 67 | + // ... |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +### JavaScript |
| 72 | +```js |
| 73 | +const fastify = require('fastify'); |
| 74 | +const splitValidator = require('fastify-split-validator'); |
| 75 | +const Ajv = require('ajv'); |
| 76 | + |
| 77 | +const app = fastify(); |
| 78 | +const validator = new Ajv({}); |
| 79 | +const bodyValidator = new Ajv({}); |
| 80 | +const headersValidator = new Ajv({}); |
| 81 | + |
| 82 | +app.register(splitValidator, { defaultValidator: validator }); |
| 83 | + |
| 84 | +app.post('/', { |
| 85 | + config: { |
| 86 | + schemaValidators: { |
| 87 | + body: bodyValidator, |
| 88 | + headers: headersValidator, |
| 89 | + }, |
| 90 | + }, |
| 91 | +}, (req, reply) => { |
| 92 | + // ... |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +See [test](test/index.test.js) for more examples. |
0 commit comments