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);
}
}
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());
}
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());
}
Aggregations