can-validate
Version:
Validation plugin for CanJS.
98 lines • 3.42 kB
JavaScript
/*can-validate@1.2.1#can-validate*/
define([
'require',
'exports',
'module',
'can-reflect'
], function (require, exports, module) {
'use strict';
var canReflect = require('can-reflect');
var validate = {};
var helpers = {
'object': function (normalizedErrors) {
var errors = normalizedErrors.length > 0 ? {} : undefined;
canReflect.eachIndex(normalizedErrors, function (error) {
canReflect.eachIndex(error.related, function (related) {
if (!errors[related]) {
errors[related] = [];
}
errors[related].push(error.message);
});
});
return errors;
},
'flat': function (normalizedErrors) {
var errors = normalizedErrors.length > 0 ? [] : undefined;
canReflect.eachIndex(normalizedErrors, function (error) {
errors.push(error.message);
});
return errors;
},
'errors': function (normalizedErrors) {
return normalizedErrors.length > 0 ? normalizedErrors : undefined;
},
'errors-object': function (normalizedErrors) {
var errors = normalizedErrors.length > 0 ? {} : undefined;
canReflect.eachIndex(normalizedErrors, function (error) {
canReflect.eachIndex(error.related, function (related) {
if (!errors[related]) {
errors[related] = [];
}
errors[related].push(error);
});
});
return errors;
}
};
var parseErrorItem = function (rawErrors) {
var errors = [];
if (typeof rawErrors === 'string') {
errors.push({
message: rawErrors,
related: ['*']
});
}
if (typeof rawErrors === 'object' && !Array.isArray(rawErrors)) {
if (rawErrors.related) {
if (!Array.isArray(rawErrors.related)) {
rawErrors.related = [rawErrors.related];
}
} else {
rawErrors.related = '*';
}
errors.push(rawErrors);
}
if (Array.isArray(rawErrors)) {
canReflect.eachIndex(rawErrors, function (error) {
[].push.apply(errors, parseErrorItem(error));
});
}
return errors;
};
var normalizeErrors = function (rawErrors) {
var normalizedErrors = [];
if (typeof rawErrors === 'string' || typeof rawErrors === 'object' && !Array.isArray(rawErrors)) {
rawErrors = [rawErrors];
}
if (rawErrors != null) {
canReflect.eachIndex(rawErrors, function (error) {
[].push.apply(normalizedErrors, parseErrorItem(error));
});
}
return normalizedErrors;
};
validate.formatErrors = function (errors, format) {
var normalized = normalizeErrors(errors);
if (format) {
if (helpers[format]) {
return helpers[format](normalized);
} else {
return normalized;
}
} else {
return normalized;
}
};
module.exports = validate;
});
//# sourceMappingURL=can-validate.js.map