question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation not working for nested list of models

See original GitHub issue

Which version of FluentValidation are you using? 6.1.0.1

Which version of ASP.NET are you using? .NET Framework 4.6.1

Describe the issue that you’re having Issue The list of objects inside the model are not getting validated.

Sample classes

public class User {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Role> Roles { get; set; }
}
public class Role {
    public string Name { get; set; }
    public string Type { get; set; }
}
public class RoleTypeInteger : Role {
    public int Value { get; set; }
}
public class RoleTypeString : Role {
    public string Value { get; set; }
}

Validator classes

public class UserValidator : AbstractValidator<User>
{
    public UserValidator()
    {
        RuleFor(x => x.Name).NotNull();
    }
}
public class RoleTypeIntegerValidator: AbstractValidator<RoleTypeInteger>
{
     public RoleTypeIntegerValidator()
     {
         RuleFor(x => x.Value).InclusiveBetween(10, 30);
     }
}
public class RoleTypeStringValidator: AbstractValidator<RoleTypeString>
{
     public RoleTypeStringValidator()
     {
         RuleFor(x => x.Value).Length(128);
     }
}

MVC Controller

public ActionResult MyMethod([ModelBinder(typeof(UserModelBinder))] User viewModel) {...}

Now in my model binder, I convert the received roles to their specific models based on the type property.

Issue The model state is valid even when the validation error should be present.

Ruled out issues Earlier I assumed that there might have been an issue with model binder or polymorphic models, but I tried updating the User model with the following code, where I was only trying to test one type of model, but it still doesn’t work.

public class User {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<RoleTypeInteger> Roles { get; set; }
}

Please suggest what am I missing here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jul 8, 2020

If you go with the third option, then your custom validator factory can be based off default implementation (you’d need to add methods for removing items from the ConcurrentDictionary).

You would then wire it up in your application startup routine, at the point you call FluentValidationModelValidatorProvider.Configure(), eg:

FluentValidationModelValidatorProvider.Configure(fv => {
  fv.ValidatorFactory = new MyCustomValidatorFactory();
});

However, personally I would suggest you go with the second option and keep your logic for loading/caching messages separate to FluentValidation’s validator lifetimes.

1reaction
JeremySkinnercommented, Jul 4, 2020

That’s a limitation of 6.x (RuleFor could only be used for simple properties, not method calls) Modern versions don’t have this limitation, but you will still bypass the expression cache.

The second solution in that link (as well as the second issue I linked to) are more likely to work on 6.x. But I’d still suggest you upgrade to 8.6 if you can.

Read more comments on GitHub >

github_iconTop Results From Across the Web

validation is not showing for nested model
I have the following Model: public partial class EmployeeInfo { public int EmployeeInfoId { get; set; } [Required] [DisplayName("Last ...
Read more >
Javax validation on nested objects - not working
Just try adding @valid to collection. it would be working as per reference hibernate @Getter @Setter @Valid @NotNull(groups ...
Read more >
Pydantic 1.6 does not validate nested models when using ...
Bug. I'm aware that validation does not apply to default values, but it appears that it is no longer applied to user-provided values...
Read more >
How do I validate a nested complex model in Blazor?
To validate the nested complex model, replace the DataAnnotationsValidator with the ObjectGraphDataAnnotationsValidator, which validates the entire object, ...
Read more >
Body - Nested Models - FastAPI
Body - Nested Models¶. With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic). List fields ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found