Skip to content

Add a test for schema-items alongside "ignored" additionalItems #635

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

Closed
Julian opened this issue Feb 8, 2023 · 3 comments · Fixed by #643
Closed

Add a test for schema-items alongside "ignored" additionalItems #635

Julian opened this issue Feb 8, 2023 · 3 comments · Fixed by #643
Labels
enhancement An enhancement to the tooling or structure of the suite (as opposed to a new test). good first issue An issue that is a good candidate for new contributors missing test A request to add a test to the suite that is currently not covered elsewhere.

Comments

@Julian
Copy link
Member

Julian commented Feb 8, 2023

In pre-2020 drafts, items could take a schema or an array of schemas, and there was an additionalItems keyword. See e.g. here

We have no test at the minute that asserts against the result of including a single schema items alongside a (vacuous) additionalItems (where the behavior should be that the result depends solely on the items schema).

In other words, a schema such as:

{
  "items": {"type": "integer"},
  "additionalItems": {"type": "string"}
}

when applied to [1, 2, 3] should succeed, and when applied to [1, "2", "3"] should fail.

@Julian Julian added missing test A request to add a test to the suite that is currently not covered elsewhere. enhancement An enhancement to the tooling or structure of the suite (as opposed to a new test). good first issue An issue that is a good candidate for new contributors labels Feb 8, 2023
@ghost
Copy link

ghost commented Feb 26, 2023

{
    "description": "validation based on item keyword",
    "schema": {
        "$schema":"https://json-schema.org/draft/2020-12/schema",
        "items": {
            "type": "integer"
        },
        "additionalItems": {
            "type": "string"
        }
    },
    "tests": [
        {
            "description": "valid with a array of type integers",
            "data": [1,2,3],
            "valid": true
        },
        {
            "description": "invalid with a array of mixed types",
            "data": [1,"2","3"],
            "valid": false
        }
    ]
}

Hi @Julian can you please check if this test is valid . If its okay ,which file should I be adding this to or should i create a new one .

@Julian
Copy link
Member Author

Julian commented Feb 26, 2023

Thanks for trying to tackle this @0xSudarshan super appreciated. Here's some notes:

  • If you're unsure whether your example is correct feel free to ask as you did, though just in case, a good way of checking is to take any JSON Schema validator (including an online one like this one) and try out the thing you put in "schema" against each of the things you put in tests -- and seeing if the result matches. If it does, you probably have it right! If it doesn't, you probably have it wrong. Though technically both cases can be wrong, because implementations may have bugs, which is exactly why we add new tests :)
  • You have it almost valid -- the behavior I mentioned applies only before draft2020 -- so we should add tests to 3 directories: draft2019-09, draft7 and draft6 (and not to draft2020-12 -- the additionalItems keyword doesn't exist there anymore in the new versions of the specification). The tests would be the same for all 3 directories, so you can basically copy paste, with only one difference -- the $schema key needs to be present in 2019 (similar to how you have it shown, but use the 2019 URL which you'll find in the 2019 files). And then for draft7 and 6 you can delete that $schema key
  • The most similar test for 2019 looks to me like this one which is almost our case here but it uses "items": {} so it has no way of testing an invalid example, whereas the one I showed can be tested against both valid and invalid examples like you showed -- but I'd put this new test right on top of the existing one there, and you should find a similar test in the draft7 and draft6 folders in the same spot
  • I'd make the description somehow mention that this is related to ignoring additionalItems, and the best description I think is similar to the test that we're "improving" -- that one has description "when items is schema, additionalItems does nothing" -- I think what we should do is use that description for your new test, and rename the existing description to "when items is schema, boolean additionalItems does nothing" (where I added the word "boolean" -- remember to do that in all 3 folders.

Let me know if that helps or if anything's unclear -- or if there's anything that'd help you as a new contributor to make this easier! And thanks again for giving this a shot!

@ghost
Copy link

ghost commented Feb 26, 2023

Thanks @Julian ! I have raised a PR #643 could you pls review it .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement to the tooling or structure of the suite (as opposed to a new test). good first issue An issue that is a good candidate for new contributors missing test A request to add a test to the suite that is currently not covered elsewhere.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant