Skip to content

--check-metaschema doesn't check $ref referenced schemas #464

Open

Description

@rssh22

Considering the next json schema definition:

{
   "title": "main.json"
    "$id": "file://test-schemas/schemas/main.json",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "required": [
        "metadata"
    ],
    "properties": {
        "metadata": {
            "additionalProperties": false,
            "type": "object",
            "required": [
                "props1",
                "props2"
            ],
            "properties": {
                "props1": {
                    "$ref": "/schemas/props1.json#/props"
                },
                "props2": {
                    "$ref": "/schemas/props2.json#/props"
                }
            }
        }
    }
}
{
    "title": "props1.json"
    "$id": "/schemas/props1.json",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "props": {
        "type": "object",
        "required": [
            "metadata"
        ],
        "properties": {
            "metadata": {
                "additionalProperties": false,
                "type": "object",
                "required": [
                    "name",
                    "id"
                ],
                "properties": {
                    "name": {
                        "type": "BADstring"
                    },
                    "id": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}
{
   "title": "props2.json"
    "$id": "/schemas/props2.json",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "props": {
        "type": "object",
        "required": [
            "metadata"
        ],
        "properties": {
            "metadata": {
                "additionalProperties": false,
                "type": "object",
                "required": [
                    "name",
                    "id"
                ],
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "id": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}

I'd like to be able to check fully recursively the schema main.json but it seems that check-jsonchema --check-metaschema main.json doesn't traverse references.

In this case, props1.json schema contains an error. "type" can't be "BADstring" (according to the metaschema". However, the check of main.json returns an ok -- validation done

Am I doing something wrong, there is a bug, or it is feature that could be proposed for enhancement?

thanks in advance!!

Activity

  1. sirosen commented on Jul 18, 2024

    @sirosen
    Member

    This is not expected to work in the way you were hoping, but I'm not sure if it's a feature request that I'd be willing to support/add.
    Let me explain, and I'll have to take more time to think about the correct path forward.


    Under the metaschema check, your schema is data being checked against a JSON Schema.
    Conceptually, if every schema evaluation looks like

    validate($SCHEMA, $DATA)
    

    then we can translate the following two invocations thusly:

    check-jsonschema --schemafile myschema.json foo.json
      => validate(myschema.json, foo.json)
    
    check-jsonschema --check-metaschema myschema.json
      => validate($METASCHEMA, myschema.json)
    

    That is, it gets no special treatment, and normal JSON Schema checking rules apply. Since it's being evaluated as an ordinary document, there's no expectation that $refs are followed. Ref resolution applies "when you are the schema", not "when you are the data".


    That explains why your $refs aren't followed. It doesn't exactly help you do metaschema checking on the contents of those refs.

    I'm giving thought to whether or not following refs (opt-in?) would be appropriate for the metaschema check. It certainly would be nice and it makes perfect sense! But it also breaks the symmetry between normal schema evaluation and metaschema evaluation, and that makes me a little uneasy.

    For right now, you can pull your $ref contents manually and check them with the metaschema check as well. I know that's not what you're looking for, but it's the solution which is available today.

  2. rssh22 commented on Jul 18, 2024

    @rssh22
    Author

    Hi @sirosen,

    I understand your concerns about breaking the symmetry. I just can argue that from an schema developer point of view it would be very useful and it will simplify the metaschema validation both manually and in cicd pipelines :)

    Whatever you decide,
    Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions