@novice1/validator-joi

Joi validator to use with @novice1/routing.

Usage no npm install needed!

<script type="module">
  import novice1ValidatorJoi from 'https://cdn.skypack.dev/@novice1/validator-joi';
</script>

README

@novice1/validator-joi

joi validator to use with @novice1/routing.

It provides a middleware that can validate the properties params, body, query, headers, cookies and files from the request using joi validation.

Installation

$ npm install @novice1/validator-joi

Example:

const router = require('@novice1/routing')();
const joi = require('joi');
const validatorJoi = require('@novice1/validator-joi');


/**
 * It will validate the  properties "params", "body", "query", "headers", "cookies" and "files"
 * from the request with the route parameters.
 * 
 */
router.setValidators(
  validatorJoi(
    // joi options
    { stripUnknown: true },
    // middleware in case validation fails
    function onerror(err, req, res, next) {
      res.status(400).json(err);
    }
  )
)

router.get({
  name: 'Main app',
  path: '/app',
  parameters: {
    query: {
      version: joi.number()
    }
  }
}, function (req, res) {
  res.json(req.query.version)
})

References