2

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": "æøå"
}
Niels Harremoes
  • 320
  • 1
  • 18
  • Thanks for the updated example. Given you need the checking to happen deeply, it's a little tricker. Should all values of unknown properties be strings or objects (or either)? Is it just, if they contain any string anywhere, they need to be ascii only? – Relequestual May 27 '20 at 10:06
  • 1
    It's probably easier to do this in two separate steps. Treating the incoming data symbol by symbol for them being in the ASCII character set and the schema validation. If you have the whole data in a string, a simple regular expression would do (as asked for here: https://stackoverflow.com/q/3203190/5127499 – basically `[^ -~]` would tell you whether there is any non-ASCII symbol). A similar regular expression could be included e.g. via `patternProperties`, `pattern` and other keywords, but it'd be rather cumbersome. – Carsten May 27 '20 at 10:46
  • I agree, doing it as a two step process would be a LOT easier. There's going to be unexpected complexity in a schema which does what you want. – Relequestual May 27 '20 at 12:27
  • The additional properties can be anything - strings, numbers, deep objects, ... . But I need to validate all text - even property names. So you are probably right that this is easier solved in a step apart from the schema validation. I will pursue that, unless someone comes up with a genius schema hack :-) – Niels Harremoes May 27 '20 at 12:46
  • "propertyNames" can validate the names of unspecified properties, and the addition of "unevaluatedProperties" in draft2019-09 should make this a bit easier too, to force *all* property names and values through a restrictive "ascii_string" definition. – Ether May 28 '20 at 16:17

1 Answers1

0

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.

Relequestual
  • 11,631
  • 6
  • 47
  • 83