Skip to content

Commit

Permalink
Validator: Revert middleware to returning a validator instance
Browse files Browse the repository at this point in the history
Signed-off-by: Fadion Dashi <jonidashi@gmail.com>
  • Loading branch information
fadion committed May 28, 2018
1 parent b2d08bd commit 07ed1f1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
4 changes: 1 addition & 3 deletions packages/validator/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const Validator = require('./lib/validator')
const Middleware = require('./lib/middleware')
const ValidationError = require('./lib/errors/validation-error')
const Joi = require('joi')

module.exports = {
rule: Joi,
Validator,
ValidatorMiddleware: Middleware,
ValidationError
ValidatorMiddleware: Middleware
}
3 changes: 0 additions & 3 deletions packages/validator/lib/errors/validation-error.js

This file was deleted.

16 changes: 5 additions & 11 deletions packages/validator/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Validator = require('./validator')
const ValidationError = require('./errors/validation-error')

/**
* Middleware class.
Expand All @@ -16,22 +15,17 @@ class Middleware {
}

/**
* Express middleware that runs the validator throws if
* it fails.
* Express middleware that runs the validator and
* injects it as an instance.
*
* @public
* @param {Object} req
* @param {Object} res
* @param {Function} next
*/
middleware(req, res, next) {
const validator = new Validator(this.fields(req), this.rules())

if (validator.fails) {
throw new ValidationError(validator.errors)
} else {
next()
}
req.validator = new Validator(this.fields(req), this.rules())
next()
}

/**
Expand All @@ -42,7 +36,7 @@ class Middleware {
* @returns {Object}
*/
fields(req) {
return {...req.body, ...req.query}
return { ...req.body, ...req.query }
}
}

Expand Down

0 comments on commit 07ed1f1

Please sign in to comment.