-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
cb2cf61
commit e611d5f
Showing
7 changed files
with
71 additions
and
67 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
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 |
---|---|---|
|
@@ -3,18 +3,26 @@ use serde_test::{assert_de_tokens, assert_de_tokens_error, Token}; | |
use std::fmt::Display; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
#[serde(validate = "validate_struct")] | ||
#[serde(validate = "validate_struct1")] | ||
#[serde(validate = "validate_struct2")] | ||
struct Struct { | ||
a: u16, | ||
} | ||
|
||
fn validate_struct(deserialized: &Struct) -> Result<(), impl Display> { | ||
fn validate_struct1(deserialized: &Struct) -> Result<(), impl Display> { | ||
if deserialized.a == 0 { | ||
return Err("field `a` can not be zero"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn validate_struct2(deserialized: &Struct) -> Result<(), impl Display> { | ||
if deserialized.a == 2 { | ||
return Err("field `a` can not be two"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
#[serde(validate = "validate_tuple_struct")] | ||
struct TupleStruct(u16); | ||
|
@@ -53,6 +61,19 @@ fn test_struct() { | |
], | ||
"field `a` can not be zero", | ||
); | ||
|
||
assert_de_tokens_error::<Struct>( | ||
&[ | ||
Token::Struct { | ||
name: "Struct", | ||
len: 1, | ||
}, | ||
Token::Str("a"), | ||
Token::U16(2), | ||
Token::StructEnd, | ||
], | ||
"field `a` can not be two", | ||
); | ||
} | ||
|
||
#[test] | ||
|
@@ -179,11 +200,19 @@ struct ValidateStruct { | |
|
||
#[derive(Debug, PartialEq, validator::Validate, Deserialize)] | ||
#[serde(validator)] | ||
#[serde(validate = "validator_struct_name")] | ||
struct ValidatorStruct { | ||
#[validate(email)] | ||
mail: String, | ||
} | ||
|
||
fn validator_struct_name(deserialized: &ValidatorStruct) -> Result<(), impl Display> { | ||
if deserialized.mail.starts_with("foo@") { | ||
return Err("name can not be 'foo'"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_validate_struct() { | ||
assert_de_tokens( | ||
|
@@ -244,4 +273,17 @@ fn test_validator_struct() { | |
], | ||
"mail: Validation error: email [{\"value\": String(\"email.example.com\")}]", | ||
); | ||
|
||
assert_de_tokens_error::<ValidatorStruct>( | ||
&[ | ||
Token::Struct { | ||
name: "ValidatorStruct", | ||
len: 1, | ||
}, | ||
Token::Str("mail"), | ||
Token::Str("[email protected]"), | ||
Token::StructEnd, | ||
], | ||
"name can not be 'foo'", | ||
); | ||
} |
11 changes: 0 additions & 11 deletions
11
test_suite/tests/ui/validate/both_validator_and_validate.rs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
test_suite/tests/ui/validate/both_validator_and_validate.stderr
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
test_suite/tests/ui/validate/validator_unimplemented.stderr
This file was deleted.
Oops, something went wrong.