0

I've created an ASP.NET Core SPA application using React as my frontend. I'm trying to display all validation errors, but it seems that only the first error is returned and I don't know why.

Here is my DTO class that contains validation attributes:

public class ProductCreateDto
{
    [Display(Name = "Name")]
    [MinLength(10)]
    public string Name { get; set; }
    [Display(Name = "Description")]
    [MinLength(10)]
    public string Description { get; set; }
    [Display(Name = "Price")]
    public double Price { get; set; }
}

This is the data that is sent to server:

{
    "name": "aav",
    "description": "aa",
    "price": ""
}

This is the response from server:

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-cc641ad6d94b3947abf9425292a70071-a22e2a3181071947-00",
"errors": {
    "$.price": [
        "The JSON value could not be converted to System.Double. Path: $.price | LineNumber: 3 | BytePositionInLine: 15."
    ]
}

}

As you can see, the errors object does not contains name and description validation error messages.

2
  • errors is an object, it's property $.price is an array Commented Feb 24, 2021 at 19:01
  • did you solved this issue? Commented Dec 8, 2021 at 13:03

1 Answer 1

1

The error is not a validation error but a JSON Parsing error. The $.price is actually the JSON path it fails to parse into the field. If the Price member was nullable it would have been parsed. And if the price is required for the Model add the [Required] attribute to it. I think this is a side effect of the new Microsoft JSON parser.

So I guess your DTO would look something like this.

public class ProductCreateDto
{
    [Display(Name = "Name")]
    [MinLength(10)]
    public string Name { get; set; }
    [Display(Name = "Description")]
    [MinLength(10)]
    public string Description { get; set; }
    [Display(Name = "Price")]
    [Required]
    public double? Price { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

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.