I have a JSON schema I was using to specify enumerations that map unique string names to unique numbers.
Here is the schema, and yes I am aware that it doesn’t guarantee the above requirement, but I’m not bothered about that at the moment.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Module",
"description": "Schema for module definitions",
"type": "object",
"properties": {
"enums": {
"type": "object",
"patternProperties": {
"^.[^."]*$": {
"$ref": "#/$defs/enum"
}
},
"additionalProperties": false
}
},
"additionalProperties": false,
"$defs": {
"enum": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "integer"
}
],
"minItems": 2,
"maxItems": 2
}
}
}
}
I am converting from Json.net to System.Text.Json & Jsonschema.net. Here is example JSON object that validated with json.net, but not with Jsonschema.net, presumably because "$schema": "https://json-schema.org/draft/2020-12/schema" is taken more seriously with the latter.
{
"enums": {
"Drive Types": [
[
"DOL", 1
],
[
"VSD", 2
],
[
"Servo", 3
]
],
"Conveyor State": [
[
"Off", 0
],
[
"Faulted", 1
],
[
"Idle", 2
]
]
}
}
What I want to do is modify my schema (and JSON object) so that it works with strict validation against Draft 2020-12. How can I do that?
2
Answers
In 2020-12,
items
no longer has an array form. That has been replaced withprefixItems
. Your schema can replace the inner 2-element arrayitems
keyword withprefixItems
and that should be correct.See: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#name-keywords-for-applying-subschema
and https://json-schema.org/understanding-json-schema/reference/array
Seems like you want a schema like this
No need for
minItems
andmaxItems
when you define theprefixItems
schema. You prevent any additional items by settingitems: false