Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging fluent validation rules

The problem

I'm struggling to make my Fluent Validation RuleSet work, currently it doesn't, and I don't have any idea why is that happening, everything seems all right. I would like to somehow step into the code that performs the validation itself, but RuleSet lambdas are ExpressionTrees which doesn't provide even poor debugging experience.

The question

Is there a way to debug RuleSet logic to see what's happening inside RuleSets?

like image 430
Lu4 Avatar asked Jul 20 '13 23:07

Lu4


People also ask

How do you debug a validation rule?

You can use debug log to debug your code. Goto Setup--> Administration Setup--> Monitoring-->Debug logs--> Click on New Button ---> click on lookup icon--> select logged in user from list -->click save. Now execute your code and go to that debug log page, you would find a entry log entry against your execution.

How do you write a test case for fluent validation?

Using the Code The program does the following basic steps: Creating a new Item object and setting the properties on that object. Creating an instance of the ItemValidator class to validate the properties on the object instance. Validating the values on the instance of the Item object.

Is fluent validation good?

FluentValidation provides a great alternative to Data Annotations in order to validate models. It gives better control of validation rules and makes validation rules easy to read, easy to test, and enable great separation of concerns.

What is fluent validation?

Fluent Validation is a validation library for . NET, used for building strongly typed validation rules for business objects. Fluent validation is one way of setting up dedicated validator objects, that you would use when you want to treat validation logic as separate from business logic.


2 Answers

FluentValidation is open source, so theoretically you could download the code from the repo at https://github.com/JeremySkinner/FluentValidation and then load up the solution, reference it directly, then step through.

Hopefully this will get you where you need to be, but I'm sure someone here could help if you provided your rules and maybe some unit tests that show the failures.

like image 120
Christian Duvall Avatar answered Sep 28 '22 06:09

Christian Duvall


The current version of FluentValidation allows you to unit test your validators by using the TestValidate extension method. There is also an async version available.

Calling this method, passing an object to validate, will return a TestValidationResult<> object which has 2 useful methods ShouldHaveValidationErrorFor and ShouldNotHaveValidationErrorFor that can be used to check if tests passed or failed.

The documentation can be found here https://docs.fluentvalidation.net/en/latest/testing.html

like image 39
DeanOC Avatar answered Sep 28 '22 05:09

DeanOC