io.convergence_platform.common.validations.OneOfValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.validations;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class OneOfValidator implements ConstraintValidator {
private String[] value;
public static void pathVariable(String value, String[] values, String varName) {
boolean valid = true;
if ("".equals(value)) {
valid = false;
} else {
valid = validate(value, values);
}
ValidationHelper.raiseValidationErrorForPathVariable(valid, "oneof", varName);
}
@Override
public void initialize(OneOf constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
value = constraintAnnotation.value();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return validate(value, this.value);
}
private static boolean validate(String value, String[] values) {
for (int i = 0; i < values.length; i++) {
if ((values[i] == null && value == null) || values[i].equals(value)) {
return true;
}
}
return false;
}
}