protovalidate

protovalidate for Erlang, Elixir and Gleam: checks protobuf messages against the buf.validate rules of their descriptors, and reports the rules they break, on cel_expr. It is pure Erlang, needs Erlang/OTP 29 or later, and passes protovalidate's conformance suite.

Warning

early work in progress

build tool dependency
rebar3 {deps, [protovalidate]}.
Gleam gleam add protovalidate
Mix {:protovalidate, "~> 0.1"}

Messages are checked in their encoded form, described by a FileDescriptorSet that includes buf/validate/validate.proto (protoc --include_imports --descriptor_set_out, or buf build -o), so any protobuf library's messages can be checked. A message type's rules are compiled once, and checking a message decodes only the fields its rules read. Compiled rules are small terms, which can be kept in persistent_term or ETS, or sent to the processes that check messages: the descriptors they read are kept once per descriptor set, in persistent_term.

Erlang

{ok, Validator} = protovalidate:new(DescriptorSet),
{ok, Users} = protovalidate:compile(Validator, <<"example.v1.User">>),
ok = protovalidate:validate(Users, Encoded),
{error, {validation, [#{rule_id := <<"string.min_len">>} | _]}} = protovalidate:validate(Users, Invalid).

Violations are the maps gpb decodes buf.validate.Violation into (see protovalidate_violations), which TrevRPC's runtime reads and writes.

Elixir

Elixir calls the Erlang modules directly:

{:ok, validator} = :protovalidate.new(descriptor_set)
{:ok, users} = :protovalidate.compile(validator, "example.v1.User")

case :protovalidate.validate(users, encoded) do
  :ok -> :ok
  {:error, {:validation, [%{rule_id: rule_id, message: message} | _]}} -> {:invalid, rule_id, message}
  {:error, {kind, message}} -> {kind, message}
end

Gleam

protovalidate/validator checks messages, and protovalidate/violation has the rules they break, as the same types as TrevRPC's trevrpc/violations.

import protovalidate/validator
import protovalidate/violation

pub fn check(descriptor_set: BitArray, encoded: BitArray) -> String {
  let assert Ok(validator) = validator.new(descriptor_set)
  let assert Ok(users) = validator.compile(validator, "example.v1.User")
  case validator.validate(users, encoded) {
    Ok(Nil) -> "valid"
    Error(validator.Invalid([first, ..])) -> violation.to_string(first)
    Error(_) -> "rules that cannot be checked"
  }
}

Regular expressions

string.pattern rules, and rules' matches() calls, run on OTP's re by default, which refuses patterns it would run with another meaning than RE2's and fails matches that backtrack too far (see cel_expr's README). As patterns often match what a peer sends, running them on RE2 itself, in time linear in the subject, is worth a NIF: depend on the re2 package too, and choose it when making the validator.

{ok, Validator} = protovalidate:new(DescriptorSet, #{regex => re2}).
{:ok, validator} = :protovalidate.new(descriptor_set, %{regex: :re2})
let assert Ok(validator) = validator.new_with_regex(descriptor_set, env.Re2)

Without the re2 package, making such a validator fails, with an error that says so.

License

MIT. The address and URI checks are ported from protovalidate-go's, under its Apache-2.0 license.