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.

[Question] Combining FluentValidation and IValidatableObject Errors

See original GitHub issue

Is it possible to run both FluentValidation and IValidatableObject in the same ViewModel? For example if I had the following ViewModel:

public class TestViewModel
{
    public Foo Foo { get; set; }
    public Bar Bar { get; set; }
}

public class Foo : IValidatableObject
{
    public string FooName { get; set; }

    public IEnumerable<ValidationResult> Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext)
    {
        if (string.IsNullOrEmpty(FooName))
        {
            yield return new ValidationResult("Required from IValidatableObject", new[] { "FooName" });
        }
    }
}

[Validator(typeof(BarValidator))]
public class Bar
{
    public string BarName { get; set; }
}

public class BarValidator : AbstractValidator<Bar>
{
    public BarValidator() {
        RuleFor(x => x.BarName).NotNull().WithMessage("Required from FluentValidation");
    }
}

Is there a way both Foo and Bar validations can run and return the results when my Controller calls ModelState.IsValid?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Aug 19, 2017

I’ve just pushed out 7.2-beta1 with initial support for IValidatableObject. Please feel free to give it a try. Note that this only supports top-level objects, but not child properties

1reaction
JeremySkinnercommented, Aug 16, 2017

It’s something I’d like to enable support for in the future in aspnetcore, but there are a lot of implications for doing this and requires a lot of work, so it’s not something that’ll be added soon I’m afraid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Combining FluentValidation and IValidatableObject Errors
Is there a way both Foo and Bar validations can run and return the results when my Controller calls ModelState.IsValid ? c# ·...
Read more >
Combining ASP.NET Core validation attributes with Value ...
I want to aggregate a collection of Errors both in your Result class and have the API return them when more than one...
Read more >
Using Fluent Validation in ASP.NET Core
With the implementation of Data Annotations in . NET Classes, the problem is that there will be a lot of duplication lines of...
Read more >
Solving .NET Validation Challenges with FluentValidation
In this hands-on demonstration, you'll learn how to validate ASP.NET code by implementing an ASP.NET web API with FluentValidation.
Read more >
how to implement validation in .net core 6 and entity ...
Fluent validation supports async validation while the built in pipeline always runs synchronous. This allows you to combine all validation ...
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