Search in sources :

Example 1 with ValidatorMetadata

use of org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata in project sling by apache.

the class ValidationServiceImpl method validatePropertyValue.

private void validatePropertyValue(@Nonnull String property, ValueMap valueMap, Resource resource, @Nonnull String relativePath, @Nonnull ResourceProperty resourceProperty, @Nonnull CompositeValidationResult result, @Nonnull ResourceBundle defaultResourceBundle) {
    Object fieldValues = valueMap.get(property);
    if (fieldValues == null) {
        if (resourceProperty.isRequired()) {
            result.addFailure(relativePath, configuration.defaultSeverity(), defaultResourceBundle, I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, property);
        }
        return;
    }
    List<ValidatorInvocation> validatorInvocations = resourceProperty.getValidatorInvocations();
    if (resourceProperty.isMultiple()) {
        if (!fieldValues.getClass().isArray()) {
            result.addFailure(relativePath + property, configuration.defaultSeverity(), defaultResourceBundle, I18N_KEY_EXPECTED_MULTIVALUE_PROPERTY);
            return;
        }
    }
    for (ValidatorInvocation validatorInvocation : validatorInvocations) {
        // lookup validator by id
        ValidatorMetadata validatorMetadata = validatorMap.get(validatorInvocation.getValidatorId());
        if (validatorMetadata == null) {
            throw new IllegalStateException("Could not find validator with id '" + validatorInvocation.getValidatorId() + "'");
        }
        int severity = getSeverityForValidator(validatorInvocation.getSeverity(), validatorMetadata.getSeverity());
        // convert the type always to an array
        Class<?> type = validatorMetadata.getType();
        if (!type.isArray()) {
            try {
                // https://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getName%28%29 has some hints on class names
                type = Class.forName("[L" + type.getName() + ";", false, type.getClassLoader());
            } catch (ClassNotFoundException e) {
                throw new SlingValidationException("Could not generate array class for type " + type, e);
            }
        }
        // it is already validated here that the property exists in the value map
        Object[] typedValue = (Object[]) valueMap.get(property, type);
        // see https://issues.apache.org/jira/browse/SLING-4178 for why the second check is necessary
        if (typedValue == null || (typedValue.length > 0 && typedValue[0] == null)) {
            // here the missing required property case was already treated in validateValueMap
            result.addFailure(relativePath + property, severity, defaultResourceBundle, I18N_KEY_WRONG_PROPERTY_TYPE, validatorMetadata.getType());
            return;
        }
        // see https://issues.apache.org/jira/browse/SLING-662 for a description on how multivalue properties are treated with ValueMap
        if (validatorMetadata.getType().isArray()) {
            // ValueMap already returns an array in both cases (property is single value or multivalue)
            validateValue(result, typedValue, property, relativePath, valueMap, resource, validatorMetadata.getValidator(), validatorInvocation.getParameters(), defaultResourceBundle, severity);
        } else {
            // call validate for each entry in the array (supports both singlevalue and multivalue)
            @Nonnull Object[] array = (Object[]) typedValue;
            if (array.length == 1) {
                validateValue(result, array[0], property, relativePath, valueMap, resource, validatorMetadata.getValidator(), validatorInvocation.getParameters(), defaultResourceBundle, severity);
            } else {
                int n = 0;
                for (Object item : array) {
                    validateValue(result, item, property + "[" + n++ + "]", relativePath, valueMap, resource, validatorMetadata.getValidator(), validatorInvocation.getParameters(), defaultResourceBundle, severity);
                }
            }
        }
    }
}
Also used : ValidatorInvocation(org.apache.sling.validation.model.ValidatorInvocation) Nonnull(javax.annotation.Nonnull) ValidatorMetadata(org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata) SlingValidationException(org.apache.sling.validation.SlingValidationException)

Example 2 with ValidatorMetadata

use of org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata in project sling by apache.

the class ValidatorMapTest method testUpdateChangingValidatorId.

@Test
public void testUpdateChangingValidatorId() {
    Map<String, Object> validatorProperties = new HashMap<>();
    String newId = "newId";
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_ID, newId);
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_SEVERITY, 1);
    Mockito.doReturn(-1).when(newValidatorServiceReference).compareTo(Mockito.anyObject());
    validatorMap.update(validatorProperties, dateValidator, validatorServiceReference);
    Assert.assertEquals(new ValidatorMetadata(dateValidator, validatorServiceReference, 1), validatorMap.get(newId));
    // make sure that the old validator id is no longer in the list
    Assert.assertNull(validatorMap.get(DATE_VALIDATOR_ID));
}
Also used : HashMap(java.util.HashMap) ValidatorMetadata(org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata) Test(org.junit.Test)

Example 3 with ValidatorMetadata

use of org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata in project sling by apache.

the class ValidatorMapTest method testPutValidatorWithSameValidatorIdAndHigherRanking.

@Test
public void testPutValidatorWithSameValidatorIdAndHigherRanking() {
    Map<String, Object> validatorProperties = new HashMap<>();
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_ID, DATE_VALIDATOR_ID);
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_SEVERITY, 2);
    Mockito.doReturn(1).when(newValidatorServiceReference).compareTo(Mockito.anyObject());
    Validator<String> stringValidator = new StringValidator();
    validatorMap.put(validatorProperties, stringValidator, newValidatorServiceReference);
    Assert.assertEquals(new ValidatorMetadata(stringValidator, newValidatorServiceReference, 2), validatorMap.get(DATE_VALIDATOR_ID));
}
Also used : HashMap(java.util.HashMap) StringValidator(org.apache.sling.validation.impl.util.examplevalidators.StringValidator) ValidatorMetadata(org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata) Test(org.junit.Test)

Example 4 with ValidatorMetadata

use of org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata in project sling by apache.

the class ValidatorMapTest method testPutValidatorWithSameValidatorIdAndLowerRanking.

@Test
public void testPutValidatorWithSameValidatorIdAndLowerRanking() {
    Map<String, Object> validatorProperties = new HashMap<>();
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_ID, DATE_VALIDATOR_ID);
    validatorProperties.put(Validator.PROPERTY_VALIDATOR_SEVERITY, 2);
    Mockito.doReturn(-1).when(newValidatorServiceReference).compareTo(Mockito.anyObject());
    Validator<String> stringValidator = new StringValidator();
    validatorMap.put(validatorProperties, stringValidator, newValidatorServiceReference);
    Assert.assertEquals(new ValidatorMetadata(dateValidator, validatorServiceReference, 10), validatorMap.get(DATE_VALIDATOR_ID));
}
Also used : HashMap(java.util.HashMap) StringValidator(org.apache.sling.validation.impl.util.examplevalidators.StringValidator) ValidatorMetadata(org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata) Test(org.junit.Test)

Aggregations

ValidatorMetadata (org.apache.sling.validation.impl.ValidatorMap.ValidatorMetadata)4 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 StringValidator (org.apache.sling.validation.impl.util.examplevalidators.StringValidator)2 Nonnull (javax.annotation.Nonnull)1 SlingValidationException (org.apache.sling.validation.SlingValidationException)1 ValidatorInvocation (org.apache.sling.validation.model.ValidatorInvocation)1