RFC: Introduce a new validation construct #1640
Replies: 3 comments 1 reply
-
A couple of things I have been thinking about since I posted the RFC: Pre-Validate built constructsThis RFC would make it possible to pre-validate objects built by the builder thus not doing the same work twice. Staged rolloutWe could get around much of the breaking changes this would cause initially by first changing the signatures to use |
Beta Was this translation helpful? Give feedback.
-
I'm pretty set on naming everything under the root |
Beta Was this translation helpful? Give feedback.
-
Example implementation of it here: https://github.com/twilight-rs/twilight/tree/erk-/validate-rfc-impl, I have found some changes I have to make to the RFC when making that and I will update it soon |
Beta Was this translation helpful? Give feedback.
-
Summary
This RFC introduces a new way to handle validation by moving the validation
into the
validate
crate and exports a new-type that will mean it isverified.
We also provide a small example program here:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=faa5e9fa8447f8bba7e30defc54cce8a
Motivation
Currently the logic behind the validation happens in a separate crate which is
then called from inside of the relevant http functions. One consequence of this
is that there is no way to not use validation for example.
Guide-level explanation
Since a type cannot uniquely describe which validation is needed we introduce
marker struct for different types of validations. For example:
These markers should probably implement a sealed trait to limit what can be used.
Then we introduce struct we can use to wrap validated data in.
Then we introduce a trait to validate data, this trait also includes a trait
method,
validate_unchecked
to skip the validation of data:This is then the methods you call to validate data, the type inference should be able to figure out which trait to call.
Reference-level explanation
Implementations of various validators could look like this:
Functions taking these values would look like the following:
Then it is possible to call them in the following fashion with the type
inference selecting the correct verifier:
Drawbacks
This may be seen as making how it works harder to understand, especially with
the type inference.
Rationale and alternatives
The main alternative would be to stay at status quo and keep it the way we have
now.
Prior art
The main prior art of this in Rust is the
validity
crate which implementessomthing similar. The main difference from the method described in this RFC is
that we use marker structs.
Unresolved questions
Validate or Verify?Validate as that is what is already used.Future possibilities
Beta Was this translation helpful? Give feedback.
All reactions