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
trevrpcrelease it was built with, and everytrevrpcrelease 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
}).
- A request that breaks its rules fails with
invalid_argumentand abuf.validate.Violationsstatus detail, as on the other servers;trevrpc_violations:from_status/1reads it on every Erlang client. On a streaming route, the request message that breaks them fails thetrevrpc_call:recv/1that returned it. A client’s validator fails a unary or server-streaming call without opening a stream, andtrevrpc_client:send/2returns{invalid, Status}for a streamed request, which is not sent, and the call goes on. - Rules are compiled when the server starts, and a rule that cannot compile, such as a CEL expression that does not parse, makes
trevrpc_server:startfail. A channel compiles a type’s rules when its first call of the type starts, and fails each call of a type whose rules cannot compile. A rule that only fails to evaluate for some values, such as a division by a field that is 0 by default, fails only the requests it cannot evaluate, withinternal, and is logged as a warning when it fails for the empty message. - The validator knows the types of the descriptor sets it is given, and leaves the others unchecked: give it the
descriptor_set/0of every service whose request types carry rules.new/1takes several, each file once. - A type’s compiled rules are kept in
persistent_term, once per descriptor set,regexoption and type, for as long as the node runs, as protovalidate keeps each descriptor set’s descriptors, so the checks a server gives each call’s process, and a channel each call, are a few words. Make validators once, at startup, rather than of descriptor sets that keep changing. trevrpc_protovalidate:new(DescriptorSets, #{regex => re2})runsstring.patternrules andmatches()calls on RE2, in time linear in the subject, through the NIF of the re2 package, which the application then depends on; by default they run on OTP’sre(see protovalidate’s README).
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).