java-bean-validation.js
Version:
Java Bean Validation implementation for JavaScript
137 lines • 4.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TYPE_DESCRIPTORS = {};
exports.GROUPS_INHERITANCE = {};
exports.VALIDATORS = {};
exports.DEFAULT_GROUP = 'Default';
exports.DEFAULT_GROUPS = [exports.DEFAULT_GROUP];
var formatMessage = function (template, attributes, invalidValue) {
return JSON.stringify({ template: template, attributes: attributes, invalidValue: invalidValue });
};
function getMessageFormatter() {
return formatMessage;
}
exports.getMessageFormatter = getMessageFormatter;
function setMessageFormatter(formatter) {
formatMessage = formatter;
}
exports.setMessageFormatter = setMessageFormatter;
function getTypeDescriptor(typeName, value, path, globalViolations) {
var name = getSimpleNameWithoutGenericArguments(typeName);
var typeDescriptor = exports.TYPE_DESCRIPTORS[name];
if (typeof typeDescriptor === 'function') {
typeDescriptor = typeDescriptor(typeName);
}
if (!typeDescriptor) {
var violatedConstraint = { constraintName: 'TypeNotFound', attributes: { typeName: typeName } };
addViolation(violatedConstraint, value, path, globalViolations);
return null;
}
return typeDescriptor;
}
exports.getTypeDescriptor = getTypeDescriptor;
function getPropertyDescriptor(type, objectWithProperty, propertyName, path, globalViolations) {
if (type.isCollection) {
var valueDescriptor = type.valueDescriptor;
if (valueDescriptor) {
return valueDescriptor;
}
}
var properties = type.properties;
if (properties) {
var property = properties[propertyName];
if (property) {
return property;
}
}
var violatedConstraint = { constraintName: 'PropertyNotFound', attributes: { typeName: type.typeName, propertyName: propertyName } };
addViolation(violatedConstraint, objectWithProperty, path, globalViolations);
return null;
}
exports.getPropertyDescriptor = getPropertyDescriptor;
function getSimpleNameWithoutGenericArguments(typeName) {
if (typeof typeName === 'string') {
return typeName;
}
else {
return typeName[0];
}
}
function groupAllowed(candidateGroups, allowedGroups) {
if (allowedGroups === void 0) { allowedGroups = exports.DEFAULT_GROUPS; }
for (var i = 0, length_1 = candidateGroups.length; i < length_1; i++) {
var group = candidateGroups[i];
if (allowedGroups.indexOf(group) >= 0) {
return true;
}
}
for (var j = 0, l = candidateGroups.length; j < l; j++) {
var group = candidateGroups[j];
var inherited = exports.GROUPS_INHERITANCE[group];
if (inherited && groupAllowed(inherited, allowedGroups)) {
return true;
}
}
return false;
}
exports.groupAllowed = groupAllowed;
function convertGroups(currentGroups, groupConversions) {
if (!groupConversions) {
return currentGroups;
}
var cascadeGroups = [];
for (var i = 0, length_2 = currentGroups.length; i < length_2; i++) {
var group = currentGroups[i];
var mapped = groupConversions[group];
if (mapped) {
cascadeGroups.push(mapped);
}
else {
cascadeGroups.push(group);
}
}
return cascadeGroups;
}
exports.convertGroups = convertGroups;
function addViolation(constraint, invalidValue, path, violations) {
var messageTemplate = getMessageTemplate(constraint);
var violation = {
constraintDescriptor: constraint,
invalidValue: invalidValue,
messageTemplate: messageTemplate,
message: formatMessage(messageTemplate, constraint.attributes, invalidValue),
propertyPath: path
};
violations.push(violation);
}
exports.addViolation = addViolation;
function getMessageTemplate(constraint) {
var template = null;
if (constraint.attributes) {
template = constraint.attributes.message;
}
if (!template) {
template = '{' + constraint.constraintName + '}';
}
return template;
}
function loadDefaultConstraintValues(constraint, validator) {
var defaultValues = validator.defaultValues;
if (!defaultValues) {
constraint.defaultValuesLoaded = true;
return;
}
var attributes = constraint.attributes;
if (!attributes) {
attributes = {};
constraint.attributes = attributes;
}
for (var key in defaultValues) {
if (attributes[key] === undefined) {
attributes[key] = defaultValues[key];
}
}
constraint.defaultValuesLoaded = true;
}
exports.loadDefaultConstraintValues = loadDefaultConstraintValues;
//# sourceMappingURL=core.js.map