SchemaStore / schemastore

A collection of JSON schema files including full API
http://schemastore.org
Apache License 2.0
3.08k stars 1.7k forks source link

type requirement in `metaschema-draft-07-unofficial-strict.json` #4031

Open Vampire opened 2 months ago

Vampire commented 2 months ago

Area with issue?

JSON Schema

✔️ Expected Behavior

This might either just be a question, or a bug report. Sorry if it turns out to just be a question, but I didn't find a more appropriate place to ask as discussions are not enabled and no chat or similar is linkes anywhere.

I'm writing a schema right now. I've sent it through the metaschema-draft-07-unofficial-strict.json. This complained at some places that the type is missing.

I for example have

"list-item": {
  "title": "...",
  "description": "...",
  "allOf": [
    { "$ref": "#/definitions/list_item_type" },
    {
      "anyOf": [
        { "$ref": "#/definitions/simple_primitive_property" },
        { "$ref": "#/definitions/integer_with_named_values_property" },
        { "$ref": "#/definitions/enum_property" }
      ]
    }
  ]
}

All those ref-ed schemas define the type to be object. If I validate a file against the schema where list-item is set to a string I correctly get a complaint from Ajv and IntelliJ that the type must be object.

But the metaschema-draft-07-unofficial-strict.json requires me to change this to

"list-item": {
  "title": "...",
  "description": "...",
  "type": "object",
  "allOf": [
    { "$ref": "#/definitions/list_item_type" },
    {
      "anyOf": [
        { "$ref": "#/definitions/simple_primitive_property" },
        { "$ref": "#/definitions/integer_with_named_values_property" },
        { "$ref": "#/definitions/enum_property" }
      ]
    }
  ]
}

This does not change much, except that the schemaPath in the Ajv output is different.

Is this expected and intentional? Is this a shortcoming of the meta schema? Can it just not know that the type would be coming from the ref-ed schema and thus has to complain to not miss other cases?

Are you making a PR for this?

No, someone else must create the PR.

EmilyGraceSeville7cf commented 2 months ago

AFAIR I wanted to make type to be specified explicitly as much as possible. The idea behind it was to make the writing schema more straightforward, without a lot of references between subschemas, to let people read schema from top to bottom without a lot of jumps between schemas. I think it can be improved though 😄.

hyperupcall commented 2 months ago

Yeah it's a shortcoming of the metaschema. In my opinion this is is a bug because it doesn't improve the text to be more readable or correct, since it does not change how the object is interpreted. (since for drafts 4-7, objects with $refs are treated like references and not schemas). And this makes the AJV schemaPath misleading. Doing this also makes the schemas less DRY and prone to go out of sync (and detecting this mismatch does not seem to be detectable with AJV strict mode).