Trait serde_valid::ValidateMaxLength
source · pub trait ValidateMaxLength {
fn validate_max_length(
&self,
max_length: usize
) -> Result<(), MaxLengthError>;
}
Expand description
Max 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, ValidateMaxLength};
struct MyType(String);
impl ValidateMaxLength for MyType {
fn validate_max_length(
&self,
max_length: usize,
) -> Result<(), serde_valid::MaxLengthError> {
self.0.validate_max_length(max_length)
}
}
#[derive(Validate)]
struct TestStruct {
#[validate(max_length = 5)]
val: MyType,
}
let s = TestStruct {
val: MyType(String::from("abcdef")),
};
assert_eq!(
s.validate().unwrap_err().to_string(),
json!({
"errors": [],
"properties": {
"val": {
"errors": ["The length of the value must be `<= 5`."]
}
}
})
.to_string()
);