Validate that json only contains ascii characters using json schema

Viewed 634

We are interfacing with a hardware device, where we are uploading json configuration files. The device cannot handle non-ascii characters anywhere on the json - not even if escaped use \uXXXX.

We are already doing schema validation using draft-07, so we would like to specify this constraint in the schema. We can put regex constraints on all our strings.

But the problem is that we must have additionalProperties: true in several places - and the additional properties must allow any json values. But we must also constrain these to contain only ascii characters.

Is this possible to specify in the schema?

Sample simplified schema:

{
   "$schema":"http://json-schema.org/draft-07/schema#",
   "type":"object",
   "properties":
   {
      "channel":
      {
        "type":"array",
        "items":
         {
            "type":"object",
            "properties":  { "name":{"type":"string"} },
             "required":["name"],
            "additionalProperties":true
         }
      }
   },
   "required":["channels"],
   "additionalProperties":true
}

Sample json which should not validate:

{
  "channels":[ {"name":"temperature", "input": "temp in °C"}],
  "other":[ {"mykey": "ü"}],
  "danish": "æøå"
}
1 Answers

The value of additionalProperties is a JSON Schema itself (booleans ARE a JSON schema too!).

If you set the value of additionalProperties to that similar to your other properties, it will be applied to all additional properties.

If you would like an example, please provide an example schema I can work with, and I'd be happy to show.

Related