4
   {
  "status": 200,
  "id": "123e4567-e89b-12d3-a456-426655440000",
  "shop": {
    "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd": {
      "123e4567-e89b-12d3-a456-426655443210": {
        "quantity": {
          "value": 10
        }
      },
      "123e4567-e89b-12d3-a456-426655443211": {
        "quantity": {
          "value": 20
        }
      }
    }
  }
}

This is my json response. I want to validate the fields "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd" , "123e4567-e89b-12d3-a456-426655443210" and "123e4567-e89b-12d3-a456-426655443211", which are uniquely generated every time whenever hits the endpoint.

behrad
  • 1,228
  • 14
  • 21
Anuja
  • 41
  • 1
  • 4
  • Is shop an array on an object? if the shop is an object it has just 2 property or it is dynamic too? – behrad Feb 18 '22 at 09:11
  • @behrad it is like shop having "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd" as a shopId , "123e4567-e89b-12d3-a456-426655443210" and "123e4567-e89b-12d3-a456-426655443211" are the variants of shopId. shop is an object having only one shopId but shopId has variants as many as we want. – Anuja Feb 18 '22 at 09:26

3 Answers3

1

Building on @pxcv7r's answer:

To validate UUID in particular, you may use format in JSON schema, which provides built-in support for the UUID syntax: { "type": "string", "format": "uuid" }

See https://json-schema.org/understanding-json-schema/reference/string.html

Additionally, you can use a combination of "propertyNames" and "unevaluatedProperties" to avoid the need for any regular expression:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "status": {
      "type": "integer"
    },
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "shop": {
      "type": "object",
      "minProperties": 1,
      "maxProperties": 1,
      "propertyNames": {
        "format": "uuid"
      },
      "unevaluatedProperties": {
        "type":"object",
        "minProperties": 1,
        "propertyNames": {
          "format": "uuid"
        },
        "unevaluatedProperties": {
          "title": "single variant of a shop",
          "type": "object",
          "properties": {
            "quantity": {
              "type": "object",
              "properties": {
                "value": {
                  "type": "integer"
                }
              }
            }
          }
        }
      }
    }
  }
}
Carsten
  • 2,047
  • 1
  • 21
  • 46
0

To validate in JSON schema that a string conforms to a regular expression pattern use { "type": "string", "pattern": "\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b" }

The concrete pattern is adapted from the question Searching for UUIDs in text with regex see there for more details.

To validate UUID in particular, you may use format in JSON schema, which provides built-in support for the UUID syntax: { "type": "string", "format": "uuid" }

See https://json-schema.org/understanding-json-schema/reference/string.html

pxcv7r
  • 478
  • 5
  • 14
  • { "$schema": "http://json-schema.org/draft-07/schema", "type": "object", "required": [ "status", "id", "shop" ], "properties": { "status": { "type": "integer" }, "id": { "type": "string" }, "shop": { "type": "object", "required": [ "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd" ], "properties": {"c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd": { .. instead of "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd" what code should i place in required[] and in properties ? – Anuja Feb 18 '22 at 09:46
0

You need "patternProperties":

{ 
  "$schema":"http://json-schema.org/draft-07/schema#",
  "type":"object",
  "properties": {
    "shop":{
      "type":"object",
      "additionalProperties":false,
      "patternProperties":{
        "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}": {
          "type":"object",
          "patternProperties" :{
            "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}":{
              "type":"object",
               "properties":{
                 "quantity":{
                   "type":"object",
                   "properties":{
                     "value":{
                       "type":"integer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Viewes in JSONBuddy

Clemens
  • 1,744
  • 11
  • 20