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

Add support for nullable primitives #411

Closed
wants to merge 1 commit into from

Conversation

goodoldneon
Copy link

@goodoldneon goodoldneon commented Sep 21, 2021

Picking up where #312 left off.

This PR adds a normalizer that converts nullable: true primitive types into anyOf types.

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 minor suggestions.

Comment on lines +155 to +160
delete copiedSchema.id
delete copiedSchema.nullable

// This stuff will be in `anyOf` instead.
delete schema.format
delete schema.type
Copy link
Owner

Choose a reason for hiding this comment

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

Can you remove all of these deletes? Unclear why you need them (156 is already included in 150; 155+159+160 I'm not sure what they're here for).

Copy link
Author

Choose a reason for hiding this comment

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

@bcherny

I think we need all of these deletes. After normalizing, we no longer want nullable, format, or type to appear in the root of the schema. That's what the anyOf is replacing.

For example, the following schema:

{
  "id": "foo",
  "type": "integer",
  "format": "int32",
  "nullable": true
}

Should be normalized to this:

 {
  "id": "foo",
  "anyOf": [
    {
      "type": "integer",
      "format": "int32"
    },
    {
      "type": "null"
    }
  ]
}

Notice how nullable is gone, and type and format have moved into the anyOf


schema.anyOf = [
copiedSchema,
// @ts-expect-error
Copy link
Owner

Choose a reason for hiding this comment

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

Can you rm this?

Copy link
Author

Choose a reason for hiding this comment

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

I get a TS error without it:

Type '{ type: "null"; }' is missing the following properties from type 'LinkedJSONSchema': additionalProperties, [Parent]ts(2739)

Copy link
Owner

Choose a reason for hiding this comment

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

Cool! Can you find a way to fix the error? eg. to add an empty additionalProperties, and to add a Parent field?

@bcherny bcherny mentioned this pull request May 22, 2022
@klarstrup
Copy link

Bump 😭

@drewcorlin1
Copy link

I'd love to be able to use this feature as well. Is there anything I can do to help this PR get merged?

@austin-garner
Copy link

Bump

@goodoldneon
Copy link
Author

Sorry, y'all. I don't write TypeScript at my new job (unfortunately 😭) so I forgot about this PR. I just responded to a comment the maintainer had, so hopefully I'll be able to push this over the finish line soon

@lucas-inflab
Copy link

lucas-inflab commented Mar 2, 2023

I really need to make this change, is there anything I can do such as new pull request? @bcherny

@jackfrankland jackfrankland mentioned this pull request Apr 17, 2023
@bcherny
Copy link
Owner

bcherny commented May 4, 2023

Closing in favor of #522

@bcherny bcherny closed this May 4, 2023
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

6 participants