ajv icon indicating copy to clipboard operation
ajv copied to clipboard

Ref within def does not act consistently

Open meghprkh opened this issue 4 months ago • 0 comments

What version of Ajv are you using? Does the issue happen if you use the latest version? Yes

├── [email protected]
├── [email protected]

Ajv options object

CLI invocation ajv validate -s test.schema.json -d test.json

JSON Schema

This schema would work complain that:

schema schema/test.schema.json is invalid
error: can't resolve reference #/$defs/JSONValue from id KVMap
{
  "$ref": "#/$defs/KVMap",
  "$defs": {
    "JSONValue": {
      "$id": "JSONValue"
    },
    "KVMap": {
      "$id": "KVMap",
      "type": "object",
      "patternProperties": {
        "^(.*)$": {
          "$ref": "#/$defs/JSONValue"
        }
      }
    }
  }
}

I am unsure if refs should use IDs instead of JSON Pointers everywhere, but IDs do seem to work.

The inconsistency between VSCode and ajv's validator wrt IDs is that ajv accepts ID as JSONValue instead of #JSONValue too, whereas VSCode does not. I cant find any mention of this on JSONSchema and think it might be VSCode which is wrong here.

Sample data

{}

What results did you expect?

This should be a valid case in JSON schema, because dictionaries are not sorted so it would be impossible to refer to a non-id anchor based def.

Are you going to resolve the issue? I dont know

meghprkh avatar Mar 10 '24 02:03 meghprkh