use of ddf.catalog.validation.impl.report.AttributeValidationReportImpl in project ddf by codice.
the class AbstractDateValidator method validate.
/**
* Validates the values of {@code attribute} that are {@link Date}s.
* <p>
* If {@code validator} returns false for a value, this method will return an {@link Optional}
* containing an {@link AttributeValidationReport} with a message of
* {@code attribute.getName() + " " + message} and a severity of {@link Severity#ERROR}.
* Otherwise, an empty {@link Optional} is returned.
*
* @param attribute the {@link Attribute} to validate
* @param validator the test to apply to the values of {@code attribute}
* @param message the message to include in the report in the case of a validation violation
* @return an {@link Optional} containing an {@link AttributeValidationReport} if there are
* violations, or an empty {@link Optional} if there are no violations
*/
protected final Optional<AttributeValidationReport> validate(final Attribute attribute, final Function<Date, Boolean> validator, final String message) {
final String name = attribute.getName();
for (final Serializable value : attribute.getValues()) {
if (value instanceof Date && !validator.apply((Date) value)) {
final AttributeValidationReportImpl report = new AttributeValidationReportImpl();
report.addViolation(new ValidationViolationImpl(Collections.singleton(name), name + " " + message, Severity.ERROR));
return Optional.of(report);
}
}
return Optional.empty();
}
use of ddf.catalog.validation.impl.report.AttributeValidationReportImpl in project ddf by codice.
the class RangeValidator method validate.
@Override
public Optional<AttributeValidationReport> validate(final Attribute attribute) {
Preconditions.checkArgument(attribute != null, "The attribute cannot be null.");
final String name = attribute.getName();
for (final Serializable value : attribute.getValues()) {
final BigDecimal bdValue;
if (value instanceof Number) {
bdValue = new BigDecimal(value.toString());
} else {
continue;
}
if (!checkRange(bdValue)) {
final String violationMessage = String.format("%s must be between %s and %s", name, min.toPlainString(), max.toPlainString());
final AttributeValidationReportImpl report = new AttributeValidationReportImpl();
report.addViolation(new ValidationViolationImpl(Collections.singleton(name), violationMessage, Severity.ERROR));
return Optional.of(report);
}
}
return Optional.empty();
}
use of ddf.catalog.validation.impl.report.AttributeValidationReportImpl in project ddf by codice.
the class SizeValidator method validate.
/**
* {@inheritDoc}
* <p>
* Validates only the values of {@code attribute} that are {@link CharSequence}s,
* {@link Collection}s, {@link Map}s, or arrays.
*/
@Override
public Optional<AttributeValidationReport> validate(final Attribute attribute) {
Preconditions.checkArgument(attribute != null, "The attribute cannot be null.");
final String name = attribute.getName();
for (final Serializable value : attribute.getValues()) {
int size;
if (value instanceof CharSequence) {
size = ((CharSequence) value).length();
} else if (value instanceof Collection) {
size = ((Collection) value).size();
} else if (value instanceof Map) {
size = ((Map) value).size();
} else if (value != null && value.getClass().isArray()) {
size = Array.getLength(value);
} else {
continue;
}
if (!checkSize(size)) {
final String violationMessage = String.format("%s size must be between %d and %d", name, min, max);
final AttributeValidationReportImpl report = new AttributeValidationReportImpl();
report.addViolation(new ValidationViolationImpl(Collections.singleton(name), violationMessage, Severity.ERROR));
return Optional.of(report);
}
}
return Optional.empty();
}
use of ddf.catalog.validation.impl.report.AttributeValidationReportImpl in project ddf by codice.
the class EnumerationValidator method validate.
/**
* {@inheritDoc}
* <p>
* Validates each of {@code attribute}'s values against the set of acceptable values by calling
* {@link String#valueOf(Object)} on each value and checking whether that string is in the set.
* <p>
*/
@Override
public Optional<AttributeValidationReport> validate(final Attribute attribute) {
Preconditions.checkArgument(attribute != null, "The attribute cannot be null.");
String name = attribute.getName();
for (final Serializable rawValue : attribute.getValues()) {
String value = String.valueOf(rawValue);
if (!values.contains(value)) {
final AttributeValidationReportImpl report = new AttributeValidationReportImpl();
// TODO (jrnorth) - escape the value.
report.addViolation(new ValidationViolationImpl(Collections.singleton(name), name + " has an invalid value: [" + value + "]", Severity.ERROR));
values.forEach(report::addSuggestedValue);
return Optional.of(report);
}
}
return Optional.empty();
}
use of ddf.catalog.validation.impl.report.AttributeValidationReportImpl in project ddf by codice.
the class ISO3CountryCodeValidator method buildReport.
private AttributeValidationReport buildReport(Attribute attribute) {
AttributeValidationReportImpl report = new AttributeValidationReportImpl();
attribute.getValues().stream().filter(String.class::isInstance).map(String.class::cast).map(ignoreCase ? String::toUpperCase : String::toString).filter(s -> !COUNTRY_CODES.contains(s)).map(s -> new ValidationViolationImpl(Collections.singleton(attribute.getName()), s + " is not a valid ISO_3166-1 Alpha3 country code.", ValidationViolation.Severity.ERROR)).forEach(report::addViolation);
COUNTRY_CODES.forEach(report::addSuggestedValue);
return report;
}
Aggregations