Socket
Socket
Sign inDemoInstall

express-validate-schema

Package Overview
Dependencies
6
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-validate-schema

a simple express middleware to validate the request/response objects (body, params, querystring and headers) againts an object schema with joi


Version published
Weekly downloads
51
decreased by-7.27%
Maintainers
1
Install size
283 kB
Created
Weekly downloads
 

Readme

Source

express-validate-schema

Build StatusCoverage StatusISC LicenseNodeJS

JavaScript Style Guide

api

const validateSchema = require('express-validate-schema')

validateSchema([options])

options plain js object

  • validationOptions an optional object, see options for joi.validate on joi repo
  • processHttpCallOnError false by default means you should use an error-handling middleware, in case you don't use it you should set this to true and when gets an error/exception on the validation express-validate-schema will process the http call immediate

each validation should be define before your route function (see examples) either the response validation

route.get('/', validateSchema().query(query_schema), your_route_function)

validating query string

validateSchema([options]).query(some joi schema)

validating params

validateSchema([options]).params(some joi schema)

validating body

validateSchema([options]).body(some joi schema)

validating headers

validateSchema([options]).headers(some joi schema)

validating a custom key in the req object

validateSchema([options]).custom('key', some joi schema)

validating response

validateSchema([options]).response(some joi schema)

example

const express = require('express')
const validateSchema = require('express-validate-schema')

const app = express()

const router = express.Router()

// validating the querystring
router.get(
  '/querystring',
  validateSchema().query(someSchema),
  (req, res) => { res.send(someBody) }
)

// validating the params
router.get(
  '/params/:id',
  validateSchema().params(someSchema),
  (req, res) => { res.send(someBody) }
)

// validating the body
router.post(
  '/body',
  validateSchema().body(someSchema),
  (req, res) => { res.send(someBody) }
)

// validating the headers
router.get(
  '/headers',
  validateSchema().headers(headersSchema),
  (req, res) => { res.send(someBody) }
)

// validating the foobar custom req key
router.get(
  '/custom/:foobar?',
  (req, res, next) => {
    req.foobar = req.params.foobar
    next()
  },
  validateSchema({ processHttpCallOnError: true }).custom('foobar', customSchema),
  (req, res) => { res.send('custom') }
)

// validating params, body and header
router.put(
  '/someresouce/:id',
  validateSchema().params(someSchema),
  validateSchema()
    .body(Joi.object().keys({name: Joi.string().required()})),
  validateSchema({ validationOptions: { allowUnknown: true } })
    .headers(Joi.object().keys({hello: Joi.string().required()})),
  (req, res) => { res.send('yay!') }
)

ISC License (ISC)

Keywords

FAQs

Last updated on 20 Aug 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc