Socket
Socket
Sign inDemoInstall

selective

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

selective - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

82

index.js

@@ -1,1 +0,81 @@

module.exports = require('./lib/selective');
module.exports.select = function(fields, options, cb) {
return new selector(fields, options, cb).run();
}
var selector = function(fields, options, cb) {
this.fields = fields;
this.options = options;
this.errors = [];
this.cb = cb;
}
selector.prototype.run = function(){
var self = this;
this.getGroups();
this.checkFields();
process.nextTick(function(){
if (self.errors.length > 0) {
return self.cb(self.errors);
}
self.cb(null, self.fields);
});
}
selector.prototype.getGroups = function() {
var group = [];
var nonGroup = [];
this.options.forEach(function(option){
if(option.indexOf('#') !== -1){
group.push(option.replace('#', ''));
}
else {
nonGroup.push(option);
}
});
var groupExists = false;
Object.keys(this.fields).forEach(function(key){
if(group.indexOf(key) !== -1){
groupExists = true;
}
});
this.options = groupExists ? group.concat(nonGroup) : nonGroup;
}
selector.prototype.checkFields = function() {
var self = this;
var nonValid = Object.keys(this.fields);
var errors = [];
this.options.forEach(function(option){
var escaped = option.replace('*', '');
var key = nonValid.indexOf(escaped);
if(key !== -1){
delete nonValid[key];
}
if(typeof self.fields[escaped] === 'undefined' && option.indexOf('*') === -1){
self.addError(escaped, 'Missing Field');
}
});
nonValid.forEach(function(field){
self.addError(field, 'Invalid Field');
});
}
selector.prototype.addError = function(field, type) {
this.errors.push({
field: field,
type: type
});
}

2

package.json
{
"name": "selective",
"description": "Select and validate dynamic parameters via predefined options",
"version": "0.1.1",
"version": "0.1.2",
"author": "Bradley Griffiths <bradley.griffiths@gmail.com>",

@@ -6,0 +6,0 @@ "repository": {

@@ -16,3 +16,3 @@ Select parameters from an object (such as express's ```req.query```) and return any errors based on a specification defined.

name: 'bradley',
email: 'bradley.griffiths@gmail.com',
email: 'bradley@example.com',
random: 'random param'

@@ -22,8 +22,4 @@ }

selective.select(params, ['#name', '#email', '#password', '*location', 'phone'], function(err, selected){
if(err) {
console.log(err)
}
else {
console.log(selected);
}
if(err) console.log(err)
else console.log(selected);
});

@@ -33,3 +29,3 @@ ```

```json
```
[ { field: 'password', type: 'Missing Field' }, // 'password' is part of a group with a param sent (name).

@@ -42,2 +38,4 @@ { field: 'phone', type: 'Missing Field' }, // 'phone' is a required field.

Run the tests ```make test```
Run the tests ```make test```
[![Build Status](https://secure.travis-ci.org/bradleyg/selective.png)](http://travis-ci.org/bradleyg/selective)
var should = require('should');
var selective = require('../lib/selective');
var selective = require('../index');
describe('selective', function(){
describe('select()', function(){
it('should return the params if all params are correct', function(done){
var params = { name: 'Bradley', email: 'bradley.griffiths@gmail.com', password: 'password', phone: '01234567891', location: 'london' };
var params = { name: 'Bradley', email: 'bradley@example.com', password: 'password', phone: '01234567891', location: 'london' };
selective.select(params, ['#name', '#email', '#password', 'phone', '*location'], function(err, selected){

@@ -15,3 +15,3 @@ should.not.exist(err);

selected.should.have.property('name', 'Bradley');
selected.should.have.property('email', 'bradley.griffiths@gmail.com');
selected.should.have.property('email', 'bradley@example.com');
selected.should.have.property('password', 'password');

@@ -23,6 +23,6 @@ selected.should.have.property('phone', '01234567891');

});
it('should return an error if a required param is not supplied', function(){
var params = { name: 'Bradley' };
selective.select(params, ['name', 'email'], function(err, selected){

@@ -35,6 +35,6 @@ should.not.exist(selected);

});
it('should not return an error if an optional param is not supplied', function(){
var params = { name: 'Bradley' };
selective.select(params, ['name', '*email'], function(err, selected){

@@ -46,6 +46,6 @@ should.not.exist(err);

});
it('should return an error if only one param of a group is supplied', function(){
var params = { name: 'Bradley' };
selective.select(params, ['#name', '#email'], function(err, selected){

@@ -58,6 +58,6 @@ should.not.exist(selected);

});
it('should return an error if an invalid param is supplied', function(){
var params = { name: 'Bradley', random: 'random param' };
selective.select(params, ['name'], function(err, selected){

@@ -70,5 +70,5 @@ should.not.exist(selected);

});
});
});
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