modern-express-joi
v1.0.7
Published
An express.js middleware makes a powerful validation request with the Joi validation.
Downloads
2
Readme
modern-express-joi
An express.js middleware makes a powerful validation request with the Joi validation.
Inspiration
express-validator
and Joi validation
Usage
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
req.checkAny(String, [option])
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.checkHeaders~~ coming soon
- ~~req.checkCookies~~ coming soon
req.sanitizeAny(String)
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
.
modernValidator(Object, [option])
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
.
Example Passing Parameters
modernValidator(schemaTemplates, {
errorFormatter: (errors) => {},
customValidator: (value) => {},
customSanitizer: (value) => {}
})
errorFormatter(errors)
A function that receives errors array
or false
for formatting errors when using req.validationErrors()
.
customValidator(value)
A function that validates the received value then returns error array
or false
.
customSanitizer(value, schema)
A function that recieves value and schema then returns formatting value follows by schema.
req.validationErrors()
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.
Example Simple Validation
// 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')
Passed
// Request
{
"name": "Hello",
"age": 18
}
// Response 200
{ "message": "Success" }
Failed
// Request
{
"age": "not number"
}
// Response 400
[
{
"message": "\"name\" is required",
"path": "name",
"type": "any.required",
"context": {
"key": "name"
}
}
]
Example Error Formatter
// 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')
Example Deep Checking Field
// 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');