Search in sources :

Example 1 with ValidationViolationImpl

use of ddf.catalog.validation.impl.violation.ValidationViolationImpl 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 ValidationViolationImpl

use of ddf.catalog.validation.impl.violation.ValidationViolationImpl 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 ValidationViolationImpl

use of ddf.catalog.validation.impl.violation.ValidationViolationImpl in project ddf by codice.

the class PatternValidator method validate.

/**
 * {@inheritDoc}
 *
 * <p>Validates only the values of {@code attribute} that are {@link CharSequence}s.
 */
@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()) {
        if (value instanceof CharSequence && !(pattern.matcher((CharSequence) value)).matches()) {
            final AttributeValidationReportImpl report = new AttributeValidationReportImpl();
            report.addViolation(new ValidationViolationImpl(Collections.singleton(name), name + " does not follow the pattern " + pattern.pattern(), 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)

Example 4 with ValidationViolationImpl

use of ddf.catalog.validation.impl.violation.ValidationViolationImpl 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 5 with ValidationViolationImpl

use of ddf.catalog.validation.impl.violation.ValidationViolationImpl 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)

Aggregations

ValidationViolationImpl (ddf.catalog.validation.impl.violation.ValidationViolationImpl)8 AttributeValidationReportImpl (ddf.catalog.validation.impl.report.AttributeValidationReportImpl)6 Serializable (java.io.Serializable)6 Attribute (ddf.catalog.data.Attribute)2 MetacardValidationReportImpl (ddf.catalog.validation.impl.report.MetacardValidationReportImpl)2 ValidationViolation (ddf.catalog.validation.violation.ValidationViolation)2 Preconditions (com.google.common.base.Preconditions)1 AttributeValidator (ddf.catalog.validation.AttributeValidator)1 ValidationExceptionImpl (ddf.catalog.validation.impl.ValidationExceptionImpl)1 AttributeValidationReport (ddf.catalog.validation.report.AttributeValidationReport)1 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1