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

DependentRules when using .Custom() validation #2170

Closed
mythjuha opened this issue Nov 17, 2023 · 5 comments
Closed

DependentRules when using .Custom() validation #2170

mythjuha opened this issue Nov 17, 2023 · 5 comments
Milestone

Comments

@mythjuha
Copy link

mythjuha commented Nov 17, 2023

Is your feature request related to a problem? Please describe.

I'm using Custom validation to compare a few sets of timeranges and want to return a detailed error message to the user so he knows which timeranges are causing problems. As the timeranges are based on calculations it's not ideal to split these up into seperate validators because then the calculations would need to be repeated every time. Hence the custom validation with .addFailure() messages works best.

Only if those validations succeed, do I want to continue checking a set of other rules. Checking these rules if the previous set fails would only cause confusion in the error list. For this this library has the .DependentRules option, but this option is not available when calling a .Custom() validation.

Describe the solution you'd like

this.RuleFor(o => o).Custom(CustomValidationMethod)
.DependentRules(() => 
{
  this.RuleFor(~~~~~~~~);
});

Describe alternatives you've considered

Also found documentation on using the When() syntax, but can't seem to get this to work either. I suppose I could add the dependentrules check at the end of the custom validator, but that would result into less readable code.

Additional Context

No response

@mythjuha
Copy link
Author

mythjuha commented Nov 17, 2023

Currently 'resolved' the issue by abusing the RootContextData. I add a key/value to it whenever i call 'AddFailure'. It would've been nice if we could just access the Failures property of the context.

When((o, context) =>
{
   CustomValidatorMethod(o, context);
   return !context.RootContextData.ContainsKey("HasFailures");
}, () =>
{
   this.RuleFor(~~~~~~~~);
});

@JeremySkinner
Copy link
Member

JeremySkinner commented Nov 18, 2023

Thanks for the suggestion - we can look at that in the future. For now, you can achieve the same result by using Must instead of Custom - Must can still call AddFailure internally, but just return true from the callback so it doesn't auto-generate a failure, and then chain on a condition as usual.

@JeremySkinner
Copy link
Member

JeremySkinner commented Nov 24, 2023

Added in 3775c94. Will be part of the 11.9 release.

@JeremySkinner
Copy link
Member

I had to revert this as it requires a breaking change, so isn't something that can be implemented in 11.x. Will re-target for 12.0 instead.

@JeremySkinner
Copy link
Member

Implemented in #2177, for 12.0

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants