I have the following class
public class ValidProjectHeader : AbstractValidator<Projects.ProjectHeader>
{
public ValidProjectHeader()
{
RuleFor(x => x.LobId).Must(ValidateLOBIDExists);
RuleFor(x => x.CreatedByUserId).NotEmpty();
RuleFor(x => x.ProjectManagerId).NotEmpty();
RuleFor(x => x.ProjectName).NotEmpty();
RuleFor(x => x.SalesRepId).NotEmpty();
RuleFor(x => x.DeliveryDate).NotEmpty();
RuleFor(x => x.ProjectStatusId).NotEmpty();
RuleFor(x => x.DeptartmentId).NotEmpty();
RuleFor(x => x.CustomerId).NotEmpty();
RuleSet("Insert", () =>
{
RuleFor(x => x.ProjectLines).Must(ValidateProjectLines).SetCollectionValidator(new ValidProjectLine());
});
RuleSet("Update", () =>
{
RuleFor(x => x.ProjectLines).SetCollectionValidator(new ValidProjectLine());
});
}
and what i am trying to do is call the validation with the rulset but i also want to return the "common" rules when i call the validation with the RuleSet.
the Code I have for calling the validation is as follows
public abstract class BaseValidator
{
private List<ValidationFailure> _errors;
public bool IsValid { get; protected set; }
public List<ValidationFailure> Errors
{
get { return _errors; }
protected set { _errors = value; }
}
public virtual bool CallValidation()
{
Errors = new List<ValidationFailure>();
ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute;
IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator;
FluentValidation.Results.ValidationResult result = validator.Validate(this);
IsValid = result.IsValid;
Errors = result.Errors.ToList();
return result.IsValid;
}
public virtual bool CallValidation(string ruleSet)
{
Errors = new List<ValidationFailure>();
ValidatorAttribute val = this.GetType().GetCustomAttributes(typeof(ValidatorAttribute), true)[0] as ValidatorAttribute;
IValidator validator = Activator.CreateInstance(val.ValidatorType) as IValidator;
FluentValidation.Results.ValidationResult result = validator.Validate(new FluentValidation.ValidationContext(this, new PropertyChain(), new RulesetValidatorSelector(ruleSet)));
IsValid = result.IsValid;
Errors = result.Errors.ToList();
return result.IsValid;
}
public BaseValidator()
{
Errors = new List<ValidationFailure>();
}
}
I can call the Method CallValidation
with the member ruleSet
but it is not calling the "common" rules also.
I know I can create a "Common" RuleSet for running these rules but in that case i would have to call the validation with the Common RuleSet always.
Is there any way I can call the RuleSet and also call the common rules.
This would execute rules in the MyRuleSet set, and those rules not in any ruleset. Note that you shouldn’t create your own ruleset called “default”, as FluentValidation will treat these rules as not being in a ruleset.
Introducing FluentValidation FluentValidation is an open source validation library for.NET. It supports a fluent API, and leverages lambda expressions to build validation rules. The current stable version (6.4.1) supports both.NET Framework 4.5+ and.NET Standard 1.0.
For example, let’s imagine we have 3 properties on a Person object (Id, Surname and Forename) and have a validation rule for each. We could group the Surname and Forename rules together in a “Names” RuleSet:
services.AddMvc ().AddFluentValidation (fv => fv.RegisterValidatorsFromAssemblyContaining<Startup> ()); To create validation rules for a class using Fluent Validation, create a separate class that extends the FluentValidation.AbstractValidator<T> class, where T is the class where you want to apply the validation rules.
Instead you could do this:
using FluentValidation;
...
FluentValidation.Results.ValidationResult resultCommon =
validator.Validate(parameter, ruleSet: "default, Insert");
The using
directive is required to bring the Validate()
extension method from DefaultValidatorExtensions
into scope, which has the ruleSet
property. Otherwise you will only have the Validate()
method available from inheriting AbstractValidator<T>
, which doesn't have a ruleSet
argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With