Trait serde_valid::validation::ValidateMinLength

source ·
pub trait ValidateMinLength {
    // Required method
    fn validate_min_length(
        &self,
        min_length: usize
    ) -> Result<(), MinLengthError>;
}
Expand description

Min length validation of the string.

See https://json-schema.org/understanding-json-schema/reference/string.html#length

use serde_json::json;
use serde_valid::{Validate, ValidateMinLength};

struct MyType(String);

impl ValidateMinLength for MyType {
    fn validate_min_length(
        &self,
        min_length: usize,
    ) -> Result<(), serde_valid::MinLengthError> {
        self.0.validate_min_length(min_length)
    }
}

#[derive(Validate)]
struct TestStruct {
    #[validate(min_length = 5)]
    val: MyType,
}

let s = TestStruct {
    val: MyType(String::from("abc")),
};

assert_eq!(
    s.validate().unwrap_err().to_string(),
    json!({
        "errors": [],
        "properties": {
            "val": {
                "errors": ["The length of the value must be `>= 5`."]
            }
        }
    })
    .to_string()
);

Required Methods§

Implementors§

source§

impl<T> ValidateMinLength for T
where T: Length + ?Sized,