Skip to content
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

Validate without context #102

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions garde/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ pub trait Validate {
///
/// This method should not be implemented manually. Implement [`Validate::validate_into`] instead,
/// because [`Validate::validate`] has a default implementation that calls [`Validate::validate_into`].
fn validate(&self, ctx: &Self::Context) -> Result<(), Report> {
fn validate(&self) -> Result<(), Report>
where
Self::Context: Default,
{
let ctx = Self::Context::default();
self.validate_with(&ctx)
}

/// Validates `Self`, returning an `Err` with an aggregate of all errors if
/// the validation failed.
///
/// This method should not be implemented manually. Implement [`Validate::validate_into`] instead,
/// because [`Validate::validate_with`] has a default implementation that calls [`Validate::validate_into`].
fn validate_with(&self, ctx: &Self::Context) -> Result<(), Report> {
let mut report = Report::new();
self.validate_into(ctx, &mut Path::empty, &mut report);
match report.is_empty() {
Expand Down Expand Up @@ -79,8 +92,18 @@ impl<T: Validate> Unvalidated<T> {

/// Validates `self`, transforming it into a `Valid<T>`.
/// This is the only way to create an instance of `Valid<T>`.
pub fn validate(self, ctx: &<T as Validate>::Context) -> Result<Valid<T>, Report> {
self.0.validate(ctx)?;
pub fn validate(self) -> Result<Valid<T>, Report>
where
<T as Validate>::Context: Default,
{
self.0.validate()?;
Ok(Valid(self.0))
}

/// Validates `self`, transforming it into a `Valid<T>`.
/// This is the only way to create an instance of `Valid<T>`.
pub fn validate_with(self, ctx: &<T as Validate>::Context) -> Result<Valid<T>, Report> {
self.0.validate_with(ctx)?;
Ok(Valid(self.0))
}
}
Expand Down
2 changes: 1 addition & 1 deletion garde/tests/rules/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn select_macro() {
identifiers: vec![UserIdentifier { id: 10 }, UserIdentifier { id: 20 }],
};

let report = v.validate(&()).unwrap_err();
let report = v.validate().unwrap_err();
{
let errors: Vec<String> = garde::select!(report, identifiers[0])
.map(|e| e.to_string())
Expand Down
2 changes: 1 addition & 1 deletion garde/tests/rules/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ fn url_valid_wrapper() {
field: "htt ps://www.youtube.com/watch?v=dQw4w9WgXcQ",
inner: &["htt ps://www.youtube.com/watch?v=dQw4w9WgXcQ"],
};
println!("{:?}", value.validate(&()).unwrap_err());
println!("{:?}", value.validate().unwrap_err());
}
4 changes: 2 additions & 2 deletions garde/tests/rules/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use owo_colors::OwoColorize;
pub fn check_ok<T: Validate + Debug>(cases: &[T], ctx: &T::Context) {
let mut some_failed = false;
for case in cases {
if let Err(report) = case.validate(ctx) {
if let Err(report) = case.validate_with(ctx) {
eprintln!(
"{} input: {case:?}, errors: [{}]",
"FAIL".red(),
Expand All @@ -32,7 +32,7 @@ pub fn __check_fail<T: Validate + Debug>(cases: &[T], ctx: &T::Context) -> Strin
let mut some_success = false;
let mut snapshot = String::new();
for case in cases {
if let Err(report) = case.validate(ctx) {
if let Err(report) = case.validate_with(ctx) {
writeln!(&mut snapshot, "{case:#?}").unwrap();
write!(&mut snapshot, "{report}").unwrap();
writeln!(&mut snapshot).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions integrations/axum_garde/src/with_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ where

let ctx = FromRef::from_ref(state);
let value = value.into_inner();
let value = Unvalidated::new(value).validate(&ctx)?;
let value = Unvalidated::new(value).validate_with(&ctx)?;

Ok(WithValidation(value))
}
Expand All @@ -98,7 +98,7 @@ where

let ctx = FromRef::from_ref(state);
let value = value.into_inner();
let value = Unvalidated::new(value).validate(&ctx)?;
let value = Unvalidated::new(value).validate_with(&ctx)?;

Ok(WithValidation(value))
}
Expand Down
Loading