New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

body-checker

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

body-checker - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

27

__tests__/test.js

@@ -24,3 +24,3 @@ jest

expect(err).toBeDefined();
expect(err.message).toEqual('2 parameters passed, but method requires 1');
expect(err.message).toEqual('Illegal parameter "rogue" provided');
expect(body).toBeUndefined();

@@ -64,2 +64,26 @@

it('Should NOT fail on an undefined required parameter', function() {
var body = {
name: 'Randy'
};
check(body, {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
required: false
}
}, function(err, body) {
expect(body).toBeDefined();
expect(err).toBeNull();
expect(body.name).toEqual('Randy');
expect(body.email).toBeUndefined();
});
});
});

@@ -121,2 +145,3 @@

}, function(err, body) {
expect(err).toBeDefined();

@@ -123,0 +148,0 @@ expect(err.message).toEqual('Expected "id" to be type number, instead found string');

38

index.js

@@ -15,13 +15,12 @@ /**

// Check for extra params
var req_count = Object.keys(body).length,
options_count = Object.keys(options).length;
if(req_count !== options_count) {
return cb(new Error(req_count + ' parameters passed, but method requires ' + options_count));
var valid_keys = Object.keys(options);
// Ensure all passed params are legal
for(var p in body) {
if(valid_keys.indexOf(p) === -1) {
return cb(new Error('Illegal parameter "' + p + '" provided'));
}
}
var valid_keys = [];
// Check types
for(var key in options) {

@@ -39,22 +38,15 @@

// Check types for all but "any"
if(options[key].type !== 'any') {
if (check[options[key].type](body[key]) === false) {
// Check types for all but "any" allow undefined properties when not required
if(check[options[key].type](body[key]) === false) {
return cb(new Error('Expected "' + key + '" to be type ' + options[key].type + ', instead found ' + typeof body[key]));
if (body[key] === undefined && !options[key].required) {
// skip when key is not present and is not required
} else {
if(options[key].type !== 'any') {
return cb(new Error('Expected "' + key + '" to be type ' + options[key].type + ', instead found ' + typeof body[key]));
}
}
}
valid_keys.push(key);
}
// Ensure all passed params are legal
for(var p in body) {
if(valid_keys.indexOf(p) === -1) {
return cb(new Error('Illegal parameter "' + p + '" provided'));
}
}
// All is good

@@ -61,0 +53,0 @@ return cb(null, body);

{
"name": "body-checker",
"version": "0.1.3",
"version": "0.1.4",
"description": "A simple tool to protect your API against bad request parameters",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc