Package net.morimekta.providence.util
Class MessageValidation<M extends PMessage<M>,E extends java.lang.Exception>
- java.lang.Object
-
- net.morimekta.providence.util.MessageValidation<M,E>
-
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MessageValidation.Builder<M extends PMessage<M>,E extends java.lang.Exception>
Builder vlass for message validators.static interface
MessageValidation.Expectation<V>
Interface for testing some expectation.static class
MessageValidation.MessageCollectionValidationExpectation<BaseMessage extends PMessage<BaseMessage>,FieldMessage extends PMessage<FieldMessage>,E extends java.lang.Exception>
Test a field that itself is a map of message values using another message validation.static class
MessageValidation.MessageMapValidationExpectation<BaseMessage extends PMessage<BaseMessage>,FieldMessage extends PMessage<FieldMessage>,E extends java.lang.Exception>
Test a field that itself is a map of message values using another message validation.static class
MessageValidation.PredicateExpectation<Value>
Check some value based on a simple predicate.static interface
MessageValidation.SimpleExpectation<V>
Interface for testing some expectation.static interface
MessageValidation.ValidationExpectation<BaseMessage extends PMessage<BaseMessage>,E extends java.lang.Exception>
Test a field that itself is using another message validation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
atPathSuffix(java.lang.String path)
static <M extends PMessage<M>,E extends java.lang.Exception>
MessageValidation.Builder<M,E>builder(PMessageDescriptor<M> descriptor, java.util.function.Function<java.lang.Exception,E> onMismatch)
Create a message validator that throws specific exception on failure.boolean
isValid(PMessageOrBuilder<M> message)
Just see if the message is valid or not.static java.lang.String
pathPrefix(java.lang.String path)
MessageValidation.Builder<M,E>
toBuilder()
Make a builder out of the current validation.<ME extends PMessageOrBuilder<M>>
MEvalidate(java.lang.String path, ME message)
Validate a message using the built expectations.<ME extends PMessageOrBuilder<M>>
MEvalidate(ME message)
Validate a message using the built expectations.java.util.List<E>
validationErrors(java.lang.String path, PMessageOrBuilder<M> message)
See if the message is valid or not.java.util.List<E>
validationErrors(PMessageOrBuilder<M> message)
See if the message is valid or not.
-
-
-
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.
-
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)
-
-