asp.net mvc - FluentValidation collection properties not validated -


this first time i'm trying implement fluentvalidation since need cover complex validation scenario.

the class i'm trying validate has large quantity of properties, complex objects , several collections.

i didn't have troubles validate properties of main class or checking if collections not empty, have problems while validating objects properties within each collection.

to implement followed examples documented here (check under "re-using validators collections"): http://fluentvalidation.codeplex.com/wikipage?title=creatingavalidator

these model classes (reduced improve readability)

public class caso { public int id { get; set; }  public string descripcion { get; set; } public list<medicamento> medicamentos { get; set; } }  public class medicamento { public int id { get; set; } public string nombre { get; set; } } 

these validator classes:

public class casovalidator : abstractvalidator<casoadverso> {     public casovalidator()     {         ruleset("iniciar", () =>         {             // validated ok             rulefor(x => x.descripcion).notempty();             // validated ok             rulefor(x => x.medicamentos).must(x => x != null && x.count > 0).withmessage("no puede iniciar un caso sin medicamentos cargados");             rulefor(x => x.medicamentos).setcollectionvalidator(new medicamentovalidator());         });     } }  public class medicamentovalidator : abstractvalidator<medicamento> {     public medicamentovalidator()     {         // not validated. if object property empty error message doesn't appear. checked using "notnull" , "notempty" clauses         rulefor(x => x.nombre).notnull().withmessage("debe especificar un nombre");      } } 

(note: i'm using ruleset because of different validation schemas dependent of document status in workflow)

i'm executing validation manually controller (no mvc integration)

[httppost] public actionresult iniciar(caso c) {     casovalidator validator = new casovalidator();     fluentvalidation.results.validationresult validate = validator.validate(c, ruleset: "iniciar");        // ... } 

with implementation properties of main class validated fine need validate each property of "medicamento" class within collection.

could missing here?. should validated using ruleforeach clause available?

any appreciated.

it appears ruleset setting applying child validator primary one.

i tested code in xunit.net test, , confirmed it.

if change rulesets execute should find works expected:

casovalidator validator = new casovalidator(); fluentvalidation.results.validationresult validate = validator.validate(c, ruleset: "default,iniciar"); 

the 'default' ruleset work on medicamentovalidator rules.

i didn't find in documentation, through testing.

this sample unit test:

[fact] public void test1() {      caso c = new caso()     {         id = 1,         descripcion = "none",         medicamentos = new list<medicamento>()     };      c.medicamentos.add(new medicamento()     {         id = 0,         nombre= null     });      casovalidator validator = new casovalidator();     fluentvalidation.results.validationresult validate = validator.validate(c, ruleset: "default,iniciar");      assert.notempty(validate.errors); } 

update: found reference jeremy skinner behavior: http://fluentvalidation.codeplex.com/discussions/266920

rulesets cascade child validators, whichever ruleset selected use top-level validator used child validator. if ran "minimal" ruleset on createprofilemodelvalidator, rules in "minimal" ruleset run on both createprofilemodelvalidator , profilevalidator.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -