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

bug with items array #311

Closed
andrejlevkovitch opened this issue Jan 26, 2024 · 8 comments
Closed

bug with items array #311

andrejlevkovitch opened this issue Jan 26, 2024 · 8 comments

Comments

@andrejlevkovitch
Copy link
Contributor

For tuple validation json schema allows usage of items keyword as an array, for example like here:

{
  "type": "array",
  "items": [
    {
      "$comment": "some comment",
      "type": "number"
    },
    {
      "type": "string"
    }
  ]
}

Also, we can use it in $defs too, like here:

{
  "type": "object",
  "properties": {
    "element": {
      "$ref": "#/$defs/element"
    }
  },
  "$defs": {
    "element": {
      "type": "array",
      "items": [
        {
          "type": "number"
        },
        {
          "type": "string"
        }
      ]
    }
  }
}

And that works perfectly with the library. But if I put $comment inside of one of the items, then I have exception:

cannot use operator[] with a string argument with array

So, that schema will throw the exception when you try to set it to validator

{
  "type": "object",
  "properties": {
    "element": {
      "$ref": "#/$defs/element"
    }
  },
  "$defs": {
    "element": {
      "type": "array",
      "items": [
        {
          "$comment": "some comment",
          "type": "number"
        },
        {
          "type": "string"
        }
      ]
    }
  }
}

Looks like only $comment keyword doesn't work properly in that context

@andrejlevkovitch
Copy link
Contributor Author

I traced the issue to that line

@andrejlevkovitch
Copy link
Contributor Author

seems that there are a problem with indexing - it tries to use an array as object and uses array index as object key

@andrejlevkovitch
Copy link
Contributor Author

probably it is related to that comment:

// need to create an object for each reference-token in the
// JSON-Pointer When not existing, a stringified integer reference
// token (e.g. "123") in the middle of the pointer will be
// interpreted a an array-index and an array will be created.

but I can not understand what that means. Is there a reason why it doesn't use json pointer directly?

@andrejlevkovitch
Copy link
Contributor Author

If I understood everything correctly, the that pr should fix the issue

@pboettch
Copy link
Owner

pboettch commented Jan 26, 2024

So, it seems there is a bug with the handling of unknown keywords. Your schema shows that.

But why we have the unknown keyword handling getting involved here? Because $defs is something from the 2020-draft of json-schema not directly supported by this library. For the draft7-version it should be definitions. This is something to be taking into consideration in the analysis of this bug.

@andrejlevkovitch
Copy link
Contributor Author

andrejlevkovitch commented Jan 27, 2024

Yeah, that is fair. I'm not sure what I should do with that issue then... I mean should I close it or keep it?

@pboettch
Copy link
Owner

Well, it is still a bug with the unknown keyword handling, I'd need a better explanation as to why this happens before merging your PR.

@pboettch
Copy link
Owner

pboettch commented Feb 5, 2024

The errors wasn't the $comment field but a nested schema in an array of schemas in an unknown-keyword.

Your fix was correct and elegant: using a json-pointer to resolve the direct sub-keyword instead of find. This works for arrays and objects. Thanks for contributing.

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