trevrpc_protovalidate

protovalidate request validation for TrevRPC's Erlang servers and clients and its Gleam binding: requests that break their buf.validate rules, CEL rules included, fail before the handler runs, or before a client sends them. It checks them with the protovalidate package, and is a Hex package of its own so that trevrpc itself does not depend on protovalidate or CEL.

Versioning: this package has a version of its own. Each release depends on the trevrpc release it was built with, and every trevrpc release is followed by one of this package. The TrevRPC runtimes share one version, and runtimes of the same version are compatible with each other; compatibility across different versions isn't guaranteed yet, a milestone for 2.0.

%% rebar.config
{deps, [trevrpc, trevrpc_protovalidate]}.

protoc-gen-trevrpc-erlang gives the service modules of proto files that import buf/validate/validate.proto a descriptor_set/0: the descriptors of the file and every file it imports, which the validator reads the rules from.

{ok, Validator} = trevrpc_protovalidate:new([example_v1_greeter:descriptor_set()]),
{ok, Server} = trevrpc_server:start(#{
    %% ...
    routes => example_v1_greeter:routes(greeter_handler),
    validator => Validator
}),

%% Clients check the requests they send with the same validator, without a round trip.
{ok, Channel} = trevrpc_channel:start(#{
    %% ...
    validator => Validator
}).

The Gleam module trevrpc/protovalidate makes the same validator for server.Config and channel.Config, from the descriptor_set() of the service modules that protoc-gen-trevrpc-gleam generates:

let assert Ok(validator) = protovalidate.new([greeter_trevrpc.descriptor_set()])
server.Config(..config, validator: Some(validator))

See Request Validation in the Erlang guide.

Development

rebar3 and Gleam both build and test the package, as they do trevrpc, with trevrpc and qmux from _checkouts links and from its path dependency. The tests need protoc, which writes the descriptor sets of test/proto, whose protos import buf/validate/validate.proto from the repository's interop/proto.

rebar3 eunit      # the checks the validator binds to request types, and a server and channel on loopback
rebar3 xref
rebar3 dialyzer
gleam test        # the Gleam binding

nix build .#trevrpc-erlang-protovalidate runs the rebar3 ones, the trevrpc-erlang-protovalidate-gleam check (nix build .#trevrpc-erlang-protovalidate.gleam) builds the package as gleam publish does and runs the Gleam tests, and the trevrpc-erlang-protovalidate-lint check runs erlfmt and gleam format. The Hex dependencies are declared in gleam.toml and locked in manifest.toml; nix run .#sync-erlang-deps copies them into rebar.config and rebar.lock, as it does trevrpc's (see trevrpc-erlang's README).