use of org.keycloak.validate.ValidationError 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);
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class UriValidator method validateUri.
public boolean validateUri(URI uri, String inputHint, ValidationContext context, Set<String> allowedSchemes, boolean allowFragment, boolean requireValidUrl) throws MalformedURLException {
boolean valid = true;
if (uri.getScheme() != null && !allowedSchemes.contains(uri.getScheme())) {
context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_SCHEME, uri, uri.getScheme()));
valid = false;
}
if (!allowFragment && uri.getFragment() != null) {
context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_FRAGMENT, uri, uri.getFragment()));
valid = false;
}
// scheme, sometimes it doesn't).
if (requireValidUrl && valid) {
// throws an exception
URL ignored = uri.toURL();
}
return valid;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class UriValidator method validate.
@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
if (input == null || (input instanceof String && ((String) input).isEmpty())) {
return context;
}
try {
URI uri = toUri(input);
if (uri == null) {
context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_URI, input));
} else {
Set<String> allowedSchemes = config.getStringSetOrDefault(KEY_ALLOWED_SCHEMES, DEFAULT_ALLOWED_SCHEMES);
boolean allowFragment = config.getBooleanOrDefault(KEY_ALLOW_FRAGMENT, DEFAULT_ALLOW_FRAGMENT);
boolean requireValidUrl = config.getBooleanOrDefault(KEY_REQUIRE_VALID_URL, DEFAULT_REQUIRE_VALID_URL);
validateUri(uri, inputHint, context, allowedSchemes, allowFragment, requireValidUrl);
}
} catch (MalformedURLException | IllegalArgumentException | URISyntaxException e) {
context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_URI, input, e.getMessage()));
}
return context;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class AbstractNumberValidator method doValidate.
@Override
protected void doValidate(Object value, String inputHint, ValidationContext context, ValidatorConfig config) {
if (config == null || config.isEmpty()) {
config = defaultConfig;
}
Number number = null;
if (value != null) {
try {
number = convert(value, config);
} catch (NumberFormatException ignore) {
// N/A
}
}
if (number == null) {
context.addError(new ValidationError(getId(), inputHint, MESSAGE_INVALID_NUMBER));
return;
}
Number min = getMinMaxConfig(config, KEY_MIN);
Number max = getMinMaxConfig(config, KEY_MAX);
if (min != null && isFirstGreaterThanToSecond(min, number)) {
context.addError(new ValidationError(getId(), inputHint, selectRangeErrorMessage(config), min, max));
return;
}
if (max != null && isFirstGreaterThanToSecond(number, max)) {
context.addError(new ValidationError(getId(), inputHint, selectRangeErrorMessage(config), min, max));
return;
}
return;
}
use of org.keycloak.validate.ValidationError 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);
}
Aggregations