All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.convergence_platform.common.validations.OneOfValidator Maven / Gradle / Ivy

Go to download

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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy