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

enhancement bcherny#543 - Prefix items support #574

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

TheoAnastasiadis
Copy link

Added optional --usePrefixItems flag that defaults to false.

When passed JSON schemas containing prefixItems and items keys are normalized as items and additionalItems, as per JSON Schema Draft 2020-12.

The draft also contains changes to the $recursiveRef and $recursiveAnchor keywords, which this PR does not deal with.

✔Tests added

added `--usePrefixItems` cli argument. When passed, JSON schemata containing `prefixItems` tuple validation key will parsed correctly.
Function isArrayType() should return true when `prefixItems` is present, even without `items` key
Added `usePrefixItems` option, which defaults to false
Created new test case
Copy link
Owner

@bcherny bcherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

A few small nits. Please also add an e2e test, similar to https://github.com/bcherny/json-schema-to-typescript/blob/master/test/e2e/options.unreachableDefinitions.ts.

At some point we should explicitly add a --jsonSchemaVersion flag, but this is a good stop-gap for now.

Thanks!

rules.forEach(rule => traverse(rootSchema, (schema, key) => rule(schema, filename, options, key, dereferencedPaths)))
return rootSchema as NormalizedJSONSchema
}
/* eslint-disable prettier/prettier */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't disable Prettier.


rules.set('Rename `prefixItems` to `items` and `items` to `additionallItems`', (schema, _, options) => {
if (isArrayType(schema) && options.usePrefixItems) {
// @ts-expect-error this is a simple renaming procedure, there is in no need to change the `schema.items` type to allow for boolean values
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't expect an error. Instead, make the types safer or use a type assertion (a as A).

schema.items = schema.prefixItems
delete schema.prefixItems
}
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove this

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

Successfully merging this pull request may close these issues.

None yet

2 participants