Skip to content

Commit

Permalink
Fix multiple custom validators (#320)
Browse files Browse the repository at this point in the history
Closes #308
  • Loading branch information
Keats committed Apr 10, 2024
1 parent fab9ced commit 81ca1a6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

## 0.18.1 (unreleased)

- Fix multiple custom validation


## 0.18.0 (2024/04/05)

- Fix regressions from the derive rewrite, some things are back to 0.16 (eg custom functions)
Expand Down
2 changes: 1 addition & 1 deletion validator_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "validator_derive"
version = "0.18.0"
version = "0.18.1"
authors = ["Vincent Prouillet <hello@vincentprouillet.com"]
license = "MIT"
description = "Macros 1.1 implementation of #[derive(Validate)]"
Expand Down
5 changes: 4 additions & 1 deletion validator_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ impl ToTokens for ValidateField {
} else {
quote!(&#actual_field)
};

for c in &self.custom {
let tokens = custom_tokens(c.clone(), &custom_actual_field, &field_name_str);
custom = quote!(
#tokens
#custom

#tokens
);
}
if !self.custom.is_empty() {
Expand Down
19 changes: 19 additions & 0 deletions validator_derive_tests/tests/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ fn can_fail_custom_fn_validation() {
assert_eq!(errs["val"][0].params["value"], "");
}

#[test]
fn can_fail_multiple_custom_fn_validation() {
#[derive(Debug, Validate)]
struct TestStruct {
#[validate(custom(function = invalid_custom_fn), custom(function=valid_custom_fn))]
val: String,
}

let s = TestStruct { val: String::new() };
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, "meh");
assert_eq!(errs["val"][0].params["value"], "");
}

#[test]
fn can_specify_message_for_custom_fn() {
#[derive(Debug, Validate)]
Expand Down

0 comments on commit 81ca1a6

Please sign in to comment.