Ankcorn / middy-sparks-joi

✨Stylish validation middleware for the stylish node.js middleware engine 🛵

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

✨Middy Sparks Joi

Middy Sparks Joi is a middleware for @middy/core. It validates incoming requests, and if an item does not spark joi we thank it and throw an exception.

https://media.giphy.com/media/uWzDsAsRm2X9qULHLs/giphy.gif

Getting Started

First, run yarn add middy-sparks-joi @hapi/joi or npm i middy-sparks-joi @hapi/joi in your project directory. Then in your lambda handler:

const middy = require('@middy/core')

// These middlewares are recommended
const jsonBodyParser = require('@middy/http-json-body-parser')
const httpErrorHandler = require('@middy/http-error-handler')

const Joi = require('@hapi/joi');
const validator = require('middy-sparks-joi')

const schema = Joi.object({
    body: Joi.object({
      creditCardNumber: Joi.string().creditcard(),
      expiryMonth: Joi.date().required(),
      expiryYear: Joi.date().required(),
      cvc: Joi.number().integer().required(3),
      nameOnCard: Joi.alphanum().required(),
      amount: Joi.number().required()
    }).required()
});

const processPayment = (event, context, callback) => {
 // you don't need to validate the event, the schema you passed to middy-sparks-joi does that for you.
 const { creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount } = event.body

 // do stuff with this data
 // ...

 return callback(null, { result: 'success', message: 'payment processed correctly'})
}

module.exports = {
  handler: middy(processPayment)
    .use(httpErrorHandler())
    .use(jsonBodyParser())
    .use(validator({
      schema
    })
}

That's it! Middy Sparks Joi protects you from inputs that don't spark joi ✨

Building Joi Schemas

The docs for joi can be found here https://joi.dev/api/

About

✨Stylish validation middleware for the stylish node.js middleware engine 🛵

License:MIT License


Languages

Language:JavaScript 100.0%