use of cern.modesti.schema.category.Constraint in project modesti by jlsalmon.
the class CoreValidationService method validatePoints.
private boolean validatePoints(Request request, List<Category> categories) {
boolean valid = true;
for (Point point : request.getNonEmptyPoints()) {
for (Category category : categories) {
for (Field field : category.getFields()) {
Object value = point.getValueByPropertyName(field.getPropertyName());
// Check for invalid fields
if (!isValidValue(value, point, field)) {
point.setValid(false);
valid = false;
point.addErrorMessage(category.getId(), field.getId(), "Value '" + value + "' is not a legal option for field '" + field.getName() + "'. Please select a value from the list.");
}
// Validate unique fields
if (field.getUnique() != null) {
Constraint constraint = new Constraint("unique", Collections.singletonList(field.getId()), null);
if (!Constraints.validate(constraint, request, category)) {
valid = false;
}
}
// Required fields (can be simple boolean or condition list)
boolean required = false;
if (field.getRequired() instanceof Boolean && (Boolean) field.getRequired()) {
required = true;
} else if (field.getRequired() != null) {
required = Conditionals.evaluate(field.getRequired(), point, request);
}
if (required) {
if (value == null || value.equals("")) {
point.setValid(false);
valid = false;
point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' is mandatory");
}
}
// Min length
if (field.getMinLength() != null) {
if (value != null && value.toString().length() < field.getMinLength()) {
point.setValid(false);
valid = false;
point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' must be at least " + field.getMinLength() + " characters in length");
}
}
// Max length
if (field.getMaxLength() != null) {
if (value != null && value.toString().length() > field.getMaxLength()) {
point.setValid(false);
valid = false;
point.addErrorMessage(category.getId(), field.getId(), "'" + field.getName() + "' must not exceed " + field.getMaxLength() + " characters in length");
}
}
// Numeric fields
if (field.getType().equals("numeric")) {
if (value != null && !value.toString().isEmpty() && !NumberUtils.isNumber(value.toString())) {
point.setValid(false);
valid = false;
point.addErrorMessage(category.getId(), field.getId(), "Value for '" + field.getName() + "' must be numeric");
}
}
}
}
}
return valid;
}
Aggregations