Valit icon indicating copy to clipboard operation
Valit copied to clipboard

For(@object) throws exception when null

Open brunomartinspro opened this issue 6 years ago • 0 comments

The for option should have a handled output with an error message, optionally a custom error message would be awesome.

   void ValidateModel(RegisterModel model)
        {
            //Force exception
            model = null;
            var result = ValitRules<RegisterModel>
                .Create()
                .Ensure(m => m.Email, _=>_
                    .Required()
                    .Email())
                .Ensure(m => m.Password, _=>_ 
                    .Required()
                    .MinLength(10))
                .Ensure(m => m.Age, _=>_
                    .IsGreaterThan(16))
                .For(model)
                .Validate();

            if(result.Succeeded)
            {
                // do something on success
            }
            else 
            {
                // do something on failure
            }
        }

brunomartinspro avatar Jan 02 '20 17:01 brunomartinspro