Another Express Validator Middleware
1. Another Joi validator middleware
1. Motivation
2. Getting started
1. Simple usecase
3. API
1. Schema
2. validate(validationSchema)
3. paramId(paramName: string)
4. queryId(paramName: string)
5. queryString(paramName: string)
6. getErrorMessage(req, schema)
7. validateWithResponse(schema)
8. defaultErrorHandler (DEPRECATED)
I tired of writing the same code over and over again with validation middlewares for each new project. So I wrote this little package to not repeat myself.
The idea is quite simple: it just takes you Joi schema and returns an express middleware that validates it against the data from request object
const Joi = require('joi')
const { validate, defaultErrorHandler } = require('aevm')
const app = require('express')
const router = app.Router()
const schema = {
body: Joi.object().keys({
email: Joi.string().email().required(),
password: Joi.string().min(12).max(80).required(),
country: Joi.string().default('Ukraine')
})
}
router.post('/login', validate(schema), (req, res, next) => { / You code here / })
app.use('/api', router)
app.use((err, req, res, next) => {
res.status(err.httpCode || 500).json({ message: err.message })
})
app.listen(3000)
Now if you provide bad data:
{
"email": "wrong_email",
"password": "tooshort"
}
It will response with the following error:
{ "message": "'email' must be a valid email" }
Default values specified in schema would be placed in the request object
console.log(req.body.country) // 'Ukraine'
Any validation schema in this package must have the following structure:
interface IUserSchema {
body?: Joi.ObjectSchema
query?: Joi.ObjectSchema
params?: Joi.ObjectSchema
headers?: Joi.ObjectSchema
}
So basically it's an object with possible keys body, query, params, headers with values of Joi.object().keys()
Obviously at least one key is required
Takes an object with keys body, query, params, headers and values Joi.object().keys() and returns an express middleware that will validate the data and pass an error to next() function if there is any
Error would be { httpCode: number, message: string }.
Takes a string and creates a schema with a single validation numeric field for the req.params object and returns an express validation middleware
Could be usefull for when you need to validate an id from url like GET /users/:userId
Takes a string and creates a schema with a single validation numeric field for the req.query object and returns an express validation middleware
Takes a string and creates a schema with a single validation string field for the req.query object and returns an express validation middleware
Takes a req object and schema and returns error message with default values for custom validation middlewareReturned values:
{
message: 'you error message or empty string',
values: { / Object with data from your schema populated with defualt values / }
}
Takes schema and returns an express middleware that would automatically response with error code 400 end message in JSON like { "message": "your error" }
The default error handler that could be used to respond with error from middleware that came from validate method