In this scenario, the application of JSON Schema Validation policy to the Mock API created on Apinizer will be tested.


Assuming that the JSON body of the incoming requests in the scenario is the same, let's add it to All.


We add JSON Schema Validation as a policy.


The sample request body in Mock Api will be as follows:

{
  "Sorgula": {
    "kriterListesi": {
      "BilesikKutukSorgulaKimlikNoSorguKriteri": {
        "DogumAy": 5,
        "DogumGun": 21,
        "DogumYil": 1990,
        "KimlikNo": "12345678901"
      }
    }
  }
}


A suitable JSON Schema Validation includes the following:

  • DogumAy: A value between 1 and 12 can be sent.
  • DogumGun: A value between 1 and 31 can be sent.
  • DogumYil: A value between 1800 and 2100 can be sent.
  • KimlikNo: It must be a string (ID number) 11 characters long.


JSON Schema Validation to be added to the policy:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "Sorgula": {
      "type": "object",
      "properties": {
        "kriterListesi": {
          "type": "object",
          "properties": {
            "BilesikKutukSorgulaKimlikNoSorguKriteri": {
              "type": "object",
              "properties": {
                "DogumAy": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 12
                },
                "DogumGun": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 31
                },
                "DogumYil": {
                  "type": "integer",
                  "minimum": 1800,
                  "maximum": 2100
                },
                "KimlikNo": {
                  "type": "string",
                  "minLength": 11,
                  "maxLength": 11
                }
              },
              "required": ["DogumAy", "DogumGun", "DogumYil", "KimlikNo"]
            }
          },
          "required": ["BilesikKutukSorgulaKimlikNoSorguKriteri"]
        }
      },
      "required": ["kriterListesi"]
    }
  },
  "required": ["Sorgula"]
}


The Json Schema Validation content above is added to the “Schema” field in the image below and saved.


When we made a request from the test console, IDNo("kimlikNo") was entered as 12 characters and validation was performed. We received the warning 'It can only be 11 characters long'.