@springworks/input-validator
Advanced tools
Comparing version 2.0.3 to 3.0.0
{ | ||
"name": "@springworks/input-validator", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "Module to help validate and filter input parameters.", | ||
"main": "input-validator.js", | ||
"private": false, | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "NODE_ENV=test istanbul cover --report teamcity --report lcov _mocha -- --ui bdd --check-leaks --recursive --slow 200 --reporter spec test" | ||
"test": "NODE_ENV=test istanbul cover _mocha", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
}, | ||
@@ -29,16 +29,14 @@ "repository": { | ||
"devDependencies": { | ||
"buddy.js": "^0.7.1", | ||
"chai": "^1.9.2", | ||
"coveralls": "~2.10.0", | ||
"eslint": "^0.9.1", | ||
"istanbul": "~0.2.6", | ||
"jsinspect": "^0.2.0", | ||
"mocha": "~1.18.0", | ||
"mocha-lcov-reporter": "0.0.1" | ||
"@springworks/test-harness": "^1.0.3", | ||
"coveralls": "^2.11.4", | ||
"eslint": "^1.1.0", | ||
"eslint-config-springworks": "^2.4.0", | ||
"eslint-plugin-mocha": "^0.4.0", | ||
"istanbul": "^0.3.18", | ||
"mocha": "^2.2.5" | ||
}, | ||
"dependencies": { | ||
"@springworks/error-factory": "^1.2.3", | ||
"hoek": "^2.10.0", | ||
"joi": "^6.4.2" | ||
"joi": "^6.6.1" | ||
} | ||
} |
'use strict'; | ||
require('chai').should(); | ||
var validator = require('../input-validator.js'); | ||
var validator = require('..'); | ||
var joi = validator.joi; | ||
describe('input-validator-test.js', function() { | ||
describe(__filename, function() { | ||
@@ -33,4 +30,4 @@ describe('Filtering params', function() { | ||
it('should remove prototype properties (length in this case).', function() { | ||
var Ref = String, | ||
actual = new Ref(); | ||
var Ref = String; | ||
var actual = new Ref(); | ||
actual.foo = 'bar'; | ||
@@ -68,4 +65,4 @@ Object.keys(validator.filterParams(actual, ['foo', 'bar'])).should.have.length(1); | ||
it('should return a list of missing params', function() { | ||
var missing = validator.missingParams({ foo: 'f' }, ['foo', 'bar']), | ||
first = missing[0]; | ||
var missing = validator.missingParams({ foo: 'f' }, ['foo', 'bar']); | ||
var first = missing[0]; | ||
(Array.isArray(missing)).should.eql(true); | ||
@@ -91,4 +88,4 @@ missing.should.have.length(1); | ||
var validated = validator.validateSchema({ | ||
'string_val': 'foo', | ||
'number_val': 1 | ||
string_val: 'foo', | ||
number_val: 1 | ||
}, schema, 'resource'); | ||
@@ -99,27 +96,18 @@ validated.should.have.keys(['string_val', 'number_val']); | ||
it('should filter excessive parameters based on schema', function() { | ||
it('should cast values based on schema', function() { | ||
var d = new Date(); | ||
var validated = validator.validateSchema({ | ||
'string_val': 'foo', | ||
'number_val': 1, | ||
'extra_key': 'bar' | ||
string_val: 'foo', | ||
number_val: '1', | ||
date_val: d.toISOString() | ||
}, schema, 'resource'); | ||
validated.should.have.keys(['string_val', 'number_val']); | ||
validated.number_val.should.equal(1); | ||
validated.date_val.toISOString().should.equal(d.toISOString()); | ||
}); | ||
it('should cast values based on schema', function() { | ||
var d = new Date(); | ||
var validated = validator.validateSchema({ | ||
'string_val': 'foo', | ||
'number_val': '1', | ||
'date_val': d.toISOString() | ||
}, schema, 'resource'); | ||
validated.number_val.should.equal(1); | ||
validated.date_val.toISOString().should.equal(d.toISOString()); | ||
}); | ||
it('should handle object ids (mongoDB)', function() { | ||
(function() { | ||
var validated = validator.validateSchema({ | ||
'string_val': 'foo', | ||
'object_id': '53fbf4615c3b9f41c381b6a3' | ||
string_val: 'foo', | ||
object_id: '53fbf4615c3b9f41c381b6a3' | ||
}, schema, 'resource'); | ||
@@ -130,18 +118,45 @@ validated.should.have.keys(['string_val', 'object_id']); | ||
it('should throw error when invalid parameters are given', function(done) { | ||
it('should throw if passed unknown parameters', function() { | ||
try { | ||
validator.validateSchema({ | ||
'string_val': 'foo', | ||
'number_val': 'fubar', | ||
'date_val': 'fubar_date', | ||
'object_id': 'invalid_object_id' | ||
string_val: 'foo', | ||
number_val: 1, | ||
extra_key: 'bar' | ||
}, schema, 'resource'); | ||
done(new Error('Should have thrown')); | ||
} | ||
catch(err) { | ||
catch (err) { | ||
err.should.have.property('statusCode', 422); | ||
err.output.payload.validation.should.have.keys([ | ||
'.extra_key' | ||
]); | ||
err.toJSON().should.eql({ | ||
message: 'Validation Failed', | ||
errors: [ | ||
{ | ||
resource: 'resource', | ||
field: '.extra_key', | ||
code: 'invalid' | ||
} | ||
] | ||
}); | ||
return; | ||
} | ||
throw new Error('Should have thrown'); | ||
}); | ||
it('should throw error when invalid parameters are given', function() { | ||
try { | ||
validator.validateSchema({ | ||
string_val: 'foo', | ||
number_val: 'fubar', | ||
date_val: 'fubar_date', | ||
object_id: 'invalid_object_id' | ||
}, schema, 'resource'); | ||
} | ||
catch (err) { | ||
err.should.have.property('statusCode', 422); | ||
// Check that all the fields are present in the boom error. | ||
err.output.payload.validation.should.have.keys([ | ||
'number_val', 'date_val', 'object_id' | ||
'number_val' | ||
]); | ||
@@ -157,27 +172,17 @@ | ||
code: 'invalid' | ||
}, | ||
{ | ||
resource: 'resource', | ||
field: 'date_val', | ||
code: 'invalid' | ||
}, | ||
{ | ||
resource: 'resource', | ||
field: 'object_id', | ||
code: 'invalid' | ||
} | ||
] | ||
}); | ||
done(); | ||
return; | ||
} | ||
throw new Error('Should have thrown'); | ||
}); | ||
it('should throw error when required parameter is missing', function(done) { | ||
it('should throw error when required parameter is missing', function() { | ||
try { | ||
validator.validateSchema({ | ||
'number_val': 1 | ||
number_val: 1 | ||
}, schema, 'resource'); | ||
done(new Error('Should have thrown')); | ||
} | ||
catch(err) { | ||
catch (err) { | ||
err.should.have.property('statusCode', 422); | ||
@@ -198,33 +203,33 @@ | ||
// Old error format (For Restify) | ||
err.should.have.deep.property('statusCode', 422); | ||
err.should.have.deep.property('code', 422); | ||
err.should.have.deep.property('message', 'Validation Failed'); | ||
err.should.have.deep.property('json'); | ||
err.should.have.deep.property('body'); | ||
err.should.have.property('statusCode', 422); | ||
err.should.have.property('code', 422); | ||
err.should.have.property('message', 'Validation Failed'); | ||
err.should.have.property('json'); | ||
err.should.have.property('body'); | ||
// Added boom properties | ||
err.should.have.deep.property('data'); | ||
err.should.have.deep.property('isBoom', true); | ||
err.should.have.deep.property('reformat'); | ||
err.should.have.property('data'); | ||
err.should.have.property('isBoom', true); | ||
err.should.have.property('reformat'); | ||
// Boom output | ||
err.should.have.deep.property('output.statusCode', 422); | ||
err.should.have.deep.property('output.payload'); | ||
err.output.should.have.property('statusCode', 422); | ||
err.output.should.have.property('payload'); | ||
err.should.have.deep.property('output.payload.statusCode', 422); | ||
err.should.have.deep.property('output.payload.message', 'Validation Failed'); | ||
err.should.have.deep.property('output.payload.error', 'Unprocessable Entity'); | ||
err.should.have.deep.property('output.payload.resource', 'resource'); | ||
done(); | ||
err.output.payload.should.have.property('statusCode', 422); | ||
err.output.payload.should.have.property('message', 'Validation Failed'); | ||
err.output.payload.should.have.property('error', 'Unprocessable Entity'); | ||
err.output.payload.should.have.property('resource', 'resource'); | ||
return; | ||
} | ||
throw new Error('Should have thrown'); | ||
}); | ||
it('should default resource', function(done) { | ||
it('should default resource', function() { | ||
try { | ||
validator.validateSchema({ | ||
'number_val': 1 | ||
number_val: 1 | ||
}, schema); | ||
done(new Error('Should have thrown')); | ||
} | ||
catch(err) { | ||
catch (err) { | ||
err.toJSON().should.eql({ | ||
@@ -240,4 +245,5 @@ message: 'Validation Failed', | ||
}); | ||
done(); | ||
return; | ||
} | ||
throw new Error('Should have thrown'); | ||
}); | ||
@@ -244,0 +250,0 @@ |
'use strict'; | ||
require('chai').should(); | ||
var validator = require('..'); | ||
var joi = validator.joi; | ||
var validate = validator.validateSchema; | ||
var validator = require('../input-validator.js'); | ||
describe(__filename, function() { | ||
var joi = validator.joi, | ||
validate = validator.validateSchema; | ||
describe('Joi Validation', function() { | ||
var schema = { | ||
@@ -18,3 +16,3 @@ foo: joi.string().required(), | ||
it('should not throw an error if given valid params', function() { | ||
var params = {foo: 'bar', 'bar': 123}; | ||
var params = { foo: 'bar', bar: 123 }; | ||
@@ -27,3 +25,3 @@ (function() { | ||
it('should throw an error if given invalid params', function() { | ||
var params = {'bar': 'asd'}; | ||
var params = { bar: 'asd' }; | ||
@@ -35,12 +33,4 @@ (function() { | ||
it('should filter out excessive parameters', function() { | ||
var params = {foo: 'bar', bar: 123, 'qux': 'should_not_be_included'}, | ||
actual = validate(params, schema, 'test'); | ||
actual.should.include.keys(['foo', 'bar']); | ||
actual.should.not.include.keys('qux'); | ||
}); | ||
it('should not throw if given a valid objectid', function() { | ||
var params = {foo: 'bar', bar: 123, baz: '507f191e810c19729de860ea'}; | ||
var params = { foo: 'bar', bar: 123, baz: '507f191e810c19729de860ea' }; | ||
@@ -52,4 +42,11 @@ (function() { | ||
it('should throw if given excessive parameters', function() { | ||
var params = { foo: 'bar', bar: 123, qux: 'should_not_be_included' }; | ||
(function() { | ||
validate(params, schema, 'test'); | ||
}).should.throw(); | ||
}); | ||
it('should throw if not given a valid obectid', function() { | ||
var params = {foo: 'bar', bar: 123, baz: 'not_a_valid_object_id'}; | ||
var params = { foo: 'bar', bar: 123, baz: 'not_a_valid_object_id' }; | ||
@@ -56,0 +53,0 @@ (function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
7
18141
12
349
1
- Removedhoek@^2.10.0
Updatedjoi@^6.6.1