3

I have an object that has keys being numbers and its values strings:

{
  0: 'blah',
  2: 'blah'
}

What this object is, each key is the index of an array that contained an error, and the string describes that error. The closest I can figure out for this schema is:

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

However this is inacurate. Maybe next time only index "4" would have an error. Is there a way to describe dyanmic keys in in object?

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 2
    Your source data is not JSON, so you cannot create a JSON schema for it. JSON requires the keys of the javascript dictionaries to be quoted strings; numbers are not valid. (There are many "lax" json readers, in the spirit of Postel's law - "be liberal in what you accept, be strict in what you generate", that can read your data, but it's not valid JSON) – Erwin Bolwidt Feb 07 '19 at 21:38
  • 1
    https://stackoverflow.com/questions/8758715/using-number-as-index-json – tom redfern Feb 08 '19 at 10:23

1 Answers1

8

You should try patternProperties. It allows you to define a schema for all properties whose names match a given regex.

{
  "patternProperties": {
    "^[0-9]+$": {"type": "string"}
  }
}
gregsdennis
  • 7,218
  • 3
  • 38
  • 71