-
Notifications
You must be signed in to change notification settings - Fork 174
Add must_not_match validator #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
| /// Validates that the 2 given fields do not match. | ||
| /// Both fields are optionals | ||
| #[must_use] | ||
| pub fn validate_must_not_match<T: PartialEq>(a: T, b: T) -> bool { | ||
| a != b | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::borrow::Cow; | ||
|
|
||
| use super::validate_must_not_match; | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_strings_valid() { | ||
| assert!(validate_must_not_match("hey".to_string(), "ho".to_string())) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_cows_valid() { | ||
| let left: Cow<'static, str> = "hey".into(); | ||
| let right: Cow<'static, str> = String::from("ho").into(); | ||
| assert!(validate_must_not_match(left, right)) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_numbers() { | ||
| assert!(validate_must_not_match(2, 3)) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_numbers_false() { | ||
| assert_eq!(false, validate_must_not_match(2, 2)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_numbers_option_false() { | ||
| assert_eq!(false, validate_must_not_match(Some(2), Some(2))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_numbers_option_true() { | ||
| assert!(validate_must_not_match(Some(6), Some(7))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_none_some() { | ||
| assert!(validate_must_not_match(None, Some(3))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_some_none() { | ||
| assert!(validate_must_not_match(Some(3), None)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_validate_must_not_match_none_none_false() { | ||
| // We need to define one of the values here as rust | ||
| // can not infer the generic type from None and None | ||
| let a: Option<u64> = None; | ||
| assert_eq!(false, validate_must_not_match(a, None)); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| use quote::quote; | ||
|
|
||
| use crate::types::MustNotMatch; | ||
| use crate::utils::{quote_code, quote_message, CrateName}; | ||
|
|
||
| pub fn must_not_match_tokens( | ||
| crate_name: &CrateName, | ||
| must_not_match: MustNotMatch, | ||
| field_name: &proc_macro2::TokenStream, | ||
| field_name_str: &str, | ||
| ) -> proc_macro2::TokenStream { | ||
| let o = must_not_match.other; | ||
| let (other, other_err) = | ||
| (quote!(self.#o), quote!(err.add_param(::std::borrow::Cow::from("other"), &self.#o);)); | ||
|
|
||
| let message = quote_message(must_not_match.message); | ||
| let code = quote_code(crate_name, must_not_match.code, "must_not_match"); | ||
|
|
||
| quote! { | ||
| if !#crate_name::validate_must_not_match(&#field_name, &#other) { | ||
| #code | ||
| #message | ||
| #other_err | ||
| err.add_param(::std::borrow::Cow::from("value"), &#field_name); | ||
| errors.add(#field_name_str, err); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
9 changes: 9 additions & 0 deletions
9
validator_derive_tests/tests/compile-fail/must_not_match/field_doesnt_exist.rs
This file contains hidden or 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,9 @@ | ||
| use validator::Validate; | ||
|
|
||
| #[derive(Validate)] | ||
| struct Test { | ||
| #[validate(must_not_match(other = password2))] | ||
| password: String, | ||
| } | ||
|
|
||
| fn main() {} |
10 changes: 10 additions & 0 deletions
10
validator_derive_tests/tests/compile-fail/must_not_match/field_doesnt_exist.stderr
This file contains hidden or 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,10 @@ | ||
| error[E0609]: no field `password2` on type `&Test` | ||
| --> tests/compile-fail/must_not_match/field_doesnt_exist.rs:5:39 | ||
| | | ||
| 5 | #[validate(must_not_match(other = password2))] | ||
| | ^^^^^^^^^ unknown field | ||
| | | ||
| help: a field with a similar name exists | ||
| | | ||
| 5 | #[validate(must_not_match(other = password))] | ||
| | ~~~~~~~~ |
10 changes: 10 additions & 0 deletions
10
validator_derive_tests/tests/compile-fail/must_not_match/field_type_doesnt_match.rs
This file contains hidden or 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,10 @@ | ||
| use validator::Validate; | ||
|
|
||
| #[derive(Validate)] | ||
| struct Test { | ||
| #[validate(must_not_match(other = "password2"))] | ||
| password: String, | ||
| password2: i32, | ||
| } | ||
|
|
||
| fn main() {} |
17 changes: 17 additions & 0 deletions
17
validator_derive_tests/tests/compile-fail/must_not_match/field_type_doesnt_match.stderr
This file contains hidden or 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,17 @@ | ||
| error[E0308]: mismatched types | ||
| --> tests/compile-fail/must_not_match/field_type_doesnt_match.rs:3:10 | ||
| | | ||
| 3 | #[derive(Validate)] | ||
| | ^^^^^^^^ | ||
| | | | ||
| | expected `&String`, found `&i32` | ||
| | arguments to this function are incorrect | ||
| | | ||
| = note: expected reference `&String` | ||
| found reference `&i32` | ||
| note: function defined here | ||
| --> $WORKSPACE/validator/src/validation/must_not_match.rs | ||
| | | ||
| | pub fn validate_must_not_match<T: PartialEq>(a: T, b: T) -> bool { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| = note: this error originates in the derive macro `Validate` (in Nightly builds, run with -Z macro-backtrace for more info) |
9 changes: 9 additions & 0 deletions
9
validator_derive_tests/tests/compile-fail/must_not_match/unexpected_name_value.rs
This file contains hidden or 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,9 @@ | ||
| use validator::Validate; | ||
|
|
||
| #[derive(Validate)] | ||
| struct Email { | ||
| #[validate(not_a(other = "validator"))] | ||
| email: String, | ||
| } | ||
|
|
||
| fn main() {} |
5 changes: 5 additions & 0 deletions
5
validator_derive_tests/tests/compile-fail/must_not_match/unexpected_name_value.stderr
This file contains hidden or 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,5 @@ | ||
| error: Unknown field: `not_a` | ||
| --> tests/compile-fail/must_not_match/unexpected_name_value.rs:5:16 | ||
| | | ||
| 5 | #[validate(not_a(other = "validator"))] | ||
| | ^^^^^ |
This file contains hidden or 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,73 @@ | ||
| use validator::Validate; | ||
|
|
||
| #[test] | ||
| fn can_validate_valid_must_not_match() { | ||
| #[derive(Debug, Validate)] | ||
| struct TestStruct { | ||
| #[validate(must_not_match(other = "val2"))] | ||
| val: String, | ||
| val2: String, | ||
| } | ||
|
|
||
| let s = TestStruct { val: "bob".to_string(), val2: "alice".to_string() }; | ||
|
|
||
| assert!(s.validate().is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn matching_fails_validation() { | ||
| #[derive(Debug, Validate)] | ||
| struct TestStruct { | ||
| #[validate(must_not_match(other = "val2"))] | ||
| val: String, | ||
| val2: String, | ||
| } | ||
|
|
||
| let s = TestStruct { val: "bob".to_string(), val2: "bob".to_string() }; | ||
|
|
||
| let res = s.validate(); | ||
| assert!(res.is_err()); | ||
| let err = res.unwrap_err(); | ||
| let errs = err.field_errors(); | ||
| assert!(errs.contains_key("val")); | ||
| assert_eq!(errs["val"].len(), 1); | ||
| assert_eq!(errs["val"][0].code, "must_not_match"); | ||
| assert_eq!(errs["val"][0].params["value"], "bob"); | ||
| assert_eq!(errs["val"][0].params["other"], "bob"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn can_specify_code_for_must_not_match() { | ||
| #[derive(Debug, Validate)] | ||
| struct TestStruct { | ||
| #[validate(must_not_match(other = "val2", code = "oops"))] | ||
| val: String, | ||
| val2: String, | ||
| } | ||
| let s = TestStruct { val: "bob".to_string(), val2: "bob".to_string() }; | ||
| let res = s.validate(); | ||
| assert!(res.is_err()); | ||
| let err = res.unwrap_err(); | ||
| let errs = err.field_errors(); | ||
| assert!(errs.contains_key("val")); | ||
| assert_eq!(errs["val"].len(), 1); | ||
| assert_eq!(errs["val"][0].code, "oops"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn can_specify_message_for_must_not_match() { | ||
| #[derive(Debug, Validate)] | ||
| struct TestStruct { | ||
| #[validate(must_not_match(other = "val2", message = "oops"))] | ||
| val: String, | ||
| val2: String, | ||
| } | ||
| let s = TestStruct { val: "bob".to_string(), val2: "bob".to_string() }; | ||
| let res = s.validate(); | ||
| assert!(res.is_err()); | ||
| let err = res.unwrap_err(); | ||
| let errs = err.field_errors(); | ||
| assert!(errs.contains_key("val")); | ||
| assert_eq!(errs["val"].len(), 1); | ||
| assert_eq!(errs["val"][0].clone().message.unwrap(), "oops"); | ||
| } |
This file contains hidden or 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,14 @@ | ||
| use validator::Validate; | ||
|
|
||
| #[derive(Validate)] | ||
| struct Test { | ||
| #[validate(must_not_match(other = s2))] | ||
| s: String, | ||
| s2: String, | ||
|
|
||
| #[validate(must_not_match(other = "s4"))] | ||
| s3: usize, | ||
| s4: usize, | ||
| } | ||
|
|
||
| fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we can use
must_matchrather than create a new validator. Something likemust_match(expected=false)with a default of true (bad name but just to illustrate)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you didn't approve generic
not()(Add not_match validator #141) and we havecontainsanddoes_not_containI thought that we need a new oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, can we move on?