body-checker
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -11,3 +11,3 @@ jest | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -17,3 +17,3 @@ rogue: 'DROP users' | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -35,2 +35,59 @@ type: 'string', | ||
describe('undefined params', function() { | ||
var check = require('body-checker'); | ||
it('Should not return undefined key values when leys are missing', function() { | ||
var reqBody = { | ||
name: "Randy" | ||
}; | ||
var strongParams = { name: { type: 'string', required: true}, | ||
address: { type: 'string', required: false}}; | ||
check(reqBody, strongParams, function(err, body) { | ||
expect(err).toBeNull(); | ||
expect(body).toBeDefined(); | ||
expect(Object.keys(body).length).toEqual(1); | ||
}); | ||
}); | ||
it('Should handle empty object when strong params without keys', function() { | ||
var reqBody = {}; | ||
var strongParams = {}; | ||
check(reqBody, strongParams, function(err, body) { | ||
expect(err).toBeNull(); | ||
expect(body).toBeDefined(); | ||
expect(Object.keys(body).length).toEqual(0); | ||
}); | ||
}); | ||
it('Should handle bad object as strong params without keys', function() { | ||
var reqBody = { test: 'should fail'}; | ||
var strongParams = {}; | ||
check(reqBody, strongParams, function(err, body) { | ||
expect(err).toBeDefined(); | ||
expect(err.message).toEqual('Illegal parameter "test" provided'); | ||
expect(body).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
describe('required parameters', function() { | ||
@@ -42,3 +99,3 @@ | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -48,3 +105,3 @@ email: '' | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -70,7 +127,7 @@ type: 'string', | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy' | ||
}; | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -101,3 +158,3 @@ type: 'string', | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -107,3 +164,3 @@ email: '' | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -136,3 +193,3 @@ type: 'string', | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -142,3 +199,3 @@ id: '1' | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -170,3 +227,3 @@ type: 'string', | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -177,3 +234,3 @@ email: '', | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -211,3 +268,3 @@ type: 'string', | ||
var body = { | ||
var reqBody = { | ||
name: 'Randy', | ||
@@ -220,3 +277,3 @@ id: 1, | ||
check(body, { | ||
check(reqBody, { | ||
name: { | ||
@@ -223,0 +280,0 @@ type: 'string', |
@@ -15,3 +15,2 @@ /** | ||
var valid_keys = Object.keys(options); | ||
@@ -34,3 +33,3 @@ | ||
// Optional default handler | ||
if(!options[key].required && !body[key]) { | ||
if(!options[key].required && !body[key] && options[key].default) { | ||
body[key] = options[key].default; | ||
@@ -37,0 +36,0 @@ } |
{ | ||
"name": "body-checker", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A simple tool to protect your API against bad request parameters", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
10936
261