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 nullable #535

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

Support nullable #535

wants to merge 9 commits into from

Conversation

mdmower
Copy link

@mdmower mdmower commented Jul 2, 2023

  • Add transform for nullable: true to include null as a possible type for any of the following schema types: const, enum, type.
  • Log a warning when const is not set to null but schema is marked nullable: true
  • Log a warning when enum does not contain null but schema is marked nullable: true
  • Add tests and update snapshot

This is a continuation of #522 by @jackfrankland to finish adding support for nullable: true in schemas.

jackfrankland and others added 3 commits April 17, 2023 13:23
- Add handling for enum, anyOf, and oneOf
- So long as the order of normalizers is maintained, no handling for
  const is necessary
- Add additional tests
@mdmower mdmower mentioned this pull request Jul 2, 2023
src/normalizer.ts Outdated Show resolved Hide resolved
src/normalizer.ts Outdated Show resolved Hide resolved
src/normalizer.ts Show resolved Hide resolved
- Update log message
- Support schema.const in case transforms are reordered in the future
- Apply new prettier styles
@mdmower mdmower marked this pull request as ready for review October 7, 2023 14:15
@mdmower mdmower changed the title Support nullable (draft PR for discussion on #522) Support nullable Oct 7, 2023
@mdmower
Copy link
Author

mdmower commented Oct 7, 2023

@bcherny - Would you be willing to review?

- Introduce warning() to output console warnings
- Add warning for nullable const, where const value is not null
- Move nullable transform before const->enum transform so that potential
  warning about nullable const can display
- Remove anyOf and oneOf handling since JSON spec should only constrain
  possibilities, not expand (allowance made for enum and const to expand
  types but they show warnings when this happens).
- Avoid pushing 'null' onto available types if it already exists.
Copy link

@zrosenbauer zrosenbauer left a comment

Choose a reason for hiding this comment

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

Other than the warn looks good to me.

Once you make the update or comment back & it makes sense to resolve happy to approve.


if (schema.const !== undefined) {
if (schema.const !== null) {
warning('normalizer', 'const should be set to null when schema is nullable', schema)

Choose a reason for hiding this comment

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

I understand why you added warning as its a good addition, but unless I'm reading the utils incorrectly it will always log if this conditional is hit. I don't see any other logs in this file, just errors being thrown.

The only location error is imported is in the cli.ts file and that means the programmatic version will always log if this is hit, when you are actually under the hood fixing the issue vs. logging a properly formated error during a cli command.

(see my comment on the recommended change in utils.tsa).

Comment on lines +213 to +216
if (!process.env.VERBOSE) {
return console.warn(messages)
}
console.warn(getStyledTextForLogging('yellow')?.('warning'), ...messages)

Choose a reason for hiding this comment

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

This will always log. If you look at the log function below it actually returns void when its NOT verbose.

Since this is just a warning I recommend the following change:

Suggested change
if (!process.env.VERBOSE) {
return console.warn(messages)
}
console.warn(getStyledTextForLogging('yellow')?.('warning'), ...messages)
if (!process.env.VERBOSE) {
return
}
console.warn(getStyledTextForLogging('yellow')?.('warning'), ...messages)

Copy link
Author

Choose a reason for hiding this comment

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

This was written to mimic

export function error(...messages: any[]): void {
if (!process.env.VERBOSE) {
return console.error(messages)
}
console.error(getStyledTextForLogging('red')?.('error'), ...messages)
}

I don't pretend to understand that choice and it's not my aim to question it. I'm just following precedence here.

Choose a reason for hiding this comment

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

I figured, but I believe that is only done in the cli not meant for run time usage as I don't really want console.error or console.warn being called when I use the library programatically.

@mdmower
Copy link
Author

mdmower commented May 23, 2024

@bcherny - I still hold hope that this PR could move forward. Is there anything I can do to improve its chances of merge? Maybe you have an opinion on zrosenbauer's review above? The logging was added based on this comment and jackfrankland's subsequent reply.

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

4 participants