Comparing version 1.0.2 to 2.0.0
@@ -0,8 +1,9 @@ | ||
"use strict"; | ||
/** | ||
* @author Adam Jaso <ajaso@pocketly.com> | ||
*/ | ||
*/ | ||
var _ = require('lodash'); | ||
var Sanitizer = require('./Sanitizer'); | ||
var Aliases = require('./Aliases'); | ||
@@ -14,24 +15,4 @@ module.exports = exports = function(CustomSanitizer) { | ||
var my = {}; | ||
for (var name in sanitizer) { | ||
if (!/^(?:get|construct)/.test(name) && _.isFunction(sanitizer[name])) { | ||
(function(name) { | ||
my[name] = function() { | ||
Array.prototype.push.call(arguments, name); | ||
return applySanitizerForType.apply(sanitizer, arguments); | ||
}; | ||
})(name); | ||
} | ||
} | ||
for (var alias in sanitizer.aliases) { | ||
if (_.isString(sanitizer.aliases[alias])) { | ||
my[alias] = my[sanitizer.aliases[alias]]; | ||
} | ||
} | ||
return { | ||
my: my, | ||
my: sanitizer, | ||
value: filterValue, | ||
@@ -77,3 +58,3 @@ primitives: function(obj) { | ||
exports.Sanitizer = Sanitizer; | ||
exports.Aliases = Aliases; | ||
//exports.Aliases = Aliases; | ||
exports.middleware = require('./middleware'); | ||
@@ -135,6 +116,4 @@ | ||
type = this.aliases.lookup(type); | ||
return this[type].apply(this, parts); | ||
} |
@@ -10,29 +10,4 @@ /** | ||
var Aliases = require('./Aliases'); | ||
class Sanitizer { | ||
constructor(CustomAliases) { | ||
this.isSanitizer = true; // duck typing | ||
var aliases; | ||
if (!_.isUndefined(CustomAliases)) { | ||
if (_.isFunction(CustomAliases) && CustomAliases.prototype.isAliases) { | ||
aliases = new CustomAliases(); | ||
} else if (_.isObject(CustomAliases) && CustomAliases.isAliases) { | ||
aliases = CustomAliases; | ||
} else { | ||
throw new Error('Invalid aliases: ' + CustomAliases); | ||
} | ||
} else { | ||
aliases = new Aliases(); | ||
} | ||
this.aliases = aliases; | ||
} | ||
bool(value) { | ||
@@ -61,3 +36,3 @@ return _.isBoolean(value) ? value : vtor.toBoolean(value); | ||
integer(value) { | ||
int(value) { | ||
try { | ||
@@ -78,3 +53,3 @@ return parseInt(value); | ||
object(obj) { | ||
obj(obj) { | ||
return _.isObject(obj) ? obj : null; | ||
@@ -118,4 +93,4 @@ } | ||
string(value) { | ||
return !_.isNull(value) ? value.toString() : null; | ||
str(value) { | ||
return !_.isNull(value) && !_.isUndefined(value) ? value.toString() : null; | ||
} | ||
@@ -122,0 +97,0 @@ |
{ | ||
"name": "sanitize", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Input sanitizing library for node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/sanitize.js", |
@@ -5,3 +5,3 @@ # node-sanitize | ||
# Summary | ||
This library is for the purpose of sanitizing user input. The examples below show some of the built in sanitizers. You can create your own custom sanitizers and aliases. Please refer to the tests for more examples of how to use this library. | ||
This library is for the purpose of sanitizing user input. The examples below show some of the built in sanitizers. You can create your own custom sanitizers. Please refer to the tests for more examples of how to use this library. | ||
@@ -8,0 +8,0 @@ # USAGE |
@@ -22,6 +22,2 @@ /** | ||
it('should have sanitize.Aliases', function() { | ||
sanitize.Aliases.should.be.a.function; | ||
}); | ||
describe('sanitize()', function() { | ||
@@ -34,3 +30,3 @@ | ||
{ | ||
type: 'b', | ||
type: 'bool', | ||
value: true, | ||
@@ -40,3 +36,3 @@ expected: true | ||
{ | ||
type: 'b', | ||
type: 'bool', | ||
value: false, | ||
@@ -53,3 +49,3 @@ expected: false | ||
{ | ||
type: 'i', | ||
type: 'int', | ||
value: 1, | ||
@@ -64,3 +60,3 @@ expected: 1 | ||
{ | ||
type: 'integer', | ||
type: 'int', | ||
value: undefined, | ||
@@ -70,3 +66,3 @@ expected: undefined | ||
{ | ||
type: 'integer', | ||
type: 'int', | ||
value: null, | ||
@@ -76,3 +72,3 @@ expected: NaN | ||
{ | ||
type: 'i', | ||
type: 'int', | ||
value: 'asdf', | ||
@@ -84,3 +80,3 @@ expected: NaN | ||
{ | ||
type: 'f', | ||
type: 'float', | ||
value: 0.0, | ||
@@ -90,3 +86,3 @@ expected: 0.0 | ||
{ | ||
type: 'flo', | ||
type: 'float', | ||
value: '1.1', | ||
@@ -96,3 +92,3 @@ expected: 1.1 | ||
{ | ||
type: 'flo', | ||
type: 'float', | ||
value: ['1.123456', 2], | ||
@@ -107,3 +103,3 @@ expected: 1.12 | ||
{ | ||
type: 'f', | ||
type: 'float', | ||
value: undefined, | ||
@@ -113,3 +109,3 @@ expected: undefined | ||
{ | ||
type: 'f', | ||
type: 'float', | ||
value: 'a1asdf', | ||
@@ -261,3 +257,3 @@ expected: NaN | ||
{ | ||
type: 'string', | ||
type: 'str', | ||
value: 'abcde', | ||
@@ -267,3 +263,3 @@ expected: 'abcde' | ||
{ | ||
type: 'string', | ||
type: 'str', | ||
value: 1, | ||
@@ -273,3 +269,3 @@ expected: '1' | ||
{ | ||
type: 'string', | ||
type: 'str', | ||
value: null, | ||
@@ -279,3 +275,3 @@ expected: null | ||
{ | ||
type: 'string', | ||
type: 'str', | ||
value: undefined, | ||
@@ -314,3 +310,3 @@ expected: undefined | ||
{ | ||
type: 'arr', | ||
type: 'array', | ||
value: [1,2,3], | ||
@@ -320,3 +316,3 @@ expected: [1,2,3] | ||
{ | ||
type: 'arr', | ||
type: 'array', | ||
value: '', | ||
@@ -377,3 +373,3 @@ expected: null | ||
types: { | ||
user_id: 'i', | ||
user_id: 'int', | ||
password: 'str', | ||
@@ -509,3 +505,3 @@ email: 'email' | ||
it('should have aliases attached to it', function() { | ||
it('should have sanitizing functions directly attached to it', function() { | ||
@@ -515,6 +511,6 @@ sanitizer.my.int('1').should.eql(1); | ||
(sanitizer.my.str(null) === null).should.be.ok; | ||
(sanitizer.my.str(undefined) === undefined).should.be.ok; | ||
(sanitizer.my.str(undefined) === undefined).should.not.be.ok; | ||
(sanitizer.my.email('asdf') === null).should.be.ok; | ||
sanitizer.my.regex('asdf', /asdf/i).should.eql('asdf'); | ||
sanitizer.my.flo(['1.2345', 2]).should.be.eql(1.23); | ||
sanitizer.my.float(['1.2345', 2]).should.be.eql(1.23); | ||
@@ -599,9 +595,9 @@ }); | ||
class MySanitizer extends sanitize.Sanitizer { | ||
integer(value) { | ||
int(value) { | ||
theValue = value; | ||
return super.integer(value); | ||
return super.int(value); | ||
} | ||
} | ||
sanitize(MySanitizer).value(5, 'i').should.be.eql(theValue); | ||
sanitize(MySanitizer).value(5, 'int').should.be.eql(theValue); | ||
@@ -612,27 +608,2 @@ }); | ||
describe('sanitize.Aliases', function() { | ||
it('should support custom aliases', function() { | ||
class CustomAliases extends sanitize.Aliases { | ||
constructor() { | ||
super(); | ||
this.inty = 'integer'; | ||
} | ||
} | ||
var customAliases = new CustomAliases(); | ||
var customSanitizer = new sanitize.Sanitizer(customAliases); | ||
var mySanitizer = sanitize(customSanitizer); | ||
mySanitizer.value('1', 'inty').should.be.eql(1); | ||
(function() { | ||
mySanitizer.value('1', 'intyy').should.be.eql(1); | ||
}).should.throw(); | ||
}); | ||
}); | ||
}); |
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
25413
9
797