diff options
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | validator/src/types.rs | 11 | ||||
| -rw-r--r-- | validator_derive/tests/complex.rs | 19 | 
3 files changed, 33 insertions, 2 deletions
| @@ -163,7 +163,10 @@ Examples:  ```  ### phone -Tests whether the String is a valid phone number (in international format, ie. containing the country indicator like `+14152370800` for an US number — where `4152370800` is the national number equivalent, which is seen as invalid). To use this validator, you must enable the `phone` feature for the `validator` crate. +Tests whether the String is a valid phone number (in international format, ie.  +containing the country indicator like `+14152370800` for an US number — where `4152370800`  +is the national number equivalent, which is seen as invalid).  +To use this validator, you must enable the `phone` feature for the `validator` crate.  This validator doesn't take any arguments: `#[validate(phone)]`;  ### custom 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 80ae360..b9363e4 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> { @@ -163,3 +163,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()); +} | 
