Extended node management in amdin panel.
This commit is contained in:
parent
d5c69fa78f
commit
0f1a21c905
10 changed files with 152 additions and 32 deletions
server/validation
|
@ -1,24 +1,64 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('ffffng').factory('Validator', function (_) {
|
||||
var isValid = function (constraint, acceptUndefined, value) {
|
||||
if (value === undefined) {
|
||||
return acceptUndefined || constraint.optional;
|
||||
angular.module('ffffng').factory('Validator', function (_, Strings, Logger) {
|
||||
function isValidBoolean(value) {
|
||||
return _.isBoolean(value);
|
||||
}
|
||||
|
||||
function isValidNumber(constraint, value) {
|
||||
if (_.isString(value)) {
|
||||
value = Strings.parseInt(value);
|
||||
}
|
||||
|
||||
if (constraint.type === 'boolean') {
|
||||
return _.isBoolean(value);
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function isValidString(constraint, value) {
|
||||
if (!_.isString(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var trimmed = value.trim();
|
||||
return (trimmed === '' && constraint.optional) || trimmed.match(constraint.regex);
|
||||
};
|
||||
}
|
||||
|
||||
var areValid = function (constraints, acceptUndefined, values) {
|
||||
function isValid(constraint, acceptUndefined, value) {
|
||||
if (value === undefined) {
|
||||
return acceptUndefined || constraint.optional;
|
||||
}
|
||||
|
||||
switch (constraint.type) {
|
||||
case 'boolean':
|
||||
return isValidBoolean(value);
|
||||
|
||||
case 'number':
|
||||
return isValidNumber(constraint, value);
|
||||
|
||||
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) {
|
||||
var fields = Object.keys(constraints);
|
||||
for (var i = 0; i < fields.length; i ++) {
|
||||
var field = fields[i];
|
||||
|
@ -27,7 +67,7 @@ angular.module('ffffng').factory('Validator', function (_) {
|
|||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
forConstraint: function (constraint, acceptUndefined) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue