An express.js middleware makes a powerful validation request with the Joi validation.
express-validator
and Joi validation
npm install modern-express-joi
import modernValidator from 'modern-express-joi'
orrequire('modern-express-joi').default
- make an
express
middleware from passing schemas tomodernValidator
- add middleware to express
- call
req.checkAny
for validating - call
req.sanitizeAny
for sanitizing
Passing first params as string to select schema template.
You can pass option
as string for deep checking field by key name.
Here are available commands.
req.checkBody
for validationreq.body
.req.checkQuery
for validationreq.query
.req.checkParams
for validationreq.params
.req.checkHeaderscoming soonreq.checkCookiescoming soon
This is similar to req.checkAny
about passing params, but it converts the target object such as req.body
, req.query
, req.params
etc to defining format in schemaTemplates
.
Passing schemaTemplates
as object that has key name of your schema templates to construct an express middleware.
The second optional parameter must be an object that contains functions, and key names are only errorFormatter
, customValidator
, or customSanitizer
.
modernValidator(schemaTemplates, {
errorFormatter: (errors) => {},
customValidator: (value) => {},
customSanitizer: (value) => {}
})
A function that receives errors array
or false
for formatting errors when using req.validationErrors()
.
A function that validates the received value then returns error array
or false
.
A function that recieves value and schema then returns formatting value follows by schema.
Getting your result of validation after you called req.checkAny
. It returns false
if it has no validation errors, but It returns error array
in otherwise.
// examples/simple-validation.js
import modernValidator from 'modern-express-joi'
import Joi from 'joi'
import express from 'express'
import bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.json())
const template = Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().positive().optional()
})
const schemaTemplates = {
template
}
app.use(modernValidator(schemaTemplates))
app.post('/users', (req, res) => {
req.checkBody('template')
req.sanitizeBody('template')
const errors = req.validationErrors()
if (errors) res.status(400).send(errors)
else res.status(200).send({ message: 'Success' })
})
app.listen(8080)
console.log('Running at port 8080')
// Request
{
"name": "Hello",
"age": 18
}
// Response 200
{ "message": "Success" }
// Request
{
"age": "not number"
}
// Response 400
[
{
"message": "\"name\" is required",
"path": "name",
"type": "any.required",
"context": {
"key": "name"
}
}
]
// examples/error-formatter.js
import modernValidator from 'modern-express-joi'
import Joi from 'joi'
import express from 'express'
import bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.json())
const template = Joi.object.keys({
name: Joi.string().required(),
age: Joi.number().positive().optional()
})
const schemaTemplates = {
template
}
const errorFormatter = (errors) => (
errors.map(error => error.message)
)
app.use(modernValidator(schemaTemplates, { errorFormatter }))
app.post('/users', (req, res) => {
req.checkBody('template')
req.sanitizeBody('template')
const errors = req.validationErrors()
if (errors) res.status(400).send(errors)
else res.status(200).send({ message: 'Success' })
})
app.listen(8080)
console.log('Running at port 8080')
// examples/deep-checking.js
import modernValidator from 'modern-express-joi'
import Joi from 'joi'
import express from 'express'
import bodyParser from 'body-parser'
const app = express()
app.use(bodyParser.json())
const schemaTemplates = {
templateName: Joi.string().required(),
templateAge: Joi.number().positive().optional()
}
app.use(modernValidator(schemaTemplates))
app.post('/users', (req, res) => {
req.checkBody('templateName', 'name')
req.checkBody('templateAge', 'age')
req.sanitizeBody('templateName', 'name')
req.sanitizeBody('templateAge', 'age')
const errors = req.validationErrors()
if (errors) res.status(400).send(errors)
else res.status(200).send({ message: 'Success' })
})
app.listen(8080)
console.log('Running at port 8080');