Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rssh22 opened this issue Jul 18, 2024 · 2 comments
Open

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

rssh22 opened this issue Jul 18, 2024 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@rssh22
Copy link

rssh22 commented Jul 18, 2024

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!!

@sirosen
Copy link
Member

sirosen commented Jul 18, 2024

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.

@sirosen sirosen added enhancement New feature or request question Further information is requested labels Jul 18, 2024
@rssh22
Copy link
Author

rssh22 commented Jul 18, 2024

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
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants