Interface Validator<T>

    • Method Detail

      • apply

        ValidationResult apply​(T value,
                               ValueContext context)
        Validates the given value. Returns a ValidationResult instance representing the outcome of the validation.
        Specified by:
        apply in interface BiFunction<T,​ValueContext,​ValidationResult>
        Parameters:
        value - the input value to validate
        context - the value context for validation
        Returns:
        the validation result
      • alwaysPass

        static <T> Validator<T> alwaysPass()
        Returns a validator that passes any value.
        Type Parameters:
        T - the value type
        Returns:
        an always-passing validator
      • from

        static <T> Validator<T> from​(SerializablePredicate<T> guard,
                                     String errorMessage)
        Builds a validator out of a conditional function and an error message. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, ValidationResult.error(String) is returned with the given message and error level ErrorLevel.ERROR.

        For instance, the following validator checks if a number is between 0 and 10, inclusive:

         Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
                 "number must be between 0 and 10");
         
        Type Parameters:
        T - the value type
        Parameters:
        guard - the function used to validate, not null
        errorMessage - the message returned if validation fails, not null
        Returns:
        the new validator using the function
      • from

        static <T> Validator<T> from​(SerializablePredicate<T> guard,
                                     String errorMessage,
                                     ErrorLevel errorLevel)
        Builds a validator out of a conditional function and an error message. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, ValidationResult.error(String) is returned with the given message and error level.

        For instance, the following validator checks if a number is between 0 and 10, inclusive:

         Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10,
                 "number must be between 0 and 10", ErrorLevel.ERROR);
         
        Type Parameters:
        T - the value type
        Parameters:
        guard - the function used to validate, not null
        errorMessage - the message returned if validation fails, not null
        errorLevel - the error level for failures from this validator, not null
        Returns:
        the new validator using the function
      • from

        static <T> Validator<T> from​(SerializablePredicate<T> guard,
                                     ErrorMessageProvider errorMessageProvider)
        Builds a validator out of a conditional function and an error message provider. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, Result.error() is returned with the message from the provider.
        Type Parameters:
        T - the value type
        Parameters:
        guard - the function used to validate, not null
        errorMessageProvider - the provider to generate error messages, not null
        Returns:
        the new validator using the function
      • from

        static <T> Validator<T> from​(SerializablePredicate<T> guard,
                                     ErrorMessageProvider errorMessageProvider,
                                     ErrorLevel errorLevel)
        Builds a validator out of a conditional function and an error message provider. If the function returns true, the validator returns Result.ok(); if it returns false or throws an exception, Result.error() is returned with the message from the provider.
        Type Parameters:
        T - the value type
        Parameters:
        guard - the function used to validate, not null
        errorMessageProvider - the provider to generate error messages, not null
        errorLevel - the error level for failures from this validator, not null
        Returns:
        the new validator using the function