question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidationFailure should be aware of it's RuleSet

See original GitHub issue

Is your feature request related to a problem? Please describe. When a ValidationFailure is constructed, it should be aware of the RuleSet name that it came from.

Describe the solution you’d like Please add the following to ValidationFailure

public string RuleSet { get; }

Describe alternatives you’ve considered I’ve attempted to foreach all of my rulesets, but unfortunately I don’t even see the validation failure when I do this.

var failures = new Dictionary<string, ValidationResult>();

foreach (var ruleSet in _ruleSets)
{
    var result = await _validator.ValidateAsync(self, opts => opts.IncludeRuleSets(ruleSet));
    failures[ruleSet] = result; // unfortunately this is always IsValid = true;
    isValid = isValid && result.Errors.All(x => x.Severity != Severity.Error);
}

Additional context Effectively I need to emit out validation failures based on the grouping of validation. In other words, a particular segment of my overall object can be valid or invalid based on a RuleSet.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
JeremySkinnercommented, Aug 27, 2020

@ChaseFlorell Yes that’s correct, custom state is always per-validator, it’s not shared between the whole rule chain. However you can add an extension method that would apply it the whole chain quite easily:

public static IRuleBuilderOptions<T, TProperty> RuleState<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, Func<T, object> stateProvider) {
	var wrapper = new Func<PropertyValidatorContext, object>(ctx => stateProvider((T) ctx.InstanceToValidate));
	return rule.Configure(config => {
		foreach (var validator in config.Validators) {
			validator.Options.CustomStateProvider = wrapper;
		}
	});
}

Then you can call:

RuleFor(x => x.Email).EmailAddress().NotNull().RuleState(_ => "Email");

It must be the last method in the chain, as it’ll only apply to any validators that precede it.

2reactions
JeremySkinnercommented, Aug 25, 2020

An individual rule can actually be part of multiple rulesets, so this will probably be an array of strings rather than a single string.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set a message for a RuleSet fails in FluentValidation
SIMPLE SOLUTION. It is impossible to set error message to RuleSet directly without changing FluentValidation sources, but you can collect ...
Read more >
Setting the Severity Level
By default, if these rules fail they will have a severity of Error . This can be changed by calling the WithSeverity method....
Read more >
Rule Set prerequisite based validation
Rule Set prerequisite based validation ... This example uses a mixture of AV mode and Rule Validation (RV) mode. A ruleset name with...
Read more >
Validating data quality in AWS Glue DataBrew
A validation failure for that ruleset might mean that the data isn't acceptable for further use. An example is missing values in key...
Read more >
How to use FluentValidation in ASP.NET Core Apps (.Net 6)
FluentValidation is a popular .NET library for building strongly-typed validation rules in applications. Learn how to use FluentValidation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found