Skip to content

sanitizing function added #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: node_js
4 changes: 3 additions & 1 deletion lib/plugins/basicTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 20 additions & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};

Expand All @@ -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;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
114 changes: 114 additions & 0 deletions test/sanitize-test.js
Original file line number Diff line number Diff line change
@@ -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");
});
Loading