restify-joi-middleware
Another joi validation middleware for restify. Inspired by restify-joi-validator
Installation:
npm i restify-joi-middleware --save
Note:
Requires Node >8.0.0
.
Example:
This example is available here as well.
const Joi = const restify = const name version = const validator = const server = restifyserverserverserverserver server // see "Middleware Options" for all options // additional middleware etc server server server server
Given the server above:
curl 'http://localhost:8080/'
# result
# {
# "code": "BadRequest",
# "message": "child \"params\" fails because [child \"id\" fails because [\"id\" must be a number]]"
# }
curl -X POST -H "Content-Type: application/json" -d '{"color":"Blue"}' http://127.00.1:8080/
# result
# {
# "code":"BadRequest",
# "message":"child \"body\" fails because [child \"name\" fails because [\"name\" is required]]"
# }
curl -X PUT -H "Content-Type: application/json" -d '{"id": 1, "name":"Max"}' http://127.00.1:8080/2
# result
# {
# "code":"BadRequest",
# "message":"\"params.id\" validation failed because \"params.id\" failed to pass the assertion test"
# }
Middleware Options:
If you don't like how errors are returned or transformed from Joi errors to restify errors, you can change that for the entire plug-in. For example:
server
Per-route Options
You can also override any middleware setting above per route, e.g.:
server
Tests
npm test