skip to Main Content

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


  1. In 2020-12, items no longer has an array form. That has been replaced with prefixItems. Your schema can replace the inner 2-element array items keyword with prefixItems 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

    Login or Signup to reply.
  2. Seems like you want a schema like this

    No need for minItems and maxItems when you define the prefixItems schema. You prevent any additional items by setting items: false

    {
    "$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"
                }
            },
            "unevaluatedProperties": false
        }
    },
    "unevaluatedProperties": false,
    "$defs": {
        "enum": {
            "type": "array",
            "items": {
                "type": "array",
                "prefixItems": [
                    {
                        "type": "string"
                    },
                    {
                        "type": "integer"
                    }
                ],
                "items": false
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search