Search in sources :

Example 1 with ValidatorContext

use of org.apache.sling.validation.spi.ValidatorContext in project sling by apache.

the class ValidationServiceImpl method validateValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void validateValue(CompositeValidationResult result, @Nonnull Object value, String property, String relativePath, @Nonnull ValueMap valueMap, Resource resource, @Nonnull Validator validator, ValueMap validatorParameters, @Nonnull ResourceBundle defaultResourceBundle, int severity) {
    try {
        ValidatorContext validationContext = new ValidatorContextImpl(relativePath + property, severity, valueMap, resource, defaultResourceBundle);
        ValidationResult validatorResult = ((Validator) validator).validate(value, validationContext, validatorParameters);
        result.addValidationResult(validatorResult);
    } catch (SlingValidationException e) {
        // wrap in another SlingValidationException to include information about the property
        throw new SlingValidationException("Could not call validator " + validator.getClass().getName() + " for resourceProperty " + relativePath + property, e);
    }
}
Also used : ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) SlingValidationException(org.apache.sling.validation.SlingValidationException) ValidationResult(org.apache.sling.validation.ValidationResult) Validator(org.apache.sling.validation.spi.Validator)

Example 2 with ValidatorContext

use of org.apache.sling.validation.spi.ValidatorContext in project sling by apache.

the class ValidationServiceImplTest method testValidateNeverCalledWithNullValues.

@Test
public void testValidateNeverCalledWithNullValues() throws Exception {
    Validator<String> myValidator = new Validator<String>() {

        @Override
        @Nonnull
        public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
            Assert.assertNotNull("data parameter for validate should never be null", data);
            Assert.assertNotNull("location of context parameter for validate should never be null", context.getLocation());
            Assert.assertNotNull("valueMap of context parameter for validate should never be null", context.getValueMap());
            Assert.assertNull("resource of context parameter for validate cannot be set if validate was called only with a value map", context.getResource());
            Assert.assertNotNull("arguments parameter for validate should never be null", arguments);
            return DefaultValidationResult.VALID;
        }
    };
    validationService.validatorMap.put("someId", myValidator, validatorServiceReference, 10);
    propertyBuilder.validator("someId", 20);
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("field1", "1");
    ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
    Assert.assertThat(vr.getFailures(), Matchers.hasSize(0));
    Assert.assertTrue(vr.isValid());
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Nonnull(javax.annotation.Nonnull) ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) DateValidator(org.apache.sling.validation.impl.util.examplevalidators.DateValidator) RegexValidator(org.apache.sling.validation.impl.validators.RegexValidator) Validator(org.apache.sling.validation.spi.Validator) Test(org.junit.Test)

Example 3 with ValidatorContext

use of org.apache.sling.validation.spi.ValidatorContext in project sling by apache.

the class ValidationServiceImplTest method testResourceWithValidatorLeveragingTheResource.

@Test
public void testResourceWithValidatorLeveragingTheResource() throws Exception {
    Validator<String> extendedValidator = new Validator<String>() {

        @Override
        @Nonnull
        public ValidationResult validate(@Nonnull String data, @Nonnull ValidatorContext context, @Nonnull ValueMap arguments) throws SlingValidationException {
            Resource resource = context.getResource();
            if (resource == null) {
                Assert.fail("Resource must not be null");
            } else {
                Assert.assertThat(resource.getPath(), Matchers.equalTo("/content/validation/1/resource"));
            }
            return DefaultValidationResult.VALID;
        }
    };
    // register validator
    validationService.validatorMap.put("myid", extendedValidator, newValidatorServiceReference, null);
    // accept any digits
    propertyBuilder.validator("myid", null);
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    ValidationModel vm = modelBuilder.build("sometype", "some source");
    // create a resource
    ResourceResolver rr = context.resourceResolver();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("field1", "1");
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", properties, JcrConstants.NT_UNSTRUCTURED, true);
    ValidationResult vr = validationService.validate(testResource, vm);
    Assert.assertTrue(vr.isValid());
}
Also used : Nonnull(javax.annotation.Nonnull) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationModel(org.apache.sling.validation.model.ValidationModel) ValidatorContext(org.apache.sling.validation.spi.ValidatorContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DateValidator(org.apache.sling.validation.impl.util.examplevalidators.DateValidator) RegexValidator(org.apache.sling.validation.impl.validators.RegexValidator) Validator(org.apache.sling.validation.spi.Validator) Test(org.junit.Test)

Aggregations

ValidationResult (org.apache.sling.validation.ValidationResult)3 Validator (org.apache.sling.validation.spi.Validator)3 ValidatorContext (org.apache.sling.validation.spi.ValidatorContext)3 HashMap (java.util.HashMap)2 Nonnull (javax.annotation.Nonnull)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 DateValidator (org.apache.sling.validation.impl.util.examplevalidators.DateValidator)2 RegexValidator (org.apache.sling.validation.impl.validators.RegexValidator)2 ValidationModel (org.apache.sling.validation.model.ValidationModel)2 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)2 Test (org.junit.Test)2 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)1 SlingValidationException (org.apache.sling.validation.SlingValidationException)1 ChildResource (org.apache.sling.validation.model.ChildResource)1