Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema Compare two properties to be equal length array

I have two properties in a schema which are both arrays. I would like to compare that they're equal length.

For example:

schema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "thing": {
      "type": "object",
      "properties": {
        "fields": {
          "type": "array",
          "items": {}
        },
        "values": {
          "type": "array",
          "items": {}
        }
      },
      "required": ["fields", "values"]
    }
  }
}

data.json

{
  "thing": {
    "fields:": ["age", "sex", "location"],
    "values:": [25, "Male", "Cape Town"]
  }
}

What I would like is to compare that fields and values are the same length in the schema. How would I do that?

like image 205
Ryan Avatar asked Oct 19 '22 05:10

Ryan


1 Answers

In JSON Schema, all validation keywords are scoped to the value that it applies to. In other words, you can't validate one value in terms of another.

If you refactor this structure in almost any other way, you shouldn't have any problems.

For example, you could define a list of pairs using the array form of the items keyword.

{
  "thing": [
    ["age", 15],
    ["sex", "Male"],
    ["location", "Capetown"]
  ]
}

Or, you could use the additionalProperties keyword and use an object to show key/value pairs in a more natural way.

{
  "thing": {
    "age": 15,
    "sex": "Male",
    "location": "Capetown"
  }
}
like image 122
Jason Desrosiers Avatar answered Nov 15 '22 10:11

Jason Desrosiers