Working on Validation Error Response

In this lesson, we're going to format our validation error response

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:13] We are almost done shaping our errors just left with this validation error. As you can see it has an additional errors object which carries the key value pairs of the property and the error message.

    [00:14 - 00:30] This can obviously be more than one and will be generated when there is some property which is received empty or is an incorrect format. What we want to do is include an errors property which will simply send the error message in form of an array rather than objects.

    [00:31 - 00:51] Let's see how to do it. First of all let's open the project and inside error response let's create another class and we will call it API validation error response.

    [00:52 - 01:04] This will derive from our API response. Let's create a new constructor.

    [01:05 - 01:16] We are not going to set anything while initializing it. So we can take this off and we will just pass the status code which we know should be 400.

    [01:17 - 01:38] So we'll simply pass 400 here. Since we are going to send an array of string as a response we can create a new property which will be i enumerable of type string and we will call it errors.

    [01:39 - 01:53] Let's import it using system.collection.generic. For API controller which is here this is responsible for showing us the error in this particular format.

    [01:54 - 02:04] To tweak the error response we will have to override this behavior of our API controller. So for this we'll have to go to our startup class.

    [02:05 - 02:13] Here it is very important to keep this one below the services dot add controller. So what we can do is we can keep it in the end.

    [02:14 - 02:37] So now we will write services dot configure. Inside angle brackets we will mention the type of service which is API behavior options.

    [02:38 - 02:50] And import it using Microsoft dot ASP netcode.mbc. Inside we can tweak the options so I will write options.

    [02:51 - 03:30] Now inside I will write options dot invalid model state response factory and here I will write action context which has all these errors. So now let's coincide again and write where errors is equal to action context dot we can make it small action.

    [03:31 - 03:41] Now action context dot model state. Now this model state is of type dictionary.

    [03:42 - 03:57] We want to go inside this model state and extract the errors if they are available and shape it as per our requirements. So firstly we will check if there are any errors by using the where statement.

    [03:58 - 04:35] We can import where using system dot link and inside we will check if errors and errors dot value dot errors dot count is more than zero. After this we will select all these errors by using select many and I will pick it using x dot value dot errors.

    [04:36 - 05:00] From this we will simply extract the error message by using select and I will extract all the error messages by writing x dot error message and finally convert it into an array because we want to show arrays. Now I will use semicolon to end this.

    [05:01 - 05:14] Now we want to pass this errors variable to our API validation error response. So how we will do it?

    [05:15 - 05:41] And this one will be equal to new API validation error response. And here I will pass the errors which is the property we have created to this particular errors.

    [05:42 - 05:58] And let's import it using API dot error response. Finally we will return new bad request object result and we will simply pass the error response.

    [05:59 - 06:06] And that's it. We can now go to postman and run this command again.

    [06:07 - 06:19] And as you can see we see an array of errors with a string as an error message. Right now we just see one but once we have more of them we will see them inside an array of errors.

    [06:20 - 06:28] So this one is sorted as well. Now all our responses are well formatted if we look at our not found response.

    [06:29 - 06:35] We have status code in the error message. Our server error has details and status code in error message.

    [06:36 - 06:43] Our bad request has been well formatted as well. The bad request has been settled as well.

    [06:44 - 06:53] And also the unknown endpoint is giving us formatted errors. Now this will help us consume them in more efficient way while working on front end.