java-bean-validation.js
Version:
Java Bean Validation implementation for JavaScript
68 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("../core/core");
var visit_1 = require("../core/visit");
var lookup_1 = require("./lookup");
var MAX_INTEGER_VALUE = 2147483647;
function getHtmlValidationRules(propertyOrTypeOrTypeName, propertyName, groups, violations) {
if (groups === void 0) { groups = core_1.DEFAULT_GROUPS; }
if (violations === void 0) { violations = []; }
var property = lookup_1.lookupPropertyDescriptor(propertyOrTypeOrTypeName, propertyName, undefined, '', violations);
if (!property) {
return null;
}
var htmlValidationRules = property.htmlValidationRules;
if (htmlValidationRules) {
return htmlValidationRules;
}
var rules = {};
var visitor = {
startValidatorVisitor: function (validatorName, validator, _value, attributes) {
if (!validator) {
return true;
}
if (validator.isHtmlRequiredValidator) {
rules.required = true;
}
switch (validatorName) {
case 'Length':
case 'Size': {
var min = +attributes.min || 0;
var max = +attributes.max || MAX_INTEGER_VALUE;
var maxLength = min > max ? min : max;
if (rules.maxLength === null || rules.maxLength === undefined || rules.maxLength > maxLength) {
rules.maxLength = maxLength;
}
break;
}
case 'Digits': {
var maxLength = attributes.integer + 1;
if (attributes.fraction) {
maxLength = maxLength + 1 + attributes.fraction;
}
if (rules.maxLength === null || rules.maxLength === undefined || rules.maxLength > maxLength) {
rules.maxLength = maxLength;
}
break;
}
case 'Min':
case 'Max':
case 'DecimalMin':
case 'DecimalMax': {
var maxLength = ((+attributes.value || 0) + '').length;
if (rules.maxLength === null || rules.maxLength === undefined || rules.maxLength > maxLength) {
rules.maxLength = maxLength;
}
break;
}
}
return true;
},
ignoreValue: true
};
visit_1.visitPropertyDescriptor(property, propertyName, undefined, '', groups, violations, violations, visitor);
property.htmlValidationRules = rules;
return rules;
}
exports.getHtmlValidationRules = getHtmlValidationRules;
//# sourceMappingURL=getHtmlValidationRules.js.map