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

Default type to object if undefined #1265

Closed
3 tasks done
domharrington opened this issue Apr 24, 2019 · 3 comments · Fixed by #1292
Closed
3 tasks done

Default type to object if undefined #1265

domharrington opened this issue Apr 24, 2019 · 3 comments · Fixed by #1292

Comments

@domharrington
Copy link
Contributor

Prerequisites

Description

Can we default the type to object if it's undefined? We accept user submitted JSON Schemas and this is a common thing that people miss. I'm not 100% sure if it's valid JSON Schema or not, but https://www.jsonschemavalidator.net/ appears to say the following is valid:

{
  "properties": {
    "a": {
      "type": "string"
    }
  }
}

But when passing that into this module, we get the following error:

image

Playground link: https://mozilla-services.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6e30sInNjaGVtYSI6eyJwcm9wZXJ0aWVzIjp7ImEiOnsidHlwZSI6InN0cmluZyJ9fX0sInVpU2NoZW1hIjp7fX0=

Steps to Reproduce

  1. Pass in a JSON Schema without a type
  2. Observe that the UnsupportedField is instantiated

Expected behavior

Default type to object if it is undefined
If type is set to something that is unknown, still create the UnsupportedField e.g. type: 'someothertype'

Affected blocks of code:
Could check for undefined in here and return object: https://github.com/mozilla-services/react-jsonschema-form/blob/2d63594e075bba3b134ef09709c773a9ea037923/src/utils.js#L68-L84

This is where it will bubble up to, but I don't think this code would have to change at all: https://github.com/mozilla-services/react-jsonschema-form/blob/2d63594e075bba3b134ef09709c773a9ea037923/src/components/fields/SchemaField.js#L49-L59

Happy to send over a PR for this if it'll get accepted!

Version

v1.5.0

@epicfaace
Copy link
Member

Yes, I believe that makes sense.

Why would you make the default type object when undefined? Wouldn't it be enough just to make the type default to object only if a properties keyword is defined as well?

@domharrington
Copy link
Contributor Author

Yeah that would also work, though you'd then also have to check for additionalProperties as well. Any other edge cases you can think of?

@epicfaace
Copy link
Member

I think that's it!

domharrington added a commit to domharrington/react-jsonschema-form that referenced this issue May 21, 2019
epicfaace pushed a commit that referenced this issue Jun 2, 2019
* Default type to `object` if undefined

Fixes #1265

* Tidy up if statement expression as per PR review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants