Skip to content

Commit 6ef778d

Browse files
committed
fix(compat): Restify 7+ compatibility
Closes z0mt3c#79 Closes z0mt3c#85
1 parent 45ead06 commit 6ef778d

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

TODO.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
- Dependency updates (esp. vulns) -- see https://github.com/z0mt3c/node-restify-validation/pull/87
2+
- Ensure all Validator.js features are properly exposed and use proper messaging
3+
- Code cleanup within lib (ES2015+, strict mode)
4+
- Migrate from Mocha / should to Jest
5+
- Attempt to remove Lodash entirely (check used Lodash methods semantics)
6+
- Address issues: https://github.com/z0mt3c/node-restify-validation/issues
7+
- Setup Travis CI; check badge
8+
- Migrate from Coveralls to Codecov; check badge
9+
- Add Dependabot badges etc.
10+
- Restore all required parts of README (conditional checks, etc.)
11+
- Deprecate content/resources/queries in favor of body/params/query

lib/index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
var _ = require('lodash')
2-
var self = {
3-
validation: require('./validation'),
4-
error: require('./error'),
5-
model: require('./model'),
6-
when: require('./conditions'),
7-
}
1+
const _ = require('lodash')
2+
const validation = require('./validation')
3+
const error = require('./error')
4+
const model = require('./model')
5+
const when = require('./conditions')
86

9-
module.exports.validation = self.validation
10-
module.exports.error = self.error
11-
module.exports.when = self.when
12-
module.exports.validatorModels = self.model.validatorModels
7+
module.exports.validation = validation
8+
module.exports.error = error
9+
module.exports.when = when
10+
module.exports.validatorModels = model.validatorModels
11+
module.exports.validationPlugin = validationPlugin
1312

14-
var defaultOptions = {
13+
const DEFAULT_OPTIONS = {
1514
errorsAsArray: true,
1615
errorHandler: false,
1716
forbidUndefinedVariables: false,
1817
validatorModels: {},
1918
}
2019

21-
module.exports.validationPlugin = function (options) {
22-
options = _.extend({}, defaultOptions, options)
20+
function validationPlugin(options) {
21+
// Massage options
22+
options = { ...DEFAULT_OPTIONS, ...options }
2323
if (_.isArray(options.validatorModels)) {
24-
// Combine list of validatorModels
25-
var validatorModels = _.toArray(options.validatorModels)
26-
validatorModels.unshift({})
27-
options.validatorModels = _.extend.apply(null, validatorModels)
24+
options.validatorModels = options.validatorModels.reduce((acc, models) => ({
25+
...acc,
26+
...models,
27+
}))
2828
}
29-
return function (req, res, next) {
30-
var validationModel = req.route ? req.route.validation : undefined
29+
30+
// Produce middleware
31+
return function restifyFreshValidationMW(req, res, next) {
32+
const spec = (req.route && req.route.spec) || req.route
33+
const validationModel = spec && spec.validation
3134

3235
if (validationModel) {
33-
// validate
34-
var errors = self.validation.process(validationModel, req, options)
36+
const errors = validation.process(validationModel, req, options)
3537

3638
if (
3739
(errors && options.errorsAsArray && errors.length > 0) ||
38-
(!options.errorsAsArray && _.keys(errors).length > 0)
40+
(!options.errorsAsArray && Object.keys(errors).length > 0)
3941
) {
40-
return self.error.handle(errors, req, res, options, next)
42+
return error.handle(errors, req, res, options, next)
4143
}
4244
}
4345

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"singleQuote": true,
3535
"semi": false
3636
},
37+
"engines": {
38+
"node": ">= 8"
39+
},
3740
"dependencies": {
3841
"lodash": "3.10.x",
3942
"validator": "3.22.x"

test/integrations/restify_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('[INTEGRATION][RESTIFY]', function () {
1212
before(function (done) {
1313
server = restify.createServer()
1414
server.use(
15-
restify.bodyParser({
15+
restify.plugins.bodyParser({
1616
mapParams: false,
1717
})
1818
)
@@ -86,9 +86,9 @@ describe('[INTEGRATION][RESTIFY]', function () {
8686
var server
8787
before(function (done) {
8888
server = restify.createServer()
89-
server.use(restify.bodyParser({ mapParams: false }))
89+
server.use(restify.plugins.bodyParser({ mapParams: false }))
9090
server.use(validationParser({ mapParams: true }))
91-
server.use(restify.queryParser({ mapParams: false }))
91+
server.use(restify.plugins.queryParser({ mapParams: false }))
9292
server.listen(0, function () {
9393
server.get(
9494
{
@@ -148,7 +148,7 @@ describe('[INTEGRATION][RESTIFY]', function () {
148148

149149
before(function (done) {
150150
server = restify.createServer()
151-
server.use(restify.bodyParser())
151+
server.use(restify.plugins.bodyParser())
152152
server.use(
153153
validationParser({
154154
forbidUndefinedVariables: false,

0 commit comments

Comments
 (0)