Git Product home page Git Product logo

mercury-schema's Introduction

mercury-schema

An Express/Connect-compatible middleware for validation and serialization using ajv and fast-json-stringify

npm page Join the community on Spectrum

About

mercury-schema is middleware you stick in front of your route handler to provide ๐Ÿ”ฅ fast schema validation of the request body and/or automatic serialization of the handler's response data.

Installation

npm install @appjumpstart/mercury-schema --save

Usage

NOTE: The example below assumes you're also using the mercury-send middleware to stringify the response automatically when calling res.send.

Add mercury-schema as a route-level middleware before your route handler and pass it a schema:

const { mercurySchema } = require('@appjumpstart/mercury-schema')

// ...

app.post('/contact', [
  mercurySchema({
    request: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        email: { type: 'string', minLength: 5 },
        message: { type: 'string' }
      },
      required: ['email', 'message']
    },
    response: {
      type: 'object',
      properties: {
        message: { type: 'string' }
      }
    }
  }),
  function contactHandler (req, res, next) {
    try {
      if (req.valid) {
        sendContactEmail(req.body)
        res.send({ message: 'Your message has been sent' })
        // Or if, for example, using express without using mercury-send:
        // const body = res.stringify({ message: 'Your message has been sent' })
        // res.type('json').end(body)
      } else {
        res.status(400).send(req.validation)
      }
    } catch (err) {
      next(err)
    }
  }
])

Acknowledgement

mercury-schema is completely modeled around the excellent validation and serialization feature within the Fastify framework.

ย 

AppJumpstart

mercury-schema's People

Stargazers

Ian Walter avatar

Watchers

Ian Walter avatar James Cloos avatar

mercury-schema's Issues

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.