Class MessageValidation<M extends PMessage<M>,​E extends java.lang.Exception>

  • Type Parameters:
    M - The message type to be validated.
    E - The exception to be thrown on validation failure.

    public class MessageValidation<M extends PMessage<M>,​E extends java.lang.Exception>
    extends java.lang.Object
    Class that handles validation of the structure or content of a message type. This this can do much more fine grained validation than just assigning required fields.
    • Method Detail

      • validate

        public <ME extends PMessageOrBuilder<M>> ME validate​(ME message)
                                                      throws E extends java.lang.Exception
        Validate a message using the built expectations.
        Type Parameters:
        ME - Message or builder type to be validated.
        Parameters:
        message - The message to be validated.
        Returns:
        Message after validation.
        Throws:
        E - On not valid message.
        E extends java.lang.Exception
      • isValid

        public boolean isValid​(PMessageOrBuilder<M> message)
        Just see if the message is valid or not. Does not throw any exception.
        Parameters:
        message - The message to be validated.
        Returns:
        True if the message is valid, false otherwise.
      • validationErrors

        public java.util.List<E> validationErrors​(PMessageOrBuilder<M> message)
        See if the message is valid or not. Collects exceptions to consumers and will try to continue on each failure.
        Parameters:
        message - The message to be validated.
        Returns:
        True if the message is valid, false otherwise.
      • builder

        public static <M extends PMessage<M>,​E extends java.lang.Exception> MessageValidation.Builder<M,​E> builder​(@Nonnull
                                                                                                                               PMessageDescriptor<M> descriptor,
                                                                                                                               @Nonnull
                                                                                                                               java.util.function.Function<java.lang.Exception,​E> onMismatch)
        Create a message validator that throws specific exception on failure.
        Type Parameters:
        M - Message type.
        E - Exception type.
        Parameters:
        descriptor - The message type descriptor to be validated.
        onMismatch - Function producer for thrown exceptions.
        Returns:
        The message validator builder.
      • validate

        public <ME extends PMessageOrBuilder<M>> ME validate​(@Nonnull
                                                             java.lang.String path,
                                                             @Nullable
                                                             ME message)
                                                      throws E extends java.lang.Exception
        Validate a message using the built expectations.
        Type Parameters:
        ME - Message or builder type to be validated.
        Parameters:
        path - Logical path in structure that is being validated. Used in failure exceptions.
        message - The message or builder to be validated.
        Returns:
        Message after validation.
        Throws:
        E - On not valid message.
        E extends java.lang.Exception
      • validationErrors

        public java.util.List<E> validationErrors​(java.lang.String path,
                                                  PMessageOrBuilder<M> message)
        See if the message is valid or not. Collects exceptions to consumers and will try to continue on each failure.
        Parameters:
        path - Logical path in structure that is being validated. Used in failure exceptions.
        message - The message to be validated.
        Returns:
        True if the message is valid, false otherwise.
      • toBuilder

        public MessageValidation.Builder<M,​E> toBuilder()
        Make a builder out of the current validation. The builder can build uppon the validation, but cannot remove expectations from it. It will not modify the original validation, only make a new one.
        Returns:
        A validation builder to extend the current.
      • pathPrefix

        public static java.lang.String pathPrefix​(java.lang.String path)
      • atPathSuffix

        public static java.lang.String atPathSuffix​(java.lang.String path)