Current section
Files
Jump to
Current section
Files
trevrpc_protovalidate
README.md
README.md
# trevrpc_protovalidate
[protovalidate](https://protovalidate.com) request validation for [TrevRPC](https://trev.zip/llc/TrevRPC)'s Erlang servers and clients and its Gleam binding: requests that break their [`buf.validate`](https://buf.build/bufbuild/protovalidate) rules, CEL rules included, fail before the handler runs, or before a client sends them. It checks them with the [`protovalidate`](https://hex.pm/packages/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.
```erlang
%% 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.
```erlang
{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_argument` and a `buf.validate.Violations` status detail, as on the other servers; `trevrpc_violations:from_status/1` reads it on every Erlang client. On a streaming route, the request message that breaks them fails the `trevrpc_call:recv/1` that returned it. A client's validator fails a unary or server-streaming call without opening a stream, and `trevrpc_client:send/2` returns `{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:start` fail. 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, with `internal`, 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/0` of every service whose request types carry rules. `new/1` takes several, each file once.
- A type's compiled rules are kept in `persistent_term`, once per descriptor set, `regex` option 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})` runs `string.pattern` rules and `matches()` calls on RE2, in time linear in the subject, through the NIF of the [re2](https://hex.pm/packages/re2) package, which the application then depends on; by default they run on OTP's `re` (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:
```gleam
let assert Ok(validator) = protovalidate.new([greeter_trevrpc.descriptor_set()])
server.Config(..config, validator: Some(validator))
```
See [Request Validation](https://trev.zip/llc/TrevRPC/wiki/Erlang-Guide#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`.
```sh
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](../README.md#development)).