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

Support prefixItems for tuple validation #543

Open
isti115 opened this issue Jul 26, 2023 · 3 comments
Open

Support prefixItems for tuple validation #543

isti115 opened this issue Jul 26, 2023 · 3 comments

Comments

@isti115
Copy link

isti115 commented Jul 26, 2023

From the JSON Schema 2020-12 Release Notes:

The items and additionalItems keywords have been replaced with prefixItems and items where prefixItems has the same functionality as the array-of-schemas for of the old items and the new items keyword has the same functionality as the old additionalItems keyword.

For an example with more explanation:
https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation

As per #1 (which has a broken link by the way), here is the respective file from the official test suite:
https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/main/tests/draft2020-12/prefixItems.json

In case there is interest in resolving this issue, I would be willing to look into the code (I've already checked the relevant parts of parsing and generation) and try to provide a pull request for it.

@bcherny
Copy link
Owner

bcherny commented Sep 23, 2023

This is a pretty unfortunate change in the spec. It means that the meaning of items is dependent on the version of JSON-Schema you use. But since a JSON-Schema is not required to specify its version, this leaves the interpretation of items ambiguous in practice 🫤.

A middle of the road approach to support this without breaking others' code may be to expose the new behavior behind a switch, say --json-schema-version=2020-12. We would check the option in the normalizer, and normalize prefixItems to items and items to additionalItems if needed.

Want to give this a shot?

TheoAnastasiadis added a commit to TheoAnastasiadis/json-schema-to-typescript that referenced this issue Jan 25, 2024
added `--usePrefixItems` cli argument. When passed, JSON schemata containing `prefixItems` tuple validation key will parsed correctly.
TheoAnastasiadis added a commit to TheoAnastasiadis/json-schema-to-typescript that referenced this issue Jan 25, 2024
Function isArrayType() should return true when `prefixItems` is present, even without `items` key
TheoAnastasiadis added a commit to TheoAnastasiadis/json-schema-to-typescript that referenced this issue Jan 25, 2024
Added `usePrefixItems` option, which defaults to false
TheoAnastasiadis added a commit to TheoAnastasiadis/json-schema-to-typescript that referenced this issue Jan 25, 2024
Created new test case
@TheoAnastasiadis
Copy link

I thought I'd have a go at it.

Added a CLI argument as "--usePrefixItems" which defaults to false, I did not think specifying JSON schema spec version would be helpful at this point since only this version would be supported.

The main addition is a new normalizer rule; renaming the schema keys.

schema.additionalItems = schema.items
schema.items = schema.prefixItems
delete schema.prefixItems

I created a simple unit test as well. I will let it sink in cause maybe more test cases might be needed. I had difficulty understanding the E2E testing setup, so I did not add any new E2E tests, maybe if somebody would very briefly walk me through I am going to do so. Tests are passing, for now.

@xcanchal-ut
Copy link

Any news on this topic?

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

No branches or pull requests

4 participants