json-schema - reference all nested properties as string regardless of name

617 Views Asked by At

I am trying to reference all nested properties as string regardless of name.

An example of the data looks like this (except with a bunch of columns):

[
    {
        "var_1": "some_string",
        "var_2": {
                "col_1": "x",
                "col_2": "y",
                "col_3": "z"
                },
        "var_3": "another_string"
        
    }
]

I used a yaml to json converter and got the following json but my process to flatten the file does not seem to get the nested information.

{
  "$id": "main.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "some data",
  "type": "object",
  "properties": {
    "var_1": {
      "$ref": "another_schema.json#/definitions/var_1"
    },
    "var_2": {
      "type": "object",
      "properties": {
        "fieldNames": {
          "uniqueItems": true,
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "var_3": {
      "type": "string",
      "description": "another variable"
    }
  }
}

Is there another way to reference all the variables/items inside of fields/fieldNames (col_1, col_2, col_3)

1

There are 1 best solutions below

3
Daniel Schneider On BEST ANSWER

I assume that you want to enforce that all properties under var_2 are of type string. I can think of 2 ways of doing that:

  1. Define additionalProperties with additional constraints, concretely "type": "string":
      "var_2": {
        "type": "object",
        "additionalProperties": {
          "type": "string"
         }
      },
  1. Use of patternProperties matching all field names (".*"). Here you define constraints for a regex matching against the field names (.* will match all field names), concretely:
      "var_2": {
        "type": "object",
        "patternProperties": {
          ".*": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },

Putting both into one schema (and adding the fact that your content starts with an array) would give you this:

{
  "$id": "main.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "title": "some data",
    "type": "object",
    "properties": {
      "var_1": {
        "type": "string"
      },
      "var_2a": {
        "type": "object",
        "patternProperties": {
          ".*": {
            "type": "string"
          }
        },
        "additionalProperties": false
      },
      "var_2b": {
        "type": "object",
        "additionalProperties": {
          "type": "string"
         }
      },
      "var_3": {
        "type": "string"
      }
    },
    "additionalProperties": false
  }
}

Will validate this:

[
    {
        "var_1": "some_string",
        "var_2a": {"foo": "x", "bar": "y"},
        "var_2b": {"foo": "x", "bar": "y"},
        "var_3": "another_string"        
    }
]

But fail this:

[
    {
        "var_1": "some_string",
        "var_2a": {"foo": 1},
        "var_2b": {"foo": true},
        "var_3": "another_string"        
    }
]