Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@appjumpstart/mercury-schema

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@appjumpstart/mercury-schema

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

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 19 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc