https://github.com/jusexton/endors
Validation library written in the rust programming language.
https://github.com/jusexton/endors
validation
Last synced: 4 months ago
JSON representation
Validation library written in the rust programming language.
- Host: GitHub
- URL: https://github.com/jusexton/endors
- Owner: jusexton
- Created: 2024-10-24T19:56:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-24T21:29:31.000Z (over 1 year ago)
- Last Synced: 2025-11-02T09:17:58.670Z (7 months ago)
- Topics: validation
- Language: Rust
- Homepage: https://docs.rs/endors
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Endors
Warning: Endors is not production ready and currently has a very unstable API. Use are your own risk.
Validation framework written in the rust programming language.
## Quick Look
```rs
struct User {
first_name: String,
last_name: String,
phone_number: Option
}
struct UserValidator;
impl Validator<&User> for UserValidator {
fn validate(&self, value: &User) -> Result<(), endors::Error> {
// Perform many validations and collect all the results into a single result.
collect_results!(
validate!(value.first_name, Len { min: 1, max: 100 }), // Uses default len error message.
validate!(
value.first_name,
|s: &str| s.len() % 2 == 0 => "First name must have an even number of characters." // Custom error message
),
validate!(
value.last_name,
NotEqual(value.first_name) => "Last name must not equal first name."
),
validate!(
value.phone_number,
IsSome => "Phone number must be provided."
),
)
// OR
// Question mark operator to fail fast and only return the first error that occurs.
validate!(value.first_name, Len { min: 1, max: 100 })?;
validate!(value.first_name, |s: &str| s.len() % 2 == 0 => "First name must have an even number of characters.")?;
validate!(value.last_name, NotEqual(value.first_name) => "Last name must not equal first name.")?;
validate!(value.phone_number, IsSome => "Phone number must be provided."?;
}
}
let result = UserValidator.validate(&User {
first_name: "John".to_string(),
last_name: "Doe".to_string(),
phone_number: Some("123-456-7890".to_string())
});
assert!(result.is_ok())
```