yajsv icon indicating copy to clipboard operation
yajsv copied to clipboard

References to anchors misinterpreted, leading to validation errors

Open sam-mccall opened this issue 3 years ago • 1 comments

Per my reading of the anchors spec the below should pass validation, and [https://www.jsonschemavalidator.net/s/tOTgHWLN](other validators seem to agree).

The intent is "the value of a property should be (something), or an array of (something)s".

Schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "PROPERTY": {
      "anyOf": [
        {
          "$anchor": "AAA",
          "type": "string"
        },
        {
          "type": "array",
          "items": { "$ref": "#AAA" }
        }
      ]
    }
  }
}

File:

{ "PROPERTY": [ "value" ] }

yajsv reports:

$ ~/go/bin/yajsv -s fail.schema.json fail.json
fail.json: fail: PROPERTY: Must validate at least one schema (anyOf)
fail.json: fail: PROPERTY.0: Invalid type. Expected: object, given: string
1 of 1 failed validation
fail.json: fail: PROPERTY: Must validate at least one schema (anyOf)
fail.json: fail: PROPERTY.0: Invalid type. Expected: object, given: string
$ ~/go/bin/yajsv -v
v1.4.0-dev

sam-mccall avatar Dec 27 '22 17:12 sam-mccall

I haven't been following the developments of JSON Schema lately. Looking at the release notes, it appears $anchors were added in the 2019-09 draft. Unfortunately, that's one version newer than what's supported by xeipuuv/gojsonschema which is what yajsv depends on.

I found an open issue (https://github.com/xeipuuv/gojsonschema/issues/289) about adding support for 2019-09. That references some alternative libraries that could be options to use here. I'll have to dig into that when I have some time.

At a minimum, yajsv could check the value of the $schema key then warn and/or error if it's not supported. Depends on if 2019-09 is at least partially backward compatible with earlier versions.

neilpa avatar Jan 01 '23 04:01 neilpa