When(bool) not working with SetValidator on property
See original GitHub issueIssue Description
I have the class EventViewModel that has a complex property SeriesScheduleViewModel
public class EventViewModel
{
public string Name {get;set;}
public string LocationName{get;set;}
public bool IsSeriesEvent {get;set;}
public SeriesScheduleViewModel SeriesSchedule {get;set;}
...
}
SeriesScheduleViewModel class contains a colection of SeriesViewModel :
public class SeriesScheduleViewModel
{
public IList<SeriesViewModel> Series { get; set; }
public string TimeZoneId { get; set; }
public IList<Range<DateTime>> ExtraDates { get; set; }
public IList<DateTime> ExceptDates { get; set; }
}
And the SeriesViewModel looks as it follows:
public class SeriesViewModel
{
public SeriesType SeriesType { get; set; }
public int DailyInterval { get; set; }
public int WeeklyInterval { get; set; }
public string SelectedDaysOfWeek { get; set; }
....
}
Validator classes:
public class EventValidator: AbstractValidator<EventViewModel>
{
public EventUpdateValidator()
{
RuleFor(x => x.SeriesSchedule).SetValidator(new SeriesScheduleValidator()).When(x => x.IsSeriesEvent);
.....
}
}
The validation for the Series property of SeriesSchedule is made with SetCollectionValidator:
public class SeriesScheduleValidator : AbstractValidator<SeriesScheduleViewModel>
{
public SeriesScheduleValidator()
{
RuleFor(x => x.TimeZoneId).NotEmpty();
RuleFor(x => x.Series).SetCollectionValidator(new SeriesValidator());
......
}
}
public class SeriesValidator : AbstractValidator<SeriesViewModel>
{
public SeriesValidator()
{
RuleFor(m => m.DailyInterval).NotEmpty().When(m => m.SeriesType == SeriesType.Daily);
RuleFor(m => m.WeeklyInterval).NotEmpty().When(m => m.SeriesType == SeriesType.Weekly);
...
}
}
The problem that I have is that the SeriesSchedule and it’s complex properties validation gets executued regardless of whether the IsSeriesEvent is set to true or false;
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
FluentValidation: how to make bool as required field with ' ...
I have this rule that IsDefault boolean property should be not null. The problem is when client do not pass this field when...
Read more >How to validate `bool` · Issue #142 · go-playground/validator
I just want to validate that the property exists -- if it exists, I know it will have a value of true or...
Read more >Custom Validators — FluentValidation documentation
The method should return a boolean indicating whether validation was successful. The generic type parameters on the base class represent the root instance...
Read more >Angular Custom Form Validators: Complete Guide
All about custom form validators, including synchronous and asynchronous, field-level, form-level, for both template-driven and reactive forms.
Read more >Validation | NestJS - A progressive Node.js framework
whitelist, boolean, If set to true, validator will strip validated (returned) object of any properties that do not use any validation decorators.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Uhh, yea. After looking again in the whole application, I just realized that I am using the IoC container integration that has all viewModels registered there. I said that there were none of the supported ways for wiring up the ViewModel to the validator because I couldn’t find any reference to the EventViewModel (such a childish mistake)
Thanaks for your help!
How are you wiring up the model to the validator for automatic integration at the moment? The only 2 supported ways are either with the ValidatorAttribute or with an IoC container. You must be using one of these approaches if it’s running at the moment.