Search in sources :

Example 1 with ConfigValidationMessage

use of org.eclipse.smarthome.config.core.validation.ConfigValidationMessage in project smarthome by eclipse.

the class ConfigDescriptionValidatorImpl method validate.

/**
 * Validates the given configuration parameters against the given configuration description having the given URI.
 *
 * @param configurationParameters the configuration parameters to be validated
 * @param configDescriptionURI the URI of the configuration description against which the configuration parameters
 *            are to be validated
 *
 * @throws ConfigValidationException if one or more configuration parameters do not match with the configuration
 *             description having the given URI
 * @throws NullPointerException if given config description URI or configuration parameters are null
 */
@Override
@SuppressWarnings("unchecked")
public void validate(Map<String, Object> configurationParameters, URI configDescriptionURI) {
    Objects.requireNonNull(configurationParameters, "Configuration parameters must not be null");
    Objects.requireNonNull(configDescriptionURI, "Config description URI must not be null");
    ConfigDescription configDescription = getConfigDescription(configDescriptionURI);
    if (configDescription == null) {
        logger.warn("Skipping config description validation because no config description found for URI '{}'", configDescriptionURI);
        return;
    }
    Map<String, ConfigDescriptionParameter> map = configDescription.toParametersMap();
    Collection<ConfigValidationMessage> configDescriptionValidationMessages = new ArrayList<>();
    for (String key : configurationParameters.keySet()) {
        ConfigDescriptionParameter configDescriptionParameter = map.get(key);
        if (configDescriptionParameter != null) {
            // If the parameter supports multiple selection, then it may be provided as an array
            if (configDescriptionParameter.isMultiple() && configurationParameters.get(key) instanceof List) {
                // Perform validation on each value in the list separately
                for (Object value : (List<Object>) configurationParameters.get(key)) {
                    ConfigValidationMessage message = validateParameter(configDescriptionParameter, value);
                    if (message != null) {
                        configDescriptionValidationMessages.add(message);
                    }
                }
            } else {
                ConfigValidationMessage message = validateParameter(configDescriptionParameter, configurationParameters.get(key));
                if (message != null) {
                    configDescriptionValidationMessages.add(message);
                }
            }
        }
    }
    if (!configDescriptionValidationMessages.isEmpty()) {
        throw new ConfigValidationException(bundleContext.getBundle(), translationProvider, configDescriptionValidationMessages);
    }
}
Also used : ConfigValidationMessage(org.eclipse.smarthome.config.core.validation.ConfigValidationMessage) ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ArrayList(java.util.ArrayList) List(java.util.List) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)1 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)1 ConfigValidationException (org.eclipse.smarthome.config.core.validation.ConfigValidationException)1 ConfigValidationMessage (org.eclipse.smarthome.config.core.validation.ConfigValidationMessage)1