1

I am creating an asp.net core web api application. Where I am try to validate my models using fluent validation.

this is my model and validator.

public class Data
{
    public string Name { get; set; }

    public int Age { get; set; }
}

public class DataValidator : AbstractValidator<Data>
{
    public DataValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty()
            .MaximumLength(5);

        RuleFor(x => x.Age)
            .LessThan(80);
    }
}

Everything works fine. Fluent validation returns all the validations together except following case.

When my request contains following JSON, Fluent Validation doesn't get hit. Asp.net core model validation is take place. in that case I am getting single validation error.

{
    "name": 123,
    "Age" : 100
}

I got following validation message.

The JSON value could not be converted to System.String. Path

  1. How to override above default message?
  2. Is there any way to handle above validation in Fluent Validation?
  3. I want both 'name' and 'age' validation messages together.
6
  • 1
    In that JSON, name is not a string. It's probably not even hitting ANY validation middleware, it'll be failing trying to deserialise to your model. Wrap the name value in double quotes. Commented Jun 13, 2020 at 11:02
  • @rgvlee Can I override that deserialise mechanism? Because I want to throw data type mismatch validation error. Commented Jun 24, 2020 at 6:07
  • Yes; you'd have to determine exactly what middleware component is causing the issue (stack trace should give you that). A custom model binder may allow you to do it. There is a related question here stackoverflow.com/a/57003369/2975810 that covers another methodology. Commented Jun 24, 2020 at 6:19
  • stackoverflow.com/a/59728990/2975810 Commented Jun 24, 2020 at 6:22
  • stackoverflow.com/a/52470911/2975810 Commented Jun 24, 2020 at 6:22

1 Answer 1

0

Let's look at your binding model:

public class Data
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Here, Name is a string while Age is an int.

Your validator is just fine, otherwise you should get a compilation error when you build the app.

Now, let's look at the JSON:

{
    "name": 123,
    "Age" : 100
}

Instead of using name, you should use Name. Plus, the value of Name should be a string, i.e. "123" instead of 123. i.e.

{
  "Name": "123",
  "Age": 100
}

After that, you should be able to get the expected validation errors.

Sign up to request clarification or add additional context in comments.

3 Comments

The key casing likely won't matter, default behaviour will happily bind regardless of whether it's 'name' or 'Name' in the JSON. I agree with the value for Name being the problem, in that it needs to be changed to a string - as I pointed out in the OP comments yesterday.
Thank you for your answer. As @rgvlee said there is no issue in name and Name. I want to notify user that name is a string but you have set an int.
@VimeshShah you can do this checking using javascript, if isNaN(parseInt(name)) === false that means user input a number. In this case you notify user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.