Skip to content

Commit

Permalink
fix(compat): Restify 7+ compatibility
Browse files Browse the repository at this point in the history
Closes z0mt3c#79
Closes z0mt3c#85
  • Loading branch information
tdd committed Apr 23, 2022
1 parent 45ead06 commit 6ef778d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
11 changes: 11 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- Dependency updates (esp. vulns) -- see https://github.com/z0mt3c/node-restify-validation/pull/87
- Ensure all Validator.js features are properly exposed and use proper messaging
- Code cleanup within lib (ES2015+, strict mode)
- Migrate from Mocha / should to Jest
- Attempt to remove Lodash entirely (check used Lodash methods semantics)
- Address issues: https://github.com/z0mt3c/node-restify-validation/issues
- Setup Travis CI; check badge
- Migrate from Coveralls to Codecov; check badge
- Add Dependabot badges etc.
- Restore all required parts of README (conditional checks, etc.)
- Deprecate content/resources/queries in favor of body/params/query
50 changes: 26 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
var _ = require('lodash')
var self = {
validation: require('./validation'),
error: require('./error'),
model: require('./model'),
when: require('./conditions'),
}
const _ = require('lodash')
const validation = require('./validation')
const error = require('./error')
const model = require('./model')
const when = require('./conditions')

module.exports.validation = self.validation
module.exports.error = self.error
module.exports.when = self.when
module.exports.validatorModels = self.model.validatorModels
module.exports.validation = validation
module.exports.error = error
module.exports.when = when
module.exports.validatorModels = model.validatorModels
module.exports.validationPlugin = validationPlugin

var defaultOptions = {
const DEFAULT_OPTIONS = {
errorsAsArray: true,
errorHandler: false,
forbidUndefinedVariables: false,
validatorModels: {},
}

module.exports.validationPlugin = function (options) {
options = _.extend({}, defaultOptions, options)
function validationPlugin(options) {
// Massage options
options = { ...DEFAULT_OPTIONS, ...options }
if (_.isArray(options.validatorModels)) {
// Combine list of validatorModels
var validatorModels = _.toArray(options.validatorModels)
validatorModels.unshift({})
options.validatorModels = _.extend.apply(null, validatorModels)
options.validatorModels = options.validatorModels.reduce((acc, models) => ({
...acc,
...models,
}))
}
return function (req, res, next) {
var validationModel = req.route ? req.route.validation : undefined

// Produce middleware
return function restifyFreshValidationMW(req, res, next) {
const spec = (req.route && req.route.spec) || req.route
const validationModel = spec && spec.validation

if (validationModel) {
// validate
var errors = self.validation.process(validationModel, req, options)
const errors = validation.process(validationModel, req, options)

if (
(errors && options.errorsAsArray && errors.length > 0) ||
(!options.errorsAsArray && _.keys(errors).length > 0)
(!options.errorsAsArray && Object.keys(errors).length > 0)
) {
return self.error.handle(errors, req, res, options, next)
return error.handle(errors, req, res, options, next)
}
}

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"singleQuote": true,
"semi": false
},
"engines": {
"node": ">= 8"
},
"dependencies": {
"lodash": "3.10.x",
"validator": "3.22.x"
Expand Down
8 changes: 4 additions & 4 deletions test/integrations/restify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('[INTEGRATION][RESTIFY]', function () {
before(function (done) {
server = restify.createServer()
server.use(
restify.bodyParser({
restify.plugins.bodyParser({
mapParams: false,
})
)
Expand Down Expand Up @@ -86,9 +86,9 @@ describe('[INTEGRATION][RESTIFY]', function () {
var server
before(function (done) {
server = restify.createServer()
server.use(restify.bodyParser({ mapParams: false }))
server.use(restify.plugins.bodyParser({ mapParams: false }))
server.use(validationParser({ mapParams: true }))
server.use(restify.queryParser({ mapParams: false }))
server.use(restify.plugins.queryParser({ mapParams: false }))
server.listen(0, function () {
server.get(
{
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('[INTEGRATION][RESTIFY]', function () {

before(function (done) {
server = restify.createServer()
server.use(restify.bodyParser())
server.use(restify.plugins.bodyParser())
server.use(
validationParser({
forbidUndefinedVariables: false,
Expand Down

0 comments on commit 6ef778d

Please sign in to comment.