pub trait Validate {
// Required method
fn validate(&self) -> Vec<ValidationError>;
}
Expand description
A trait for validating structs.
Implement this trait for your structs to define custom validation logic.
The validate
method should return a vector of ValidationError
s indicating
any validation failures.
Required Methods§
Sourcefn validate(&self) -> Vec<ValidationError>
fn validate(&self) -> Vec<ValidationError>
Validates the current instance and returns a list of validation errors.
If the instance is valid, the returned vector should be empty.
§Examples
use struct_validation_core::{Validate, ValidationError};
struct User {
username: String,
email: String,
}
impl Validate for User {
fn validate(&self) -> Vec<ValidationError> {
let mut errors = Vec::new();
if self.username.is_empty() {
errors.push(ValidationError::new("username", "must not be empty"));
}
if !self.email.contains('@') {
errors.push(ValidationError::new("email", "must contain '@'"));
}
errors
}
}