Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently testing APIs and I want create a generic validator that will validate the json properties and its values to the Model.

For example I have the following Json response from the server
C#
{
   "id" : 1,
   "name" : "John",
   "age" : 30
 
}

//I created a Model DTO for this to compare it to json I get back.
public class UserDTO
{
    public int id { get; set; }
    public string name{ get; set; }
    public int age { get; set; }

     public static UserDTO Userinfo()
     {
            return new UserDTO()
           {
               id = 1,
               name = "John",
               age = 30
           };

     }

}


What I have tried:

//So I am trying to do here is create a class that will take any Model type class and then have a validate method where it takes the IRestresponse(using Restsharp) and convert to objects, get the properties and values. Then compare that to the Model class.


C#
public class PropertiesValidator<TModel> where TModel : class
  {
      public void Validate(IRestResponse response, TModel tmodelObj)
      {
          var content = response.Content;
          object acutalObjects = JsonConvert.DeserializeObject(content);

          //Getting Type of Generic class Model properties
          PropertyInfo[] properties = tmodelObj.GetType().GetProperties();
          foreach (PropertyInfo modelProperty in properties)
          {
              PropertyInfo currentExpectedProperty = tmodelObj.GetType().GetProperty(modelProperty.Name);
              string exceptionMessage =
                  string.Format("The property {0} of class {1} was not as expected.", modelProperty.Name, modelProperty.DeclaringType.Name);

              Assert.AreEqual(currentExpectedProperty.GetValue(tmodelObj, null), modelProperty.GetValue(acutalObjects, null), exceptionMessage);
          }
      }
Posted
Updated 25-Jun-20 11:20am
v3

1 solution

This is not valid JSON
JavaScript
{
   id = 1,
   name = "John",
   age = 30
}
As the names (id, name, age) are all required to be wrapped in quotes as they are strings.
RFC 4627
2.2. Objects

An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value. A single comma separates a value from a following
name. The names within an object SHOULD be unique.

object = begin-object [ member *( value-separator member ) ]
end-object

member = string name-separator value

2.5. Strings

The representation of strings is similar to conventions used in the C
family of programming languages. A string begins and ends with
quotation marks. All Unicode characters may be placed within the
quotation marks except for the characters that must be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F).
Reference:
RFC 4627: The application/json Media Type for JavaScript Object Notation (JSON)[^]
 
Share this answer
 
Comments
Member 14779968 25-Jun-20 17:03pm    
Yes that was my mistake. I fixed the Json.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900