Skip to content

Commit

Permalink
Merge pull request #127 from greyblake/fix-serde-complex
Browse files Browse the repository at this point in the history
Fix serde_complex example
  • Loading branch information
greyblake authored Feb 25, 2024
2 parents ec3f1fa + c93ed19 commit fd09132
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Support `Arbitrary` for any inner types
* Ability to specify boundaries (`greater`, `greater_or_equal`, `less`, `less_or_equal`, `len_char_min`, `len_char_max`) with expressions or named constants.
* Add `#[inline]` attribute to trivial functions
* Improve error messages

### v0.4.0 - 2023-11-21
* Support of arbitrary inner types with custom sanitizers and validators.
Expand Down
15 changes: 12 additions & 3 deletions examples/serde_complex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ fn main() {
"#;
let res: Result<Product, _> = serde_json::from_str(json);
let err = res.unwrap_err();
assert!(err.to_string().contains("empty, expected valid Name"));
assert_eq!(
err.to_string(),
"Name is empty. Expected valid Name at line 3 column 27"
);
}

{
Expand All @@ -57,7 +60,10 @@ fn main() {
"#;
let res: Result<Product, _> = serde_json::from_str(json);
let err = res.unwrap_err();
assert!(err.to_string().contains("invalid, expected valid ImageUrl"));
assert_eq!(
err.to_string(),
"ImageUrl failed the predicate test. Expected valid ImageUrl at line 4 column 60"
);
}

{
Expand All @@ -71,7 +77,10 @@ fn main() {
"#;
let res: Result<Product, _> = serde_json::from_str(json);
let err = res.unwrap_err();
assert!(err.to_string().contains("too small, expected valid Price"));
assert_eq!(
err.to_string(),
"Price is too small. The value must be greater than 0.0. Expected valid Price at line 6 column 13"
);
}

{
Expand Down
2 changes: 1 addition & 1 deletion nutype_macros/src/common/gen/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn gen_impl_trait_serde_deserialize(
quote! {
#type_name::new(raw_value).map_err(|validation_error| {
// Add a hint about which type is causing the error,
let err_msg = format!("{validation_error}, expected valid {}", #type_name_str);
let err_msg = format!("{validation_error} Expected valid {}", #type_name_str);
<DE::Error as serde::de::Error>::custom(err_msg)
})
}
Expand Down
10 changes: 8 additions & 2 deletions test_suite/tests/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ mod traits {

{
let err = "5,5".parse::<Position>().unwrap_err();
assert_eq!(err.to_string(), "Failed to parse Position: invalid");
assert_eq!(
err.to_string(),
"Failed to parse Position: Position failed the predicate test."
);
}
}
}
Expand Down Expand Up @@ -284,7 +287,10 @@ mod traits {

{
let err = serde_json::from_str::<LinePoint>("{\"x\":7,\"y\":9}").unwrap_err();
assert_eq!(err.to_string(), "invalid, expected valid LinePoint");
assert_eq!(
err.to_string(),
"LinePoint failed the predicate test. Expected valid LinePoint"
);
}

{
Expand Down
10 changes: 8 additions & 2 deletions test_suite/tests/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ mod validators {

let err = Percentage::try_from(-0.1).unwrap_err();

assert_eq!(err.to_string(), "too small");
assert_eq!(
err.to_string(),
"Percentage is too small. The value must be greater or equal to 0.0."
);
}
}
}
Expand Down Expand Up @@ -457,7 +460,10 @@ mod traits {

// Unhappy path: validation error
let err: DistParseError = "12.35".parse::<Dist>().unwrap_err();
assert_eq!(err.to_string(), "Failed to parse Dist: too big");
assert_eq!(
err.to_string(),
"Failed to parse Dist: Dist is too big. The value must be less than 12.34."
);
}

#[test]
Expand Down
10 changes: 8 additions & 2 deletions test_suite/tests/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ mod validators {

let err = Age::try_from(17).unwrap_err();

assert_eq!(err.to_string(), "too small");
assert_eq!(
err.to_string(),
"Age is too small. The value must be greater or equal to 18."
);
}
}
}
Expand Down Expand Up @@ -608,7 +611,10 @@ mod traits {

// Unhappy path: validation error
let err: AgeParseError = "101".parse::<Age>().unwrap_err();
assert_eq!(err.to_string(), "Failed to parse Age: too big");
assert_eq!(
err.to_string(),
"Failed to parse Age: Age is too big. The value must be less or equal to 99."
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod validators {
#[nutype(validate(not_empty))]
pub struct Email(String);

assert_eq!(EmailError::NotEmptyViolated.to_string(), "empty");
assert_eq!(EmailError::NotEmptyViolated.to_string(), "Email is empty.");
}

mod when_boundaries_defined_as_constants {
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/ui/float/visibility/private.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here
--> tests/ui/float/visibility/private.rs:4:5
|
4 | #[nutype(sanitize(with = |n| n.clamp(0.0, 100.0)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion test_suite/tests/ui/float/visibility/private_error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here
--> tests/ui/float/visibility/private_error.rs:4:5
|
4 | #[nutype(validate(greater_or_equal = 0.0, less_or_equal = 100.0))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion test_suite/tests/ui/integer/visibility/private.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the struct `Percentage` which is defined here
--> tests/ui/integer/visibility/private.rs:4:5
|
4 | #[nutype(sanitize(with = |n: i32| n.clamp(0, 100)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the enum `PercentageError` which is defined here
--> tests/ui/integer/visibility/private_error.rs:4:5
|
4 | #[nutype(validate(greater_or_equal = 0, less_or_equal = 100))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion test_suite/tests/ui/string/visibility/private.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the struct `Email` which is defined here
--> tests/ui/string/visibility/private.rs:4:5
|
4 | #[nutype(sanitize(trim))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion test_suite/tests/ui/string/visibility/private_error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ note: ...and refers to the enum `NameError` which is defined here
--> tests/ui/string/visibility/private_error.rs:4:5
|
4 | #[nutype(validate(not_empty))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ you could import this directly
= note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit fd09132

Please sign in to comment.