yassun7010 / serde_valid

JSON Schema based validation tool using serde.
MIT License
45 stars 10 forks source link

Support for optional? #58

Closed ollyde closed 4 months ago

ollyde commented 5 months ago

Example

use serde_valid::Validate;

#[derive(Serialize, Deserialize, Validate)]
pub struct Input {
    #[validate(max_length = 55)]
    #[validate(max_length = 2)]
    pub name_update: Option<String>
    #[validate(max_length = 100)]
    #[validate(max_length = 5)]
    pub email_update: Option<String>,
}

/// Validate input object that extends Validate via serde_valid
pub fn validate_input<T: Validate>(object_to_validate: &T) -> Result<(), Error> {
    match object_to_validate.validate() {
        Ok(_) => Ok(()),
        Err(e) => {
            eprintln!("Validation error: {0}", e);
            let error_message = format!("Validation error: {0}", e);
            // Some error
        }
    }
}

This will fail since name is optional. There are many use cases for this.

yassun7010 commented 4 months ago

Thanks!

Option is already supported.

See ok test case, and err test case.

If you expect different results from those test cases, please give me more information.