1

I have some 3rd party software that isn't handling Unicode correctly. I want to represent this by providing a JSON Schema that disallows non-ascii characters. For an individual value, I can say

{
    "type": "string",
    "pattern: "^[a-zA-Z0-9]*$"
}

which describes what I'm looking for.

However, I'd like to be able to just say all strings in the schema match this pattern. I could do a custom format, but that doesn't seem much better:

{
    "type": "string",
    "format": "ascii-string"
}

Is there a way to specify that I want every string to default to this format/pattern?

Nathaniel Waisbrot
  • 23,261
  • 7
  • 71
  • 99

1 Answers1

3

you can do this by applying the schema specifying the pattern to all children recursively with a $ref pointing children of objects (with additionalProperties) and arrays (with items) back at itself. that would look like:

{
  "pattern": "^[a-zA-Z0-9]*$",
  "additionalProperties": {
    "$ref": "#"
  },
  "items": {
    "$ref": "#"
  }
}

https://jsonschema.dev/s/YsM20

note that I used the pattern from your example, though that does not do the thing you said, restricting to ascii only. you can find a solution for that part elsewhere, e.g. Regex to test if only ASCII characters

you may also wish to add "propertyNames": {"$ref": "#"} since object property names are not normal values which the schema would apply to.

Ethan
  • 595
  • 2
  • 9
  • 1
    "If additionalProperties is an object, that object is a schema that will be used to validate any additional properties not listed in properties." Wow! I was more than half expecting the answer to be "you can't do this". Very cool! – Nathaniel Waisbrot Jul 30 '20 at 04:19