2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
angular.module('ffffng').factory('Validator', function (_, Strings, Logger) {
|
2016-06-07 11:58:29 +02:00
|
|
|
// TODO: sanitize input for further processing as specified by constraints (correct types, trimming, etc.)
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
function isValidBoolean(value) {
|
2016-06-07 11:58:29 +02:00
|
|
|
return _.isBoolean(value) || value === 'true' || value === 'false';
|
2016-06-07 10:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isValidNumber(constraint, value) {
|
|
|
|
if (_.isString(value)) {
|
|
|
|
value = Strings.parseInt(value);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
if (!_.isNumber(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isNaN(value) || !_.isFinite(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isNumber(constraint.min) && value < constraint.min) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isNumber(constraint.max) && value > constraint.max) {
|
|
|
|
return false;
|
2016-06-07 00:21:26 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-07 11:58:29 +02:00
|
|
|
function isValidEnum(constraint, value) {
|
|
|
|
if (!_.isString(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.indexOf(constraint.allowed, value) >= 0;
|
|
|
|
}
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
function isValidString(constraint, value) {
|
2016-05-24 14:42:25 +02:00
|
|
|
if (!_.isString(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var trimmed = value.trim();
|
|
|
|
return (trimmed === '' && constraint.optional) || trimmed.match(constraint.regex);
|
2016-06-07 10:50:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isValid(constraint, acceptUndefined, value) {
|
|
|
|
if (value === undefined) {
|
|
|
|
return acceptUndefined || constraint.optional;
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
switch (constraint.type) {
|
|
|
|
case 'boolean':
|
|
|
|
return isValidBoolean(value);
|
|
|
|
|
|
|
|
case 'number':
|
|
|
|
return isValidNumber(constraint, value);
|
|
|
|
|
2016-06-07 11:58:29 +02:00
|
|
|
case 'enum':
|
|
|
|
return isValidEnum(constraint, value);
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
case 'string':
|
|
|
|
return isValidString(constraint, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.tag('validation').error('No validation method for constraint type: {}', constraint.type);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function areValid(constraints, acceptUndefined, values) {
|
2014-05-12 20:08:19 +02:00
|
|
|
var fields = Object.keys(constraints);
|
|
|
|
for (var i = 0; i < fields.length; i ++) {
|
|
|
|
var field = fields[i];
|
|
|
|
if (!isValid(constraints[field], acceptUndefined, values[field])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-06-07 10:50:15 +02:00
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
forConstraint: function (constraint, acceptUndefined) {
|
|
|
|
return _.partial(isValid, constraint, acceptUndefined);
|
|
|
|
},
|
|
|
|
forConstraints: function (constraints, acceptUndefined) {
|
|
|
|
return _.partial(areValid, constraints, acceptUndefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|