0

I'm using C# & Restsharp in order to create HTTP calls to one of our APIs. I'm struggling with the Response retrieved by the API. I'd like to validate the model structure but didn't find any decent way of doing that on my side (handling the response).

For instance, I have a DTO object as the following:

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
}

Sometimes, the Response object can include only the FirstName, without having the LastName and PhoneNumber at all and sometimes it might include more properties (Country, PostalCode, etc...). I should validate that the User model only includes the above (FirstName, LastName and PhoneNumber). Nothing more, and nothing that is missing.

What's the best approach to doing that?

JumpIntoTheWater
  • 1,306
  • 2
  • 19
  • 46
  • Hi! you can either use a validation in the controller or run validation during the call pipe. Does this help? https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0 or https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validationattribute?view=net-6.0 – MC LinkTimeError Feb 27 '22 at 12:55
  • @MCLinkTimeError Actually no. Since I'm only using the API and don't have access to it. I'm handling the response. – JumpIntoTheWater Feb 28 '22 at 12:29
  • Why is this question tagged [tag:selenium]? I don't see any references to browser automation here. – Greg Burghardt Feb 28 '22 at 13:18

1 Answers1

0

Take a look at the fluent validation, you can create roles for your DTO´s and verify them.

Fluent Validation Link