Auto-rules for structs?
quodlibetor opened this issue · 7 comments
If you're looking for an idea for something to do, I think it would be pretty cool if I could:
#[derive(AccordValidate)]
struct Account {
#[accord_rule(length(1..64))]
name: String,
#[accord_rule(length(5..64), contains("@"), contains("."))]
email: String,
#[accord_rule(range(12..127))]
age: i8,
}
And get an auto-impld Account::validate
function. A similar thing like derive(AccordNew)
or AccordBuilder
seem like something that would also be nice so that it's impossible to even build invalid objects in the common case.
Thanks for the input! I am looking at adding capability for derive
ing so that the whole FromData
implementation that is needed to work with Rocket can be derived. Getting something working like what you proposes would be just as awesome if not even more. I'm definitely gonna give that a shot too!
edited for clarity
@ChrisBuchholz Look at https://github.com/Keats/validator the validator_derive
crate does exactly that
@Keats: Sergio of Rocket fame showed it to me yesterday. It's quite cool! Right now, Accord supports stable (and nightly, but with no extra features yet), and I'm working on some derive functionality for enhancing the rocket integration, and possibly other stuff too, using your validator to draw inspiration from!
I'm hoping to land some of the first stuff later today.
Looking at the README and issues, it seems that the plan (regarding structs only) is to build something very similar to validator
with the main (only?) difference being the error messages?
Can you expand a bit on what's going to be different?
@Keats: Well, I have only looked at validator briefly, but I can tell you that my vision for Accord is pretty much what it is right now: that you can use the rules!
macro to validate stuff and get great error messages that are serializable and are able to be directly dropped back to an application that can then use to inform the user about what problems their is with their input. There can always be added more validators of course. Except for that, the only thing missing in my main vision is the easier integration with frameworks like rocket, which can be added on the nightly version of accord with derive
and other stuff.
I'm also gonna explore other ways of setting up the rules, like OP suggested, and like validator does, but that is not really priority. The priority is features that works on stable, which all of Accord does now, and even more features and better integration with third party frameworks (on nightly if its a must, like it is with rocket).
What are you poking at?
Just wanted to avoid duplication of work if the goals coincided but they don't: i18n support is an explicit non-goal for me.
Regarding the nightly, custom derive will be stable early february so don't worry too much about it!
@Keats: sure, no problem.
It is good the more brainpower is used on something, and although accord and validator differs on some aspects, it also overlaps in others. It is also sad if brainpower is scattered too much. Many can become a greater one. There might be a opportunity to merge the two projects for a greater good.
About the custom derive: sure, but some of the things I'm working on for nightly, like better rocket integration, will not be for maybe a year (since rocket will not), so accord has to modeled in a way that accord is not a showstopper for anyone, only the third party libraries/frameworks are, and that accord can work (with a varying degree of features) in most possible scenarios.