-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow unsupported_field_type to be ignored (#189)
* validatior_derive: allow unsupported_field_type to be ignored
- Loading branch information
1 parent
7e85875
commit e64a5ef
Showing
5 changed files
with
69 additions
and
19 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
8 changes: 0 additions & 8 deletions
8
validator_derive_tests/tests/compile-fail/unsupported_field_type.rs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
validator_derive_tests/tests/compile-fail/unsupported_field_type.stderr
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
validator_derive_tests/tests/run-pass/unsupported_field_type.rs
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,8 @@ | ||
use validator::Validate; | ||
|
||
#[derive(Validate)] | ||
struct Test { | ||
s: [u8; 11], | ||
} | ||
|
||
fn main() {} |
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,60 @@ | ||
use validator::{Validate, ValidationError}; | ||
|
||
fn valid_custom_fn(arr: &[u8; 2]) -> Result<(), ValidationError> { | ||
match arr[0] == 1 { | ||
true => Ok(()), | ||
false => Err(ValidationError::new("meh")), | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_validate_valid_email_with_unsupported_array() { | ||
#[derive(Debug, Validate)] | ||
struct TestStruct { | ||
#[validate(email)] | ||
val: String, | ||
#[allow(dead_code)] | ||
array: [u8; 2], | ||
} | ||
|
||
let s = TestStruct { val: "bob@bob.com".to_string(), array: [0u8; 2] }; | ||
|
||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn can_validate_custom_with_unsupported_array() { | ||
#[derive(Debug, Validate)] | ||
struct TestStruct { | ||
#[validate(email)] | ||
val: String, | ||
#[validate(custom = "valid_custom_fn")] | ||
array: [u8; 2], | ||
} | ||
|
||
let s = TestStruct { val: "bob@bob.com".to_string(), array: [1u8, 1u8] }; | ||
|
||
assert!(s.validate().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn can_fail_custom_with_unsupported_array() { | ||
#[derive(Debug, Validate)] | ||
struct TestStruct { | ||
#[validate(email)] | ||
val: String, | ||
#[validate(custom = "valid_custom_fn")] | ||
array: [u8; 2], | ||
} | ||
|
||
let s = TestStruct { val: "bob@bob.com".to_string(), array: [0u8, 1u8] }; | ||
|
||
let res = s.validate(); | ||
assert!(res.is_err()); | ||
let err = res.unwrap_err(); | ||
let errs = err.field_errors(); | ||
assert!(errs.contains_key("array")); | ||
assert_eq!(errs["array"].len(), 1); | ||
assert_eq!(errs["array"][0].code, "meh"); | ||
assert_eq!(errs["array"][0].params["value"][0], 0); | ||
} |