close

Filter

loading table of contents...

Studio Developer Manual / Version 2101

Table Of Contents

7.17.1.2 Implementing Validators

You can implement a validator for a single property or a validator that takes multiple properties into account when computing issues. Single-property validators are generally more reusable across document types and across projects and should cover the vast majority of use cases.

For a single-property validator, you can implement the interface PropertyValidator of the package com.coremedia.rest.validation. The easiest way of doing this is by inheriting from the class ObjectPropertyValidatorBase and implementing the method isValid(Object).

public class MyValidator extends ObjectPropertyValidatorBase {
  @Override
  protected boolean isValid(Object value) {
    return ...;
  }
}

Example 7.76. Implementing a property validator


If you know that all property values belong to a given Java class, you can inherit from PropertyValidatorBase instead, specifying the value type as the generic type argument of the base class and passing a class object of the value class to the base class's constructor. You can then implement a more specific isValid method that immediately receives an argument of the correct type.

To enable a property validator, you register it in a content type validator that is defined in the Spring application context. The following code snippet shows how the validator is applied to the property myProperty of the document type MyDocumentType. Here the validator is configured to apply to all subtypes of the given document type, too. By default, the validator would only apply to exactly the given document type.

@Bean
ContentTypeValidator myContentTypeValidator(CapConnection connection,
                                            MyValidator myValidator) {
  ContentTypeValidator contentTypeValidator = new ContentTypeValidator();
  contentTypeValidator.setConnection(connection);
  contentTypeValidator.setContentType("MyDocumentType");
  contentTypeValidator.setValidatingSubtypes(true);
  contentTypeValidator.setValidators(
    Collections.singletonList(myValidator));
  return contentTypeValidator;
}

@Bean
MyValidator myValidator() {
  MyValidator validator = new MyValidator();
  validator.setProperty("myProperty");
  return validator;
}

Example 7.77. Configuring a property validator


See the Javadoc of the REST Service API and especially the packages com.coremedia.rest.validators and com.coremedia.rest.cap.validators for the predefined validators.

For all validators that inherit from PropertyValidatorBase, which includes all standard validators, you can set the field code in the Spring configuration to an issue code of your choice. If you choose not to do so, the class name of the validator implementation will be used as the issue code. For example, the validator com.coremedia.rest.validators.RegExpValidator creates issue with code RegExpValidator by default.

To provide multiple validators for a single document type you can either provide multiple beans inheriting from contentTypeValidator or, more commonly, multiple validators in the validators property of a single content type validator.

If you want to handle multiple properties of a content at once, your validator should inherit from the base class ContentTypeValidatorBase The single method to implement is validate(Content, Issues), which receives the content to analyze as its first argument and an Issues object as its second argument. Whenever a problem is detected, you can call the method addIssue(severity, property, code, ...) of the issues object to register a new issue.

public class MyContentValidator extends ContentTypeValidatorBase {
  @Override
  public void validate(Content content, Issues issues) {
    if (...) {
      issues.addIssue(Severity.ERROR, "myProperty", "myCode");
    }
  }
}

Example 7.78. Implementing a content validator


By inheriting from ContentTypeValidatorBase you can easily specify the name of the content type to which is validator is applied when configuring the validator into the Spring application context.

@Bean
MyContentValidator myContentValidator(CapConnection connection) {
  MyContentValidator myContentValidator = new MyContentValidator();
  myContentValidator.setConnection(connection);
  myContentValidator.setContentType("MyDocumentType");
  return myContentValidator;
}

Example 7.79. Configuring a content validator


You can also implement the interface CapTypeValidator directly, if you do not want to make use of the convenience methods of ContentTypeValidatorBase. Finally, by implementing com.coremedia.rest.validation.Validator<Content> you could create validators that are not even bound to a document type. This should only be necessary in very rare cases.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.