Search in sources :

Example 1 with AttributeValidationReportImpl

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();
}
Also used : Serializable(java.io.Serializable) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) Date(java.util.Date)

Example 2 with AttributeValidationReportImpl

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();
}
Also used : Serializable(java.io.Serializable) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) BigDecimal(java.math.BigDecimal)

Example 3 with AttributeValidationReportImpl

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();
}
Also used : Serializable(java.io.Serializable) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) Collection(java.util.Collection) Map(java.util.Map)

Example 4 with AttributeValidationReportImpl

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();
}
Also used : Serializable(java.io.Serializable) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl)

Example 5 with AttributeValidationReportImpl

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;
}
Also used : Arrays(java.util.Arrays) Set(java.util.Set) HashCodeBuilder(org.apache.commons.lang.builder.HashCodeBuilder) Collectors(java.util.stream.Collectors) AttributeValidationReport(ddf.catalog.validation.report.AttributeValidationReport) ValidationViolation(ddf.catalog.validation.violation.ValidationViolation) AttributeValidator(ddf.catalog.validation.AttributeValidator) Attribute(ddf.catalog.data.Attribute) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl) Locale(java.util.Locale) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) AttributeValidationReportImpl(ddf.catalog.validation.impl.report.AttributeValidationReportImpl) ValidationViolationImpl(ddf.catalog.validation.impl.violation.ValidationViolationImpl)

Aggregations

AttributeValidationReportImpl (ddf.catalog.validation.impl.report.AttributeValidationReportImpl)6 ValidationViolationImpl (ddf.catalog.validation.impl.violation.ValidationViolationImpl)6 Serializable (java.io.Serializable)5 Preconditions (com.google.common.base.Preconditions)1 Attribute (ddf.catalog.data.Attribute)1 AttributeValidator (ddf.catalog.validation.AttributeValidator)1 AttributeValidationReport (ddf.catalog.validation.report.AttributeValidationReport)1 ValidationViolation (ddf.catalog.validation.violation.ValidationViolation)1 BigDecimal (java.math.BigDecimal)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 EqualsBuilder (org.apache.commons.lang.builder.EqualsBuilder)1 HashCodeBuilder (org.apache.commons.lang.builder.HashCodeBuilder)1