Git Product home page Git Product logo

middy-ajv's Introduction

Middy AJV

Middy logo

AJV middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

This middleware automatically validates incoming events and outgoing responses against custom schemas defined with the JSON schema syntax.

If an incoming event fails validation a BadRequest error is raised. If an outgoing response fails validation a InternalServerError error is raised.

This middleware can be used in combination with httpErrorHandler to automatically return the right response to the user.

It can also be used in combination with httpcontentnegotiation to load localised translations for the error messages (based on the currently requested language). This feature uses internally ajv-i18n module, so reference to this module for options and more advanced use cases. By default the language used will be English (en), but you can redefine the default language by passing it in the ajvOptions options with the key defaultLanguage and specifying as value one of the supported locales.

Also, this middleware accepts an object with plugins to be applied to customize the internal ajv instance.

Install

To install this middleware you can use NPM:

npm install --save middy-ajv

Requires: @middy/core:>=2.0.0

Options

  • eventSchema (function) (default undefined): The JSON schema compiled ajv validator that will be used to validate the input (request.event) of the Lambda handler.
  • contextSchema (function) (default undefined): The JSON schema object or compiled ajv validator that will be used to validate the input (request.context) of the Lambda handler. Has additional support for typeof keyword to allow validation of "typeof":"function".
  • responseSchema (function) (default undefined): The JSON schema compiled ajv validator that will be used to validate the output (request.response) of the Lambda handler.
  • availableLanguages (object) (optional): Error messages can be returned in multiple languages using ajv-i18n. Language is selected based on event.preferredLanguage set by @middy/http-content-negotiation. Should be in the format: { 'en': require('ajv-i18n/localize/en') }.
  • defaultLanguage (string) (default: en): The default language to use when availableLanguages is provided and event.preferredLanguage is not supported.

NOTES:

  • At least one of inputSchema or outputSchema is required.

Sample usage

Example for validation using precompiled schema:

import middy from '@middy/core'
import validator from 'middy-ajv'

const handler = middy((event, context) => {
  return {}
})

const eventSchema = require('schema.js')

handler.use(validator({ eventSchema }))

// invokes the handler, note that property foo is missing
const event = {
  body: JSON.stringify({something: 'somethingelse'})
}
handler(event, {}, (err, res) => {
  t.is(err.message,'Event object failed validation')
})

Build step

Folder Structure

{project}
|-- handlers
| |-- {endpoint}
| | |-- index.js
| | |-- schema.event.json
| | |-- schema.context.json
| | |-- schema.response.json

Install

$ npm install -D ajv-cli

Run

# or write your own
npx middy-ajv

After the compile script has been run on the endpoint folder, it will contain schema.event.js, schema.context.js, schema.response.js and index.js.

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

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) 2017-2021 will Farrell and the Middy team.

FOSSA Status

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.