trevrpc/protovalidate

protovalidate request validation: a trevrpc/validator.Validator that checks request messages against their buf.validate rules with the protovalidate package, CEL rules included.

The rules come from descriptor sets: the descriptor_set() of the service modules that protoc-gen-trevrpc-gleam generates for proto files that import buf/validate/validate.proto, or any encoded FileDescriptorSet that holds the request types and every file they import.

let assert Ok(validator) =
  protovalidate.new([greeter_trevrpc.descriptor_set()])
server.Config(..config, validator: Some(validator))
// Clients can check the requests they send with the same validator.
channel.Config(..channel_config, validator: Some(validator))

A request that breaks its rules fails with InvalidArgument and a buf.validate.Violations detail, which trevrpc/violations.from_status reads. A request whose rules cannot be evaluated for its values, such as a division by a field that is 0, fails with Internal.

A type’s rules are compiled when the server starts, or when a channel’s first call of the type starts. Rules that cannot be compiled, such as a CEL expression that does not parse, fail the server’s start, or each call of the type on the channel. A type the descriptor sets do not have is not checked: give the validator the descriptor set of every file whose types carry rules. See trevrpc_protovalidate for the details.

Values

pub fn new(
  descriptor_sets: List(BitArray),
) -> Result(
  fn(String) -> option.Option(
    fn(dynamic.Dynamic, BitArray) -> Result(Nil, status.Status),
  ),
  String,
)

A validator of the request types in the encoded FileDescriptorSets descriptor_sets, whose patterns run on OTP’s re. A file that more than one set has is taken from the first. It fails when a set does not decode.

pub fn new_with_regex(
  descriptor_sets: List(BitArray),
  regex: env.Regex,
) -> Result(
  fn(String) -> option.Option(
    fn(dynamic.Dynamic, BitArray) -> Result(Nil, status.Status),
  ),
  String,
)

A validator of the request types in descriptor_sets, as new makes one, whose string.pattern rules and matches calls run on regex. env.Re2 runs them on RE2, in time linear in what they match, through the re2 package, which the application then depends on, and without which this fails.

✨ Search Document