Keats / validator

Simple validation for Rust structs
MIT License
1.98k stars 142 forks source link

support async validator #191

Open LyunKi opened 2 years ago

LyunKi commented 2 years ago

In some cases, we may need an async validator, so that we can call value.validate_async(xxx) which just like this js lib https://github.com/yiminghe/async-validator/

IniterWorker commented 2 years ago

Are you requesting to allow async fn call througth the custom field in validate macro ?

bradleyess commented 2 years ago

@IniterWorker - I'm here to suggest that async fn's can be used in custom derive annotations. I'm not sure where to start but would be happy to collaborate on it.

paramako commented 11 months ago

any progress on this?

Keats commented 11 months ago

None. A rewrite of the macro has landed on the next branch if someones wants to have a look.

pintariching commented 11 months ago

We should also think about if we want to be async framework agnistic or support just tokio or smol or others or all of them with features.

DenuxPlays commented 8 months ago

Any progress on this? Or any workaround that works with this crate: https://github.com/rambler-digital-solutions/actix-web-validator ? Supporting only non async methods makes this crate unusable for something like username validation if an async connection is used to the database.

wsantos commented 1 week ago

This is what I've been doing if I have a db/async validator, here is a dummy example, hope it helps until this is fixed.:


    #[derive(Deserialize, Validate)]
    pub struct SignupForm {
        #[validate(length(min = 3))]
        pub username: String,
        #[validate(email)]
        pub email: String,
    }

    let mut validation_errors = ValidationErrors::new();

    if let Err(form_validation_errors) = signup_form.validate() {
        validation_errors = form_validation_errors;
    }

    let user_result =
        User::create(&signup_form.username, &signup_form.email, &app_state.conn).await;

    if user_result.is_err() {
        validation_errors.add(
            "general",
            ValidationError::new("A user with this email or username already exists."),
        );
    } else {
        Token::send_auth_token_email(user_result.unwrap().id, &app_state.conn).await?;
    }