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

References to anchors misinterpreted, leading to validation errors #22

Open
sam-mccall opened this issue Dec 27, 2022 · 1 comment
Open

Comments

@sam-mccall
Copy link

sam-mccall commented Dec 27, 2022

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
@neilpa
Copy link
Owner

neilpa commented Jan 1, 2023

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 (xeipuuv/gojsonschema#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants