wolfson109
wolfson109

Reputation: 934

How do I use JSONSchema to accept any object string value, regardless of its key?

I have a system that is receiving JSON messages which contain metadata from a static analysis of a file. The names of these fields are dynamically generated from the scan and can be any valid string, but the value is always a valid string.

e.g.

{
    "filename": "hello.txt",
    ...
    "meta": {
        "some file property": "any string",
        "some other file property": "another string",
        ...
    }
}

I have no way of knowing what the keys in meta will be before receiving the message, nor do I know how many keys there will be. Is there a way of capturing in a JSONSchema that it doesn't matter what keys are present, so long as their values are always strings?

Upvotes: 7

Views: 1395

Answers (1)

Relequestual
Relequestual

Reputation: 12355

I think you're looking for additionalProperties

Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".

The value of additionalProperties can be a JSON Schema, like so

...
"additionalProperties" : {
  "type": "string"
}
...

Feel free to let me know if I've missed anything in my explanation, or ask any further questions.

Upvotes: 7

Related Questions