Created
May 4, 2015 04:31
-
-
Save FelipeBarrosCruz/202b28e56d2f6d526fe4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @package Validator | |
* @author FelipeBarros<[email protected]> [2015-05-04] | |
* | |
**/ | |
'use strict'; | |
/* REFERENCE: http://stackoverflow.com/a/1187628 */ | |
var array_diff = function(a1, a2) { | |
var a = [], | |
diff = []; | |
for (var i = 0; i < a1.length; i++) | |
a[a1[i]] = true; | |
for (var i = 0; i < a2.length; i++) | |
if (a[a2[i]]) delete a[a2[i]]; | |
else a[a2[i]] = true; | |
for (var k in a) | |
diff.push(k); | |
return diff; | |
}; | |
function Assert() { | |
var NotBlank = function() { | |
return { | |
validate: function(_value) { | |
return (_value) ? true : false; | |
}, | |
getMessage: function(_field) { | |
return 'This field: [' + _field + '] cannot be blank!'; | |
} | |
} | |
}; | |
return { | |
NotBlank: NotBlank | |
}; | |
}; | |
function Validator() { | |
var errors = [], | |
validateValue, | |
self_reference = function() { | |
return { | |
validateValue: validateValue, | |
errors: errors | |
}; | |
}; | |
validateValue = function(request_data, constraint_data) { | |
var diff = array_diff(Object.keys(request_data), Object.keys(constraint_data)); | |
for (var c_field in constraint_data) { | |
if (!(c_field in request_data)) { | |
if (diff.indexOf(c_field) == -1) { | |
diff.push(c_field); | |
} | |
continue; | |
} | |
var rule = constraint_data[c_field]; | |
if (!rule.validate(request_data[c_field])) { | |
errors.push({ | |
field: c_field, | |
message: rule.getMessage(c_field) | |
}); | |
} | |
} | |
for (var i = 0; i < diff.length; i++) { | |
errors.push({ | |
field: diff[i], | |
message: 'This field [' + diff[i] + '] not be expected!' | |
}); | |
} | |
return self_reference(); | |
}; | |
return self_reference(); | |
}; | |
function Request() { | |
/* REFERENCE http://stackoverflow.com/a/2880929 */ | |
var all = function() { | |
var match, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function(s) { | |
return decodeURIComponent(s.replace(pl, " ")); | |
}, | |
query = window.location.search.substring(1); | |
var urlParams = {}; | |
while (match = search.exec(query)) { | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
return urlParams; | |
} | |
return {all: all}; | |
}; | |
var app = { | |
ENV: 'local', | |
validator: new Validator(), | |
assert: new Assert(), | |
request: new Request() | |
}; | |
(function(app) { | |
console.log(app); | |
var request; | |
(window.onpopstate = function() { | |
request = app.request.all(); | |
})(); | |
var constraint = { | |
username: app.assert.NotBlank(), | |
email: app.assert.NotBlank() | |
}; | |
var errors = app.validator.validateValue(request, constraint).errors; | |
console.log(errors); | |
})(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment