diff options
| author | Vincent Prouillet | 2017-10-10 11:32:17 +0900 |
|---|---|---|
| committer | GitHub | 2017-10-10 11:32:17 +0900 |
| commit | 176c58bc03a9cbcb03087e17ddd5b503ce77fc1f (patch) | |
| tree | bb64d2bdf62c3f4584ab19f96dd56fad0ec9f03e | |
| parent | e62ff2584462e0077e40832160fbacca35860954 (diff) | |
| parent | c644dc0aba4e927d299b0f93fd3b87bbaf8f366c (diff) | |
| download | validator-176c58bc03a9cbcb03087e17ddd5b503ce77fc1f.tar.bz2 | |
Merge pull request #32 from voidxnull/fix/impl-err
Implement Error and Display for ValidationErrors
| -rw-r--r-- | validator/src/types.rs | 11 | ||||
| -rw-r--r-- | validator_derive/tests/complex.rs | 19 |
2 files changed, 29 insertions, 1 deletions
diff --git a/validator/src/types.rs b/validator/src/types.rs index bb176c3..5c62e6d 100644 --- a/validator/src/types.rs +++ b/validator/src/types.rs @@ -59,3 +59,14 @@ impl ValidationErrors { self.0.is_empty() } } + +impl std::error::Error for ValidationErrors { + fn description(&self) -> &str { "Validation failed" } + fn cause(&self) -> Option<&std::error::Error> { None } +} + +impl fmt::Display for ValidationErrors { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(self, fmt) + } +} diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index db5f1bf..0bd74fa 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -9,7 +9,7 @@ extern crate regex; extern crate lazy_static; use regex::Regex; -use validator::{Validate, ValidationError}; +use validator::{Validate, ValidationError, ValidationErrors}; fn validate_unique_username(username: &str) -> Result<(), ValidationError> { @@ -153,3 +153,20 @@ fn test_can_validate_option_fields_without_lifetime() { }; assert!(s.validate().is_ok()); } + +#[test] +fn test_works_with_question_mark_operator() { + fn some_fn() -> Result<(), ValidationErrors> { + let signup = SignupData { + mail: "invalid_email".to_string(), + site: "http://hello.com".to_string(), + first_name: "Bob".to_string(), + age: 18, + }; + + signup.validate()?; + Ok(()) + } + + assert!(some_fn().is_err()); +} |
