Skip to content

Commit

Permalink
Add UI tests for custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Aug 22, 2024
1 parent 8387ad0 commit ffeb029
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
8 changes: 4 additions & 4 deletions nutype_macros/src/common/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ where
match (validators.len(), maybe_with, maybe_error) {
(0, Some(with), Some(error)) => Ok(RawValidation::Custom { with, error }),
(0, Some(_), None) => {
let msg = "`with` attribute must be used with `error` attribute";
let msg = "The `with` attribute requires an accompanying `error` attribute.\nPlease provide the error type that the `with` validation function returns.";
Err(syn::Error::new(input.span(), msg))
}
(0, None, Some(_)) => {
let msg = "`error` attribute must be used with `with` attribute";
(0, None, Some(error_type)) => {
let msg = format!("The `error` attribute requires an accompanying `with` attribute.\nPlease provide the validation function that returns Result<(), {error_type}>.");
Err(syn::Error::new(input.span(), msg))
}
(0, None, None) => {
Expand All @@ -197,7 +197,7 @@ where
(_, None, None) => Ok(RawValidation::Standard { validators }),
(_, _, _) => {
let msg =
"`with` and `error` attributes cannot be used mixed with other validators";
"`with` and `error` attributes cannot be used mixed with other validators.";
Err(syn::Error::new(input.span(), msg))
}
}
Expand Down
14 changes: 14 additions & 0 deletions test_suite/tests/ui/common/custom_validaiton_no_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use nutype::nutype;

#[nutype(
validate(error = NumError)
)]
pub struct Num(i32);

#[derive(Debug)]
enum NumError {
TooBig,
TooSmall,
}

fn main () {}
6 changes: 6 additions & 0 deletions test_suite/tests/ui/common/custom_validaiton_no_with.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: The `error` attribute requires an accompanying `with` attribute.
Please provide the validation function that returns Result<(), NumError>.
--> tests/ui/common/custom_validaiton_no_with.rs:4:30
|
4 | validate(error = NumError)
| ^
24 changes: 24 additions & 0 deletions test_suite/tests/ui/common/custom_validation_mixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use nutype::nutype;

#[nutype(
validate(with = validate_num, error = NumError, predicate = |val| *val > 0 )
)]
pub struct Num(i32);

fn validate_num(val: &i32) -> Result<(), NumError> {
if *val > 100 {
Err(NumError::TooBig)
} else if *val < 0 {
Err(NumError::TooSmall)
} else {
Ok(())
}
}

#[derive(Debug)]
enum NumError {
TooBig,
TooSmall,
}

fn main () {}
5 changes: 5 additions & 0 deletions test_suite/tests/ui/common/custom_validation_mixed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `with` and `error` attributes cannot be used mixed with other validators.
--> tests/ui/common/custom_validation_mixed.rs:4:80
|
4 | validate(with = validate_num, error = NumError, predicate = |val| *val > 0 )
| ^
6 changes: 6 additions & 0 deletions test_suite/tests/ui/common/custom_validation_no_error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: The `with` attribute requires an accompanying `error` attribute.
Please provide the error type that the `with` validation function returns.
--> tests/ui/common/custom_validation_no_error.rs:4:34
|
4 | validate(with = validate_num,)
| ^

0 comments on commit ffeb029

Please sign in to comment.