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

express-json-schema-validation

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-json-schema-validation

Express JSON Schema validation middleware

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

express-json-schema-validation

Express JSON Schema validation middleware

How to use:

npm i express-json-schema-validation

Define your schema with JSON Schema http://json-schema.org

model.json :

{
  "title": "model",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    }
  }
  "required": ["id"],
  "additionalProperties": false
}

Use it in express router:

import model from './schemas/model.json'
import { ValidationMiddleware, Validator } from 'express-json-schema-validation'

const validate = new ValidationMiddleware({ model })
const router = new Router()
router.post('/models', validate('model'), ModelController.get)

Handle validation errors in express error handler:

function handleError (err, req, res, next) {
  switch(err.code) {
    'EVALIDATION':
      res.status(422).json(err)
  }
}

Validate errors:

const validator = new Validator({ model })
const error = validator.validate('model', object)
if (error) {
  // ValidationError
}

Error format:

ValidationError({
  code: 'EVALIDATION',
  errors: [{
    pointer: '/id' // invalid property pointer,
    message: 'validation error' // validation message
  }]
})

FAQs

Package last updated on 20 Jun 2017

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