Class Validator

java.lang.Object
build.buf.protovalidate.Validator

public class Validator extends Object
Performs validation on any proto.Message values. The Validator is safe for concurrent use.
  • Constructor Details

    • Validator

      public Validator(Config config)
      Constructs a new Validator.
      Parameters:
      config - specified configuration.
    • Validator

      public Validator()
      Constructs a new Validator with a default configuration.
  • Method Details

    • validate

      public ValidationResult validate(com.google.protobuf.Message msg) throws ValidationException
      Checks that message satisfies its constraints. Constraints are defined within the Protobuf file as options from the buf.validate package. A ValidationResult is returned which contains a list of violations. If the list is empty, the message is valid. If the list is non-empty, the message is invalid. An exception is thrown if the message cannot be validated because the evaluation logic for the message cannot be built (CompilationException), or there is a type error when attempting to evaluate a CEL expression associated with the message (ExecutionException).
      Parameters:
      msg - the Message to be validated.
      Returns:
      the ValidationResult from the evaluation.
      Throws:
      ValidationException - if there are any compilation or validation execution errors.
    • loadMessages

      public void loadMessages(com.google.protobuf.Message... messages) throws CompilationException
      Loads messages that are expected to be validated, allowing the Validator to warm up. Messages included transitively (i.e., fields with message values) are automatically handled.
      Parameters:
      messages - the list of Message to load.
      Throws:
      CompilationException - if there are any compilation errors during warm-up.
    • loadDescriptors

      public void loadDescriptors(com.google.protobuf.Descriptors.Descriptor... descriptors) throws CompilationException
      Loads message descriptors that are expected to be validated, allowing the Validator to warm up. Messages included transitively (i.e., fields with message values) are automatically handled.
      Parameters:
      descriptors - the list of Descriptors.Descriptor to load.
      Throws:
      CompilationException - if there are any compilation errors during warm-up.