I have a model to validate:
```
public class BarcodeSearchCriteria
{
public string Barcode { get; set; }
public BarcodeType BarcodeType { get; set; } // enum
}
```
and a validator doing it:
```
public class BarcodeSearchCriteriaValidator : AbstractValidator<BarcodeSearchCriteria>
{
public BarcodeSearchCriteriaValidator()
{
CascadeMode = CascadeMode.StopOnFirstFailure;
RuleFor(x => x.Barcode).NotNull();
RuleFor(x => x.BarcodeType).NotEmpty();
RuleFor(x => x).
.Must(c =>
{
switch (c.BarcodeType)
{
case BarcodeType.EAN:
return c.Barcode.Length == 13;
case BarcodeType.UPC:
return c.Barcode.Length == 12;
default:
return false;
}
});
.WithName("BarcodeLength");
}
```
Why in case when `Barcode` is null it doesn't stop after the first rule failed but throws `NullReferenceException`?
Comments: Hi, I think you've misunderstood the purpose of CascadeMode. CascadeMode affects rules within the *same* chain, eg: RuleFor(x => x.Foo).NotNull().NotEqual("Bar") Setting CascadeMode to StopOnFirstFailure would mean that the NotEqual rule would not run if the NotNull fails. CascadeMode does not affect independent rules. [Please read this page in the documentation](https://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#Cascade)
```
public class BarcodeSearchCriteria
{
public string Barcode { get; set; }
public BarcodeType BarcodeType { get; set; } // enum
}
```
and a validator doing it:
```
public class BarcodeSearchCriteriaValidator : AbstractValidator<BarcodeSearchCriteria>
{
public BarcodeSearchCriteriaValidator()
{
CascadeMode = CascadeMode.StopOnFirstFailure;
RuleFor(x => x.Barcode).NotNull();
RuleFor(x => x.BarcodeType).NotEmpty();
RuleFor(x => x).
.Must(c =>
{
switch (c.BarcodeType)
{
case BarcodeType.EAN:
return c.Barcode.Length == 13;
case BarcodeType.UPC:
return c.Barcode.Length == 12;
default:
return false;
}
});
.WithName("BarcodeLength");
}
```
Why in case when `Barcode` is null it doesn't stop after the first rule failed but throws `NullReferenceException`?
Comments: Hi, I think you've misunderstood the purpose of CascadeMode. CascadeMode affects rules within the *same* chain, eg: RuleFor(x => x.Foo).NotNull().NotEqual("Bar") Setting CascadeMode to StopOnFirstFailure would mean that the NotEqual rule would not run if the NotNull fails. CascadeMode does not affect independent rules. [Please read this page in the documentation](https://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#Cascade)