-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43320a9
commit 186f949
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod duration; | ||
|
||
pub use duration::duration_exclusive_maximum; | ||
pub use duration::duration_exclusive_minimum; | ||
pub use duration::duration_maximum; | ||
pub use duration::duration_minimum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::time::Duration; | ||
|
||
#[allow(dead_code)] | ||
pub fn duration_maximum( | ||
maximum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val <= maximum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {:?} is greater than maximum {:?}.", | ||
val, maximum | ||
))) | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn duration_minimum( | ||
minimum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val >= minimum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {:?} is less than minimum {:?}.", | ||
val, minimum | ||
))) | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn duration_exclusive_maximum( | ||
maximum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val < maximum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {:?} is greater than or equal to exclusive maximum {:?}.", | ||
val, maximum | ||
))) | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn duration_exclusive_minimum( | ||
minimum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val > minimum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {:?} is less than or equal to exclusive minimum {:?}.", | ||
val, minimum | ||
))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::time::Duration; | ||
|
||
use serde_json::json; | ||
use serde_valid::utils::{duration_maximum, duration_minimum}; | ||
use serde_valid::Validate; | ||
|
||
#[test] | ||
fn duration_maximum_is_ok() { | ||
#[derive(Validate)] | ||
struct TestStruct { | ||
#[validate(custom(duration_maximum(Duration::from_micros(5))))] | ||
val: Duration, | ||
} | ||
|
||
let s = TestStruct { | ||
val: Duration::from_micros(5), | ||
}; | ||
|
||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn duration_minimum_is_ok() { | ||
#[derive(Validate)] | ||
struct TestStruct { | ||
#[validate(custom(duration_minimum(Duration::from_micros(5))))] | ||
val: Duration, | ||
} | ||
|
||
let s = TestStruct { | ||
val: Duration::from_secs(5), | ||
}; | ||
|
||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn duration_maximum_is_err() { | ||
#[derive(Validate)] | ||
struct TestStruct { | ||
#[validate(custom(duration_maximum(Duration::from_micros(5))))] | ||
val: Duration, | ||
} | ||
|
||
let s = TestStruct { | ||
val: Duration::from_micros(10), | ||
}; | ||
|
||
assert_eq!( | ||
s.validate().unwrap_err().to_string(), | ||
json!({ | ||
"errors": [], | ||
"properties": { | ||
"val": { | ||
"errors": [ | ||
"Duration 10µs is greater than maximum 5µs." | ||
] | ||
} | ||
} | ||
}) | ||
.to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn duration_minimum_is_err() { | ||
#[derive(Validate)] | ||
struct TestStruct { | ||
#[validate(custom(duration_minimum(Duration::from_micros(5))))] | ||
val: Duration, | ||
} | ||
|
||
let s = TestStruct { | ||
val: Duration::from_micros(1), | ||
}; | ||
|
||
assert_eq!( | ||
s.validate().unwrap_err().to_string(), | ||
json!({ | ||
"errors": [], | ||
"properties": { | ||
"val": { | ||
"errors": [ | ||
"Duration 1µs is less than minimum 5µs." | ||
] | ||
} | ||
} | ||
}) | ||
.to_string() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters