diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..587bd3e --- /dev/null +++ b/.travis.yml @@ -0,0 +1 @@ +language: node_js 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..a90cb3d 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); @@ -29,11 +29,13 @@ Validator.prototype.validate = function (obj, valid) { } if (!result.status) { result.info += "Validation failed for property " + e + ":"; + invalid.push(e); } ret = ret.and(result); } } } + ret.invalid = invalid; return ret; }; @@ -55,5 +57,22 @@ Validator.registerGlobalType = function (key, fn) { 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) { + if (!defaultValue[el]) throw new Error("NO_DEFAULT_VALUE"); + obj[el] = defaultValue[el]; + result.info += "Sanitization done for property " + el + ":"; + }); + result.sanitized = obj; + result.invalid = ret.invalid; + return result; + } else return ret; +}; module.exports = Validator; diff --git a/package.json b/package.json index 3687d49..43915ad 100644 --- a/package.json +++ b/package.json @@ -17,11 +17,14 @@ "private": true, "license": "AGPL", "main": "./index.js", + "scripts": { + "test": "node_modules/.bin/mocha test/test.js" + }, "dependencies": { }, "optionalDependencies": {}, "devDependencies": { - + "mocha": "latest" } } diff --git a/test/sanitize-test.js b/test/sanitize-test.js new file mode 100644 index 0000000..7aceb5b --- /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 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'], + text: ['string'], + room: [{ + id: ['string'], + type: ['string'] + }] + }, {}); + }, /^Error: 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"); + }); + + +});