0

I'd like to validate a json object with the json-schema, but that json object can duplicate its values ​​as many times as the user wants.

The keys of that object can be repeated as many times as the user wishes at the times the user create his json.

example 1: (collection with object)

{ 
  "info":
  [
  { 
    "name":  "aaron",
    "email": "aaron.com"
  }
  ]
}

JSON-SCHEMA of Example 1

   {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "email": {
          "type": "string"
        }
      },
      "required": [
        "name",
        "email"
      ]
    }

example 2: (collection with 2 object)

{ 
  "info":
  [
  { 
    "name":  "aaron",
    "email": "aaron.com"
  },

  { 
    "name":  "misa",
    "email": "misa.com"
  }
  ]
}

JSON SCHEMA of example 2

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "info": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "email": {
              "type": "string"
            }
          },
          "required": [
            "name",
            "email"
          ]
        },
        {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "email": {
              "type": "string"
            }
          },
          "required": [
            "name",
            "email"
          ]
        }
      ]
    }
  },
  "required": [
    "info"
  ]
}

In short, what I am looking for is a dynamic json schema that no matter how many times the collection grows, it can use only 1 and not generate several.

2 Answers 2

1

As you're using draft-04, I'll quote from the draft-04 specification.

This means you want items to have an object value as opposed to an array of objects.

The value of "items" MUST be either an object or an array. If it is
an object, this object MUST be a valid JSON Schema. If it is an
array, items of this array MUST be objects, and each of these objects MUST be a valid JSON Schema.

Draft-04 specificiation https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.3.1

In JSON Schema 2020-12, items may ONLY be an object value, and you must use a different keyword for tuple like validation.

Sign up to request clarification or add additional context in comments.

Comments

1

This worked for me-

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "info": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "email": {
              "type": "string"
            }
          },
          "required": [
            "name",
            "email"
          ]
        }
      ]
      "additionalItems":{
         "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "email": {
              "type": "string"
            }
          },
          "required": [
            "name",
            "email"
          ]
      }
    }
  },
  "required": [
    "info"
  ]
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.