npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

restify-joi-middleware

v5.0.1

Published

Simple joi middleware for restify.

Downloads

744

Readme

restify-joi-middleware

Another joi validation middleware for restify. Inspired by restify-joi-validator

standard travis npm

Installation:

npm i restify-joi-middleware --save

Note:

Requires Node >8.0.0.

Example:

This example is available here as well.

const Joi = require('joi')
const restify = require('restify')
const {name, version} = require('./package.json')
const validator = require('restify-joi-middleware')

const server = restify.createServer({name, version})
server.use(restify.plugins.acceptParser(server.acceptable))
server.use(restify.plugins.queryParser())
server.use(restify.plugins.bodyParser({mapParams: false}))
server.use(restify.plugins.gzipResponse())

server.use(validator()) // see "Middleware Options" for all options

// additional middleware etc

server.get({
  path: '/:id',
  validation: {
    schema: {
      params: Joi.object().keys({
        id: Joi.number().min(0).required()
      }).required()
    }
  }
}, (req, res, next) => {
  res.send(200, {id: req.params.id})
  next()
})

server.post({
  path: '/',
  validation: {
    schema: {
      body: Joi.object().keys({
        name: Joi.string().required()
      }).required()
    },
    // overrides middleware settings for this route
    options: {
      joiOptions: {
        allowUnknown: false
      }
    }
  }
}, (req, res, next) => {
  res.send(201, {id: 1, name: req.body.name})
  next()
})

server.put({
  path: '/:id',
  // Joi.object().keys({}) schemas work too
  validation: { 
    schema: Joi.object().keys({
      params: Joi.object().keys({
        id: Joi.number().min(0).required()
      }).required(),
      body: Joi.object().keys({
        id: Joi.number().min(0).required(),
        name: Joi.string().required()
      }).required()
    }).assert('params.id', Joi.ref('body.id'))
  }
}, (req, res, next) => {
  res.send(200, {id: 1, name: req.body.name})
  next()
})

server.listen(8080, () => console.log(`${server.name} listening on: ${server.url}`))

Given the server above:

curl 'http://localhost:8080/'
# result
# {
#   "code": "BadRequest",
#   "message": "child \"params\" fails because [child \"id\" fails because [\"id\" must be a number]]"
# }

curl -X POST -H "Content-Type: application/json" -d '{"color":"Blue"}' http://127.00.1:8080/
# result
# {
#   "code":"BadRequest",
#   "message":"child \"body\" fails because [child \"name\" fails because [\"name\" is required]]"
# }

curl -X PUT -H "Content-Type: application/json" -d '{"id": 1, "name":"Max"}' http://127.00.1:8080/2
# result
# {
#   "code":"BadRequest",
#   "message":"\"params.id\" validation failed because \"params.id\" failed to pass the assertion test"
# }

Middleware Options:

If you don't like how errors are returned or transformed from Joi errors to restify errors, you can change that for the entire plug-in. For example:

server.use(validator({
  joiOptions: {
    convert: true,
    allowUnknown: true,
    abortEarly: false
    // .. all additional joi options
  },
  // changes the request keys validated
  keysToValidate: ['params', 'body', 'query', 'user', 'headers', 'trailers', 'files'],
  
  // changes how joi errors are transformed to be returned - no error details are returned 
  // in this case
  errorTransformer: (validationInput, joiError) => new restifyErrors.BadRequestError(),
  
  // changes how errors are returned
  errorResponder: (transformedErr, req, res, next) => {
    res.send(400, transformedErr)
    return next()
  }
}))

Per-route Options

You can also override any middleware setting above per route, e.g.:

server.get({
  path: '/:id',
  validation: {
    schema: {
      params: Joi.object().keys({
        id: Joi.number().min(0).required()
      }).required()
    },
    options: {
      joiOptions: {
        allowUnknown: false
        // .. all additional joi options
      },
      
      // changes how errors are returned
      errorResponder: (transformedErr, req, res, next) => {
        res.send(400, transformedErr)
        return next()
      },
      
      // changes how joi errors are transformed to be returned - no error details are returned 
      // in this case
      errorTransformer: (validationInput, joiError) => new restifyErrors.BadRequestError()
      
      // keysToValidate can also be overridden here 
    }
  }
}, (req, res, next) => {
  res.send(200, {id: req.params.id})
  next()
})

Tests

npm test