Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: CascadeMode set inside Validator .ctor is broken

$
0
0
Hi

I think you've misunderstood the use of the cascade mode. This settings affects validators *in the same chain*, not separate RuleFor statements. For example:

RuleFor(x=>x.Foo).NotEmpty().NotEqual(1);

If you set the cascade mode to stoponfirstfailure here ten the NotEqual rule will not run if the NotEmpty fails. Rules declared in separate RuleFor statements are completely separate and are always invoked.

Hope this helps.

Jeremy Skinner
Sent from my iPhone

On 12 Feb 2012, at 05:41, bchavez <notifications@codeplex.com> wrote:

From: bchavez

Hi,

I have the following validator:

    public class ResponseValidator : AbstractValidator<Response>
    {
        public ResponseValidator()
        {
            CascadeMode = CascadeMode.StopOnFirstFailure;

            RuleFor( cr => cr.Result )
                .Must( r => r == RequestResult.Success )
                .WithMessage( "The request failed." );

            RuleFor( r => r.CheckoutId ).NotEmpty()
                .WithName( "CheckoutResponse.CheckoutId" );
        }
    }

Notice I set the CascadeMode = CascaseMode.StopOnFirstFailure.

Next, I validate a response by:

var result = validator.Validate( new Response {Result = RequestResult.Failure, Message = "Invalid request!"} );
    result.IsValid.ShouldBeFalse(); //OK
    result.Errors.Count.ShouldEqual( 1 ); //Count = 2 !!!

If I'm not mistaken, Cascase.StopOnFirstFailure should return a Errors.Count of 1.

Let me know your thoughts.

Thanks,
Brian


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>