Middleware to validate request body or parameters against a Joi schema.
const Joi = require('joi')
const express = require('express')
const bodyParser = require('body-parser')
const validate = require('@twostoryrobot/express-joi-middleware')
const app = express()
const querySchema = Joi.object().keys({
user: Joi.string().guid().required()
})
const bodySchema = Joi.object().keys({
username: Joi.string().alphanum().min(3).required(),
password: Joi.string().min(4).required()
})
app.use(bodyParser.json())
app.get('/user', validate.query(querySchema), getUser)
app.post('/user', validate.body(bodySchema), postUser)
Validates a request's query parameters against the supplied schema.
schema
- A Joi schema
Example
const querySchema = Joi.object().keys({
user: Joi.string().guid().required()
})
app.get('/user', validate.query(querySchema), getUser)
Validates a request's body against the supplied schema.
schema
- A Joi schema
Example
const bodySchema = Joi.object().keys({
username: Joi.string().alphanum().min(3).required(),
password: Joi.string().min(4).required()
})
app.post('/user', validate.body(bodySchema), postUser)
npm install @twostoryrobot/express-joi-middleware
MIT