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

spring.turbo.bean.jsr380.FieldsValueNotMatchValidator Maven / Gradle / Ivy

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *    ____             _            _____           _
 *   / ___| _ __  _ __(_)_ __   __ |_   _|   _ _ __| |__   ___
 *   \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
 *    ___) | |_) | |  | | | | | (_| || || |_| | |  | |_) | (_) |
 *   |____/| .__/|_|  |_|_| |_|\__, ||_| \__,_|_|  |_.__/ \___/
 *         |_|                 |___/   https://github.com/yingzhuo/spring-turbo
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.bean.jsr380;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.lang.Nullable;
import spring.turbo.util.Asserts;

import java.util.Objects;

/**
 * @author 应卓
 * @see FieldsValueNotMatch
 * @since 1.0.0
 */
public class FieldsValueNotMatchValidator implements ConstraintValidator {

    @Nullable
    private String field;

    @Nullable
    private String fieldMatch;

    @Override
    public void initialize(FieldsValueNotMatch annotation) {
        this.field = annotation.field();
        this.fieldMatch = annotation.fieldMatch();
    }

    @Override
    public boolean isValid(@Nullable Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        Asserts.notNull(field);
        Asserts.notNull(fieldMatch);

        Object fieldValue = new BeanWrapperImpl(value).getPropertyValue(field);
        Object fieldMatchValue = new BeanWrapperImpl(value).getPropertyValue(fieldMatch);
        return !Objects.equals(fieldValue, fieldMatchValue);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy