use fields_valid::FieldsValidate;
#[derive(FieldsValidate)]
struct User<'r> {
// #[valid(len(5, 11), email, regex("^[\\d@\\.]+$"), "邮箱格式不正确")]
#[valid(len(5, 17), "The length of the mailbox must be between 5-16")]
#[valid(email, "E-mail format is incorrect")]
email: String,
#[valid(len(8, 17), "The minimum password length is 8 digits and the maximum is 16 digits")]
password: String,
#[valid(eq("#password"), "The two passwords are inconsistent")]
password2: &'r str,
#[valid(len(5, 12), "The length of the phone number is incorrect")]
#[valid(regex("\\d+"), "The phone number must be a number")]
phone: Option<String>,
#[valid(range(5.8, 10.1), "Incorrect amount returned")]
amount: u32,
}
#[inline]
fn fields_validate<T: FieldsValidate>(fields: &T) -> Result<(), &'static str> {
fields.fields_validate()
}
fn main() {
let user = User {
email: "[email protected]".to_string(),
password: "12ndd232.23".to_string(),
password2: "1225",
phone: None, //Some("111111".to_string())
amount: 7,
};
println!("{:?}", fields_validate(&user));
}
-
Notifications
You must be signed in to change notification settings - Fork 0
horou-dsk/fields-valid
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Rust struct field validate
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published