From 3291161c0d90fc9e599e80ab436599714c83efc1 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Mon, 15 Jun 2015 18:33:07 +0530 Subject: [PATCH 1/8] sanitizing function added --- lib/plugins/basicTypes.js | 4 +++- lib/validator.js | 20 ++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/plugins/basicTypes.js b/lib/plugins/basicTypes.js index 85c56fe..c94a829 100644 --- a/lib/plugins/basicTypes.js +++ b/lib/plugins/basicTypes.js @@ -3,7 +3,9 @@ function addBasicTypes(validator) { validator.registerGlobalType('array', function(v) { return v instanceof Array; }); - validator.registerGlobalType('null', function(v) { return v === null;}); + validator.registerGlobalType('null', function(v) { + return v === null; + }); var to = ['object', 'undefined', 'number', 'string', 'boolean'] to.forEach(function(type) { validator.registerGlobalType(type, function(v) { diff --git a/lib/validator.js b/lib/validator.js index c8f512d..e7d8ace 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -8,7 +8,7 @@ function Validator(k) { } Validator.prototype.validate = function (obj, valid) { - + var invalid = []; var ret = new Result(false); if (typeof obj === 'object') { ret = new Result(true); @@ -28,12 +28,15 @@ Validator.prototype.validate = function (obj, valid) { } } if (!result.status) { - result.info += "Validation failed for property " + e + ":"; + result.info += "Validation failed for property " + e + + ":"; + invalid.push(e); } ret = ret.and(result); } } } + ret.invalid = invalid; return ret; }; @@ -55,5 +58,18 @@ Validator.registerGlobalType = function (key, fn) { keys[key] = fn; }; +Validator.prototype.sanitize = function(obj, valid, defaultValue) { + var ret = this.validate(obj, valid); + if (ret.status === false) { + var result = new Result(true); + ret.invalid.forEach(function(el) { + obj[el] = defaultValue[el]; + result.info += "Sanitization done for property "+ el + + ":"; + }); + result.sanitized = obj; + } + return result; +} module.exports = Validator; From 68538a4cec48893a56d4d9f99fe00390be9548ba Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Wed, 17 Jun 2015 18:14:51 +0530 Subject: [PATCH 2/8] added travis configuration --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..21bdd0d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +#services: redis-server +#env: DB=postgres +# +#addons: +# postgresql: "9.3" + +#notifications: +## email: +# "devs@scrollback.io" \ No newline at end of file From d5529c92fa61eb9ec974fea31fbe9c7e8c052940 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Wed, 17 Jun 2015 18:23:38 +0530 Subject: [PATCH 3/8] add test script for travis --- .travis.yml | 9 --------- package.json | 3 +++ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 21bdd0d..587bd3e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1 @@ language: node_js -#services: redis-server -#env: DB=postgres -# -#addons: -# postgresql: "9.3" - -#notifications: -## email: -# "devs@scrollback.io" \ No newline at end of file diff --git a/package.json b/package.json index 3687d49..e8c023d 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "private": true, "license": "AGPL", "main": "./index.js", + "scripts": { + "test": "node_modules/.bin/mocha test/test.js" + }, "dependencies": { }, From 94b42589335d388f5546ecbe961881325a6baa70 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Wed, 17 Jun 2015 18:28:14 +0530 Subject: [PATCH 4/8] dependency added --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e8c023d..43915ad 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,6 @@ }, "optionalDependencies": {}, "devDependencies": { - + "mocha": "latest" } } From a1bcaa06ea6b23b68904cca1215028136d366dbb Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Wed, 17 Jun 2015 20:22:06 +0530 Subject: [PATCH 5/8] added test for sanitization --- lib/validator.js | 28 ++++-- test/sanitize-test.js | 114 +++++++++++++++++++++ test/test.js | 228 +----------------------------------------- test/valid-test.js | 226 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 361 insertions(+), 235 deletions(-) create mode 100644 test/sanitize-test.js create mode 100644 test/valid-test.js diff --git a/lib/validator.js b/lib/validator.js index e7d8ace..d662d1e 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -7,7 +7,7 @@ function Validator(k) { this.ckeys = {}; } -Validator.prototype.validate = function (obj, valid) { +Validator.prototype.validate = function(obj, valid) { var invalid = []; var ret = new Result(false); if (typeof obj === 'object') { @@ -28,8 +28,7 @@ Validator.prototype.validate = function (obj, valid) { } } if (!result.status) { - result.info += "Validation failed for property " + e - + ":"; + result.info += "Validation failed for property " + e + ":"; invalid.push(e); } ret = ret.and(result); @@ -43,33 +42,44 @@ Validator.prototype.validate = function (obj, valid) { -Validator.prototype.getType = function (key) { +Validator.prototype.getType = function(key) { if (this.ckeys[key]) return this.ckeys[key]; else return keys[key]; }; -Validator.prototype.registerType = function (key, fn) { +Validator.prototype.registerType = function(key, fn) { if ((typeof key === 'string') && (typeof fn !== 'function')) throw new Error("INVALID_PARAMS"); this.ckeys[key] = fn; }; -Validator.registerGlobalType = function (key, fn) { +Validator.registerGlobalType = function(key, fn) { if ((typeof key === 'string') && (typeof fn !== 'function')) throw new Error("INVALID_PARAMS"); keys[key] = fn; }; Validator.prototype.sanitize = function(obj, valid, defaultValue) { + var ret = this.validate(obj, valid); + if (!defaultValue) return ret; + if (ret.status === false) { var result = new Result(true); ret.invalid.forEach(function(el) { + try { + if (!defaultValue[el]) + throw "NO_DEFAULT_VALUE"; + } catch (err) { + result.err = err; + return result; + } obj[el] = defaultValue[el]; - result.info += "Sanitization done for property "+ el + + result.info += "Sanitization done for property " + el + ":"; }); result.sanitized = obj; - } - return result; + result.invalid = ret.invalid; + return result; + } else return ret; } module.exports = Validator; diff --git a/test/sanitize-test.js b/test/sanitize-test.js new file mode 100644 index 0000000..21e2f61 --- /dev/null +++ b/test/sanitize-test.js @@ -0,0 +1,114 @@ +var validator = require("../index.js")(); +var assert = require('assert'); + +var message = + + it("should sanitize an invalid object", function() { + var sanitize = validator.sanitize({ + type: "text", + id: null, + text: "this is new text", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }, { + type: ['string'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }, { + id: "defaultid", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }); + console.log("Sanitize: ", sanitize); + assert.equal(sanitize.sanitized.id, "defaultid", "sanitization failed"); + }); + +it("should sanitize invalid object", function() { + var sanitize = validator.sanitize({ + type: "text", + id: "SAdasdasdd", + text: "this is new text", + room: { + id: 3223, + type: 4324 + } + }, { + type: ['string'], + id: ['number'], + text: ['number'], + room: [{ + id: ['string'], + type: ['string'] + }] + }, { + id: 21341, + text: 3424, + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }); + console.log("Sanitize: ", sanitize); + assert.deepEqual(sanitize.invalid, ['id', 'text', 'room']); + assert.deepEqual(sanitize.sanitized, { + type: 'text', + id: 21341, + text: 3424, + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }, "sanitization failed"); +}); + +it("should return status false if no defaultvalue", function() { + var sanitize = validator.sanitize({ + type: "text", + id: null, + text: "this is new text", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }, { + type: ['string'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }); + console.log("Sanitize: ", sanitize); + assert.equal(sanitize.status, false, "should return false"); +}); + +it("should throw error if no defaultvalue", function() { + var sanitize = validator.sanitize({ + type: "text", + id: null, + text: "this is new text", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }, { + type: ['string'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }, {}); + console.log(sanitize.err); + assert.equal(sanitize.err, "NO_DEFAULT_VALUE", "should return false"); +}); diff --git a/test/test.js b/test/test.js index 9e702d7..8f90e9c 100644 --- a/test/test.js +++ b/test/test.js @@ -1,226 +1,2 @@ -var validator = require("../index.js")(); -var assert = require('assert'); - -var message = { - type: "text", - id: "ksjalfkjklajsfl", - text: "this is new text", - room: { - id: 'jdakjf930784ufjhcu', - type: 'room' - } -}; - - -describe("Validator test", function() { - - it("should return status true", function() { - var isValid = validator.validate(message, { - type: ['string'], - id: ['string'], - text: ['string'], - room: [{ - id: ['string'], - type: ['string'] - }] - }); - console.log("Valid", isValid); - assert.equal(isValid.status, true, "Validation failed"); - }); - - it("should return status false", function() { - var isValid = validator.validate(message, { - type: ['string'], - id: ['string'], - text: ['number'], - room: [{ - id: ['string'], - type: ['number'] - }] - }); - console.log("Valid", isValid); - assert.equal(isValid.status, false, "should not return true"); - }); - - it("test with function: should return true", function() { - var isValid = validator.validate(message, { - type: ['string'], - id: [function(id) { - return id.length > 1; - }], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'] - }] - }); - console.log("Valid", isValid); - assert.equal(isValid.status, true, "should return status true"); - }); - - - it("test with function: should return false", function() { - var isValid = validator.validate(message, { - type: ['string'], - id: [function(id) { - return id.length < 1; - }], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'] - }] - }); - console.log("Valid", isValid); - assert.equal(isValid.status, false, "should return status false"); - }); - - it("Test for non empty string: false", function() { - var isValid = validator.validate(message, { - type: ['nonEmptyString'], - id: [function(id) { - return id.length > 1; - }], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'] - }] - }); - console.log("Valid", isValid); - assert.equal(isValid.status, true, "should return status true"); - }); - - it("Test for non empty string: false", function() { - - message.type = ""; - var isValid = validator.validate(message, { - type: ['nonEmptyString'], - id: [function(id) { - return id.length > 1; - }], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'] - }] - }); - console.log("Valid", isValid); - message.type = "text"; - assert.equal(isValid.status, false, "should return status false"); - }); - - it("Test for strict object: false", function() { - message.test = []; - var t = { - type: ['string'], - test: ['strictObject'], - id: ['string'], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'] - }] - }; - var isValid = validator.validate(message, t); - - console.log("Valid", isValid); - assert.equal(isValid.status, false, "should return status false"); - t.test = ['object']; - isValid = validator.validate(message, t) - assert.equal(isValid.status, true, "should return true"); - delete message.test; - }); - - it("Test for nested strict object: true", function() { - message.test = {}; - var t = { - type: ['string'], - test: ['strictObject'], - id: ['string'], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'], - temp: ['strictObject'] - }] - }; - //message.room.temp = {}; - var isValid = validator.validate(message, t); - - console.log("Valid", isValid); - assert.equal(isValid.status, false, "should return status false"); - delete message.test; - delete message.room.temp; - }); - - - it("Test for strict object: true", function() { - message.test = {}; - var t = { - type: ['string'], - test: ['strictObject'], - id: ['string'], - text: ['string'], - room: [{ - id: ['string'], - type: ['anything'], - temp: ['strictObject'] - }] - }; - message.room.temp = {}; - var isValid = validator.validate(message, t); - - console.log("Valid", isValid); - assert.equal(isValid.status, true, "should return status true"); - t.test = ['object']; - isValid = validator.validate(message, t) - assert.equal(isValid.status, true, "should return true"); - delete message.test; - delete message.room.temp; - }); - - - - - it("Test for adding function: return true", function() { - validator.registerType('zero', function(v) { - return v === 0; - }); - message.number = 0; - var t = { - "number": ['zero'] - }; - var isValid = validator.validate(message, t); - assert.equal(isValid.status, true, "Should return true"); - }); - - - it("Test for adding function: return false", function() { - message.number = 1; - var t = { - "number": ['zero'] - }; - var isValid = validator.validate(message, t); - assert.equal(isValid.status, false, "Should return false"); - }); - - it("Test for deep object", function() { - var room = { - params: { - threader: { enabled: true} - } - } - var t = { - params: [{ - threader: ['undefined', { - enabled: ['boolean'] - }] - }] - } - var isValid = validator.validate(room, t); - assert.equal(isValid.status, true, "Should return false"); - }); - - -}); +require("./valid-test.js"); +require("./sanitize-test.js") diff --git a/test/valid-test.js b/test/valid-test.js new file mode 100644 index 0000000..9e702d7 --- /dev/null +++ b/test/valid-test.js @@ -0,0 +1,226 @@ +var validator = require("../index.js")(); +var assert = require('assert'); + +var message = { + type: "text", + id: "ksjalfkjklajsfl", + text: "this is new text", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } +}; + + +describe("Validator test", function() { + + it("should return status true", function() { + var isValid = validator.validate(message, { + type: ['string'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }); + console.log("Valid", isValid); + assert.equal(isValid.status, true, "Validation failed"); + }); + + it("should return status false", function() { + var isValid = validator.validate(message, { + type: ['string'], + id: ['string'], + text: ['number'], + room: [{ + id: ['string'], + type: ['number'] + }] + }); + console.log("Valid", isValid); + assert.equal(isValid.status, false, "should not return true"); + }); + + it("test with function: should return true", function() { + var isValid = validator.validate(message, { + type: ['string'], + id: [function(id) { + return id.length > 1; + }], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'] + }] + }); + console.log("Valid", isValid); + assert.equal(isValid.status, true, "should return status true"); + }); + + + it("test with function: should return false", function() { + var isValid = validator.validate(message, { + type: ['string'], + id: [function(id) { + return id.length < 1; + }], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'] + }] + }); + console.log("Valid", isValid); + assert.equal(isValid.status, false, "should return status false"); + }); + + it("Test for non empty string: false", function() { + var isValid = validator.validate(message, { + type: ['nonEmptyString'], + id: [function(id) { + return id.length > 1; + }], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'] + }] + }); + console.log("Valid", isValid); + assert.equal(isValid.status, true, "should return status true"); + }); + + it("Test for non empty string: false", function() { + + message.type = ""; + var isValid = validator.validate(message, { + type: ['nonEmptyString'], + id: [function(id) { + return id.length > 1; + }], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'] + }] + }); + console.log("Valid", isValid); + message.type = "text"; + assert.equal(isValid.status, false, "should return status false"); + }); + + it("Test for strict object: false", function() { + message.test = []; + var t = { + type: ['string'], + test: ['strictObject'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'] + }] + }; + var isValid = validator.validate(message, t); + + console.log("Valid", isValid); + assert.equal(isValid.status, false, "should return status false"); + t.test = ['object']; + isValid = validator.validate(message, t) + assert.equal(isValid.status, true, "should return true"); + delete message.test; + }); + + it("Test for nested strict object: true", function() { + message.test = {}; + var t = { + type: ['string'], + test: ['strictObject'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'], + temp: ['strictObject'] + }] + }; + //message.room.temp = {}; + var isValid = validator.validate(message, t); + + console.log("Valid", isValid); + assert.equal(isValid.status, false, "should return status false"); + delete message.test; + delete message.room.temp; + }); + + + it("Test for strict object: true", function() { + message.test = {}; + var t = { + type: ['string'], + test: ['strictObject'], + id: ['string'], + text: ['string'], + room: [{ + id: ['string'], + type: ['anything'], + temp: ['strictObject'] + }] + }; + message.room.temp = {}; + var isValid = validator.validate(message, t); + + console.log("Valid", isValid); + assert.equal(isValid.status, true, "should return status true"); + t.test = ['object']; + isValid = validator.validate(message, t) + assert.equal(isValid.status, true, "should return true"); + delete message.test; + delete message.room.temp; + }); + + + + + it("Test for adding function: return true", function() { + validator.registerType('zero', function(v) { + return v === 0; + }); + message.number = 0; + var t = { + "number": ['zero'] + }; + var isValid = validator.validate(message, t); + assert.equal(isValid.status, true, "Should return true"); + }); + + + it("Test for adding function: return false", function() { + message.number = 1; + var t = { + "number": ['zero'] + }; + var isValid = validator.validate(message, t); + assert.equal(isValid.status, false, "Should return false"); + }); + + it("Test for deep object", function() { + var room = { + params: { + threader: { enabled: true} + } + } + var t = { + params: [{ + threader: ['undefined', { + enabled: ['boolean'] + }] + }] + } + var isValid = validator.validate(room, t); + assert.equal(isValid.status, true, "Should return false"); + }); + + +}); From ce8b04876ee5fc7b8102c66e24b381fdc506d767 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Thu, 18 Jun 2015 13:46:31 +0530 Subject: [PATCH 6/8] throw error if no default value --- lib/validator.js | 21 ++++++++------------- test/sanitize-test.js | 38 +++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index d662d1e..2a5b570 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -7,7 +7,7 @@ function Validator(k) { this.ckeys = {}; } -Validator.prototype.validate = function(obj, valid) { +Validator.prototype.validate = function (obj, valid) { var invalid = []; var ret = new Result(false); if (typeof obj === 'object') { @@ -42,40 +42,35 @@ Validator.prototype.validate = function(obj, valid) { -Validator.prototype.getType = function(key) { +Validator.prototype.getType = function (key) { if (this.ckeys[key]) return this.ckeys[key]; else return keys[key]; }; -Validator.prototype.registerType = function(key, fn) { +Validator.prototype.registerType = function (key, fn) { if ((typeof key === 'string') && (typeof fn !== 'function')) throw new Error("INVALID_PARAMS"); this.ckeys[key] = fn; }; -Validator.registerGlobalType = function(key, fn) { +Validator.registerGlobalType = function (key, fn) { if ((typeof key === 'string') && (typeof fn !== 'function')) throw new Error("INVALID_PARAMS"); keys[key] = fn; }; -Validator.prototype.sanitize = function(obj, valid, defaultValue) { +Validator.prototype.sanitize = function (obj, valid, defaultValue) { var ret = this.validate(obj, valid); if (!defaultValue) return ret; if (ret.status === false) { var result = new Result(true); - ret.invalid.forEach(function(el) { - try { - if (!defaultValue[el]) - throw "NO_DEFAULT_VALUE"; - } catch (err) { - result.err = err; - return result; - } + ret.invalid.forEach(function (el) { + if (!defaultValue[el]) throw new Error("NO_DEFAULT_VALUE"); obj[el] = defaultValue[el]; result.info += "Sanitization done for property " + el + ":"; }); + console.log("post forEach") result.sanitized = obj; result.invalid = ret.invalid; return result; diff --git a/test/sanitize-test.js b/test/sanitize-test.js index 21e2f61..7aceb5b 100644 --- a/test/sanitize-test.js +++ b/test/sanitize-test.js @@ -91,24 +91,24 @@ it("should return status false if no defaultvalue", function() { assert.equal(sanitize.status, false, "should return false"); }); -it("should throw error if no defaultvalue", function() { - var sanitize = validator.sanitize({ - type: "text", - id: null, - text: "this is new text", - room: { - id: 'jdakjf930784ufjhcu', - type: 'room' - } - }, { - type: ['string'], - id: ['string'], - text: ['string'], - room: [{ +it("should throw error if no default value", function() { + assert.throws(function(){ + validator.sanitize({ + type: "text", + id: null, + text: "this is new text", + room: { + id: 'jdakjf930784ufjhcu', + type: 'room' + } + }, { + type: ['string'], id: ['string'], - type: ['string'] - }] - }, {}); - console.log(sanitize.err); - assert.equal(sanitize.err, "NO_DEFAULT_VALUE", "should return false"); + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }, {}); + }, /^Error: NO_DEFAULT_VALUE$/, "should return false"); }); From 100e8c9b99042914dfbc276e0584f0ce6fed6ef1 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Thu, 18 Jun 2015 13:48:28 +0530 Subject: [PATCH 7/8] small fix --- lib/validator.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index 2a5b570..722f57c 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -67,10 +67,8 @@ Validator.prototype.sanitize = function (obj, valid, defaultValue) { ret.invalid.forEach(function (el) { if (!defaultValue[el]) throw new Error("NO_DEFAULT_VALUE"); obj[el] = defaultValue[el]; - result.info += "Sanitization done for property " + el + - ":"; + result.info += "Sanitization done for property " + el + ":"; }); - console.log("post forEach") result.sanitized = obj; result.invalid = ret.invalid; return result; From 553540c2d1acc3fd89f19e6b19b8fc47bced4228 Mon Sep 17 00:00:00 2001 From: Chandrakanta Shatapathy Date: Thu, 18 Jun 2015 13:52:14 +0530 Subject: [PATCH 8/8] smallfix --- lib/validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validator.js b/lib/validator.js index 722f57c..a90cb3d 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -73,6 +73,6 @@ Validator.prototype.sanitize = function (obj, valid, defaultValue) { result.invalid = ret.invalid; return result; } else return ret; -} +}; module.exports = Validator;