Skip to content

a simple express middleware to validate the request objects and the response body againts an object schema

License

Notifications You must be signed in to change notification settings

jgalmeida/express-validate-schema

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-validate-schema

Build StatusCode Coverage 100%ISC LicenseNodeJS

JavaScript Style Guide

api

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

each middleware should be define before your route function (see examples)

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

validating query string

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

validating params

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

validating body

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

validating headers

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

validating response

validateSchema([joi 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 params, body and header
router.put(
  '/someresouce/:id',
  validateSchema().params(someSchema),
  validateSchema()
    .body(Joi.object().keys({name: Joi.string().required()})),
  validateSchema({allowUnknown: true})
    .headers(Joi.object().keys({hello: Joi.string().required()})),
  (req, res) => { res.send('yay!') }
)

ISC License (ISC)

About

a simple express middleware to validate the request objects and the response body againts an object schema

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%