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

Fix boolean json schemas #515

Merged
merged 6 commits into from
May 4, 2023

Conversation

notaphplover
Copy link
Contributor

Added

  • Added NEVER schema type.

Changed

  • Updated parser to correctly parse boolean JSON schemas.

@notaphplover notaphplover marked this pull request as ready for review February 23, 2023 23:07
@notaphplover
Copy link
Contributor Author

Hey @bcherny, this should do the trick to support boolean JSON schemas. Do you have any test cases in mind?

I added a couple of cases but I'm open to add / remove more :)

if (isBoolean(schema)) {
return parseBooleanSchema(schema, keyName, options)
}

return parseLiteral(schema, keyName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh I don't know what to do here. In theory we should omit anything at this point instead of parsing the literal, but I don't want to introduce too many changes in this PR

Copy link
Owner

Choose a reason for hiding this comment

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

Could be a little cleaner to move the parseBooleanSchema into parseLiteral, since true/false are literals.

Copy link
Contributor Author

@notaphplover notaphplover Apr 26, 2023

Choose a reason for hiding this comment

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

I think this would be a mistake. Parsing true and false as schemas is not the same as parsing them as values. Depending on the context, true and false are schemas or values.

A simple example would be the true boolean schema vs a true enum value. They should not be parsed as the same.

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.

This looks great -- thanks for the contribution! Couple of minor suggestions. Could you also please add test cases for top-level true/false schemas?

ie.

export const input = true
export const input = false

if (isBoolean(schema)) {
return parseBooleanSchema(schema, keyName, options)
}

return parseLiteral(schema, keyName)
Copy link
Owner

Choose a reason for hiding this comment

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

Could be a little cleaner to move the parseBooleanSchema into parseLiteral, since true/false are literals.

@notaphplover
Copy link
Contributor Author

notaphplover commented May 2, 2023

Hey @bcherny, when adding the root boolean schema case, it seems the ref parser can not dereference it, it throws an error instead. I can have a look at it, but seems the parser is expecting an object instead of a boolean value.

Would it be ok if I submit a pr in the parser repo? I think some sort of conditional logic to handle boolean schemas would do the trick.

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.

Looks good. Thanks for the contribution!

@bcherny bcherny merged commit 05b0103 into bcherny:master May 4, 2023
@bcherny
Copy link
Owner

bcherny commented May 4, 2023

Published 13.0.0.

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.

2 participants