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.

When(bool) not working with SetValidator on property

See original GitHub issue

Issue 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
emanu11commented, Oct 25, 2018

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)

  FluentValidationModelValidatorProvider.Configure(p =>
                {
                    p.ValidatorFactory = new ModelValidatorFactory(typeof(ModelValidators.Namespace));
                });

Thanaks for your help!

0reactions
JeremySkinnercommented, Oct 25, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

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