ffffng/server/validation/validator.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-05-12 20:08:19 +02:00
'use strict';
angular.module('ffffng').factory('Validator', function (_) {
var isValid = function (constraint, acceptUndefined, value) {
if (value === undefined) {
return acceptUndefined || constraint.optional;
}
2016-06-07 00:21:26 +02:00
if (constraint.type === 'boolean') {
return _.isBoolean(value);
}
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);
};
var areValid = function (constraints, acceptUndefined, values) {
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;
};
return {
forConstraint: function (constraint, acceptUndefined) {
return _.partial(isValid, constraint, acceptUndefined);
},
forConstraints: function (constraints, acceptUndefined) {
return _.partial(areValid, constraints, acceptUndefined);
}
};
});