Search in sources :

Example 1 with ValidationResult

use of org.keycloak.validate.ValidationResult in project keycloak by keycloak.

the class AbstractNumberValidator method validateConfig.

@Override
public ValidationResult validateConfig(KeycloakSession session, ValidatorConfig config) {
    Set<ValidationError> errors = new LinkedHashSet<>();
    if (config != null) {
        boolean containsMin = config.containsKey(KEY_MIN);
        boolean containsMax = config.containsKey(KEY_MAX);
        Number min = getMinMaxConfig(config, KEY_MIN);
        Number max = getMinMaxConfig(config, KEY_MAX);
        if (containsMin && min == null) {
            errors.add(new ValidationError(getId(), KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MIN)));
        }
        if (containsMax && max == null) {
            errors.add(new ValidationError(getId(), KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MAX)));
        }
        if (errors.isEmpty() && containsMin && containsMax && (!isFirstGreaterThanToSecond(max, min))) {
            errors.add(new ValidationError(getId(), KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_VALUE));
        }
    }
    ValidationResult s = super.validateConfig(session, config);
    if (!s.isValid()) {
        errors.addAll(s.getErrors());
    }
    return new ValidationResult(errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ValidationError(org.keycloak.validate.ValidationError) ValidationResult(org.keycloak.validate.ValidationResult)

Example 2 with ValidationResult

use of org.keycloak.validate.ValidationResult in project keycloak by keycloak.

the class LengthValidator method validateConfig.

@Override
public ValidationResult validateConfig(KeycloakSession session, ValidatorConfig config) {
    Set<ValidationError> errors = new LinkedHashSet<>();
    if (config == null || config == ValidatorConfig.EMPTY) {
        errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
        errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
    } else {
        if (config.containsKey(KEY_TRIM_DISABLED) && (config.getBoolean(KEY_TRIM_DISABLED) == null)) {
            errors.add(new ValidationError(ID, KEY_TRIM_DISABLED, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_BOOLEAN_VALUE, config.get(KEY_TRIM_DISABLED)));
        }
        boolean containsMin = config.containsKey(KEY_MIN);
        boolean containsMax = config.containsKey(KEY_MAX);
        if (!(containsMin || containsMax)) {
            errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
            errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
        } else {
            if (containsMin && config.getInt(KEY_MIN) == null) {
                errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MIN)));
            }
            if (containsMax && config.getInt(KEY_MAX) == null) {
                errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MAX)));
            }
            if (errors.isEmpty() && containsMin && containsMax && (config.getInt(KEY_MIN) > config.getInt(KEY_MAX))) {
                errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_VALUE));
            }
        }
    }
    return new ValidationResult(errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ValidationError(org.keycloak.validate.ValidationError) ValidationResult(org.keycloak.validate.ValidationResult)

Example 3 with ValidationResult

use of org.keycloak.validate.ValidationResult in project keycloak by keycloak.

the class UPConfigUtils method validateValidationConfig.

/**
 * Validate that validation configuration is correct.
 *
 * @param session to be used for Validator SPI integration
 * @param validatorConfig config to be checked
 * @param errors to add error message in if something is invalid
 */
private static void validateValidationConfig(KeycloakSession session, String validator, Map<String, Object> validatorConfig, String attributeName, List<String> errors) {
    if (isBlank(validator)) {
        errors.add("Validation without validator id is defined for attribute '" + attributeName + "'");
    } else {
        if (session != null) {
            if (Validators.validator(session, validator) == null) {
                errors.add("Validator '" + validator + "' defined for attribute '" + attributeName + "' doesn't exist");
            } else {
                ValidationResult result = Validators.validateConfig(session, validator, ValidatorConfig.configFromMap(validatorConfig));
                if (!result.isValid()) {
                    final StringBuilder sb = new StringBuilder();
                    result.forEachError(err -> sb.append(err.toString() + ", "));
                    errors.add("Validator '" + validator + "' defined for attribute '" + attributeName + "' has incorrect configuration: " + sb.toString());
                }
            }
        }
    }
}
Also used : ValidationResult(org.keycloak.validate.ValidationResult)

Aggregations

ValidationResult (org.keycloak.validate.ValidationResult)3 LinkedHashSet (java.util.LinkedHashSet)2 ValidationError (org.keycloak.validate.ValidationError)2