Validation

Coat validation

On creating a config class from the generated builder the configuration will always be validated in order to fail early in case the given configuration is missing some values or existing values cannot be converted into the specified type.

If the configuration is invalid, it will throw a ConfigValidationException. This exception has a method getValidationResult() that returns a value of type ValidationResult that contains more information about the missing or wrong config values.

To issue an error message, the toString() method of the ValidationResult can be used. For example

1try {
2  MyConfig config= MyConfigBuilder.from(props);
3} catch (final ConfigValidationException ex) {
4  System.err.println("Error in config file:\n" 
5                     + ex.getValidationResult().toString());
6  System.exit(1);
7}

Java Bean Validation

While coat doesn’t provide explicit support for Java Bean Validation they can be used together.

Java Bean Validation allows for more specific constraints on the values in a config object, e.g. minimum and maximum values of an integer. Just add the appropriate annotations to the accessor methods and validate them with a jakarta.validation.Validator.

Be aware that Java Bean Validation requires the usage of “get” prefixes on the accessor methods. Coat explicitly supports this use case by stripping that prefix when inferring the config key by default.

See the example project for a usage example of Java Bean Validation with Coat.