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

Allow type or null? #441

Closed
Andrew-Max opened this issue Jan 13, 2020 · 2 comments
Closed

Allow type or null? #441

Andrew-Max opened this issue Jan 13, 2020 · 2 comments

Comments

@Andrew-Max
Copy link

Andrew-Max commented Jan 13, 2020

I have a schema:

{
    type: "object",
    properties: {
      product_id:      { type: "string" },
      tip_description: { type: "string" },
      core_diameter:   { type: "number" },
      outer_diameter:  { type: "number" },
      aperature:       { type: "number" },
    },
    required: []
  }

Note nothing is required here and the only way to set an undefined is with null

So it seems to me like this should be a valid entry

{
  "product_id": null,
  "tip_description": null,
  "core_diameter": null,
  "outer_diameter": null,
  "aperature": null
}

But validation fails telling me that it expected string or numeric types, not null. I explicitly do not want to trim out null properties, I want them to be saved as present with a value of null so it is clear to end users which properties exist.

Is there any way to accomplish this?

@stefanosx
Copy link

I run into the same issue, and I solved the issue by adding an array as a type(Not sure if it is the best solution).

So you can change your schema to look like this:

{
    type: "object",
    properties: {
      product_id:      { type: ["string", "null"] },
      ...
    },
    required: []
  }

And that should work fine.

@Andrew-Max
Copy link
Author

@stefanosx clever solution. It's hackier than I'd like for something I'm putting into production but it does solve my immediate issue for now. Thanks!

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

No branches or pull requests

2 participants