Middy joi validator middleware
middy middleware validator using joi.dev
Install
To install middy-joi-validator:
Using NPM:
npm install --save joi @bits-cr/middy-joi-validator
Using yarn:
yarn add joi @bits-cr/middy-joi-validator
Documentation and examples
//# handler.js #
// import core
import middy from '@middy/core' // esm Node v14+
//const middy = require('@middy/core') // commonjs Node v12+
// import some middlewares
import jsonBodyParser from '@middy/http-json-body-parser'
import httpErrorHandler from '@middy/http-error-handler'
import validator from 'middy-joi-validator'
// import joi
import Joi from 'joi';
// This is your common handler, in no way different than what you are used to doing every day in AWS Lambda
const baseHandler = async (event, context) => {
// we don't need to deserialize the body ourself as a middleware will be used to do that
const { creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount } = event.body
// do stuff with this data
// ...
const response = { result: 'success', message: 'payment processed correctly'}
return {statusCode: 200, body: JSON.stringify(response)}
}
// Notice that in the handler you only added base business logic (no deserialization,
// validation or error handler), we will add the rest with middlewares
const inputSchema = Joi.object({
creditCardNumber: Joi.string().min(12).max(19).pattern(/4242424242424242/).required(),
expiryMonth: Joi.number().integer().min(1).max(12),
expiryYear: Joi.number().integer().min(2017).max(2027),
cvc: Joi.string().min(3).max(4),
nameOnCard: Joi.string(),
amount: Joi.number()
})
// Let's "middyfy" our handler, then we will be able to attach middlewares to it
const handler = middy(baseHandler)
.use(jsonBodyParser()) // parses the request body when it's a JSON and converts it to an object
.use(validator({inputSchema})) // validates the input
.use(httpErrorHandler()) // handles common http errors and returns proper responses
module.exports = { handler }
Contributing
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
License
Licensed under MIT License. Copyright (c) 2022 Diego Arce and the Contributors.