io.convergence_platform.common.validations.MinimumValueValidator 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 MinimumValueValidator implements ConstraintValidator {
private double value;
public static void pathVariable(String value, double constraint, String varName) {
boolean valid = true;
if ("".equals(value)) {
valid = false;
} else if (ValidationHelper.isValidDouble(value)) {
valid = validate(Double.parseDouble(value), constraint);
}
ValidationHelper.raiseValidationErrorForPathVariable(valid, "min", varName);
}
@Override
public void initialize(MinimumValue constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
value = constraintAnnotation.value();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
return validate(value, this.value);
}
private static boolean validate(Object value, double constraint) {
if (value == null) {
return false;
}
if (value instanceof Integer) {
return ((Integer) value) >= constraint;
} else if (value instanceof Float) {
return ((Float) value) >= constraint;
} else if (value instanceof Double) {
return ((Double) value) >= constraint;
}
return false;
}
}