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

ru.tinkoff.kora.validation.common.Validator Maven / Gradle / Ivy

There is a newer version: 1.1.16
Show newest version
package ru.tinkoff.kora.validation.common;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.util.List;

/**
 * Validates instances
 *
 * @param  type of instance it supports
 */
public interface Validator {

    /**
     * Validates value and return validation failures if occurred
     *
     * @param value   to validate
     * @param context context of validation and its options {@link ValidationContext}
     * @return validation violations, if input value is null then fails with violation
     */
    @Nonnull
    List validate(@Nullable T value, @Nonnull ValidationContext context);

    /**
     * Validates value and return validation failures if occurred
     *
     * @param value to validate
     * @return validation violations, if input value is null then fails with violation
     */
    @Nonnull
    default List validate(@Nullable T value) {
        return validate(value, ValidationContext.builder().build());
    }

    /**
     * Validates value and return validation failures if occurred
     *
     * @param value   to validate
     * @param context context of validation and its options {@link ValidationContext}
     * @throws ViolationException is thrown if any violations occur, if input value is null then fails with violation
     */
    default void validateAndThrow(@Nullable T value, @Nonnull ValidationContext context) throws ViolationException {
        final List violations = validate(value, context);
        if (!violations.isEmpty()) {
            throw new ViolationException(violations);
        }
    }

    /**
     * Validates value and return validation failures if occurred
     *
     * @param value to validate
     * @throws ViolationException is thrown if any violations occur, if input value is null then fails with violation
     */
    default void validateAndThrow(@Nullable T value) throws ViolationException {
        final List violations = validate(value);
        if (!violations.isEmpty()) {
            throw new ViolationException(violations);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy