-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from lasantosr/dive-context
Allow an optional context when using dive
- Loading branch information
Showing
9 changed files
with
109 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::util; | ||
|
||
#[derive(Clone, Copy, Debug, garde::Validate)] | ||
#[garde(context((usize, usize) as ctx))] | ||
struct Inner<'a> { | ||
#[garde(length(min = ctx.0, max = ctx.1))] | ||
field: &'a str, | ||
} | ||
|
||
#[derive(Debug, garde::Validate)] | ||
struct Test<'a> { | ||
#[garde(skip)] | ||
min: usize, | ||
#[garde(skip)] | ||
max: usize, | ||
#[garde(dive((self.min, self.max)))] | ||
inner: Inner<'a>, | ||
} | ||
|
||
#[derive(Debug, garde::Validate)] | ||
#[garde(context((usize, usize)))] | ||
struct Test2<'a> { | ||
#[garde(dive)] | ||
inner: Inner<'a>, | ||
} | ||
|
||
#[test] | ||
fn valid() { | ||
let inner = Inner { field: "asdf" }; | ||
util::check_ok(&[Test2 { inner }], &(1, 5)); | ||
util::check_ok( | ||
&[Test { | ||
min: 1, | ||
max: 5, | ||
inner, | ||
}], | ||
&(), | ||
); | ||
} | ||
|
||
#[test] | ||
fn invalid() { | ||
let inner = Inner { field: "asdfgh" }; | ||
util::check_fail!(&[Test2 { inner }], &(1, 5)); | ||
util::check_fail!( | ||
&[Test { | ||
min: 1, | ||
max: 5, | ||
inner, | ||
}], | ||
&(), | ||
); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
garde/tests/rules/snapshots/rules__rules__dive_with_ctx__invalid-2.snap
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
source: garde/tests/./rules/dive_with_ctx.rs | ||
expression: snapshot | ||
snapshot_kind: text | ||
--- | ||
Test { | ||
min: 1, | ||
max: 5, | ||
inner: Inner { | ||
field: "asdfgh", | ||
}, | ||
} | ||
inner.field: length is greater than 5 |
11 changes: 11 additions & 0 deletions
11
garde/tests/rules/snapshots/rules__rules__dive_with_ctx__invalid.snap
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
source: garde/tests/./rules/dive_with_ctx.rs | ||
expression: snapshot | ||
snapshot_kind: text | ||
--- | ||
Test2 { | ||
inner: Inner { | ||
field: "asdfgh", | ||
}, | ||
} | ||
inner.field: length is greater than 5 |
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
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