react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

formData fields that are not defined in schema are rendered or show an error

Open LucianBuzzo opened this issue 7 years ago • 7 comments

Prerequisites

  • [x] I have read the documentation;
  • [x] In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

It appears that as of v1.0.6 formData values that do not have a corresponding schema definition are automatically displayed in the form if the additionalProperties key in the schema is set to true. This behaviour did not exist in v1.0.5. Additionally, if the undefined field is an array, I see an ugly error message in the form.

screen shot 2018-11-19 at 11 12 36

Steps to Reproduce

  1. Render a form using formData that contain fields undefined in your schema

See this codesandbox for v1.0.6 behaviour: https://codesandbox.io/s/v00w0081jl See this codesandbox for v1.0.5 behaviour: https://codesandbox.io/s/r01o58w6lo

Expected behavior

I expect undefined fields to not be displayed at all in the form, or be provided with some configuration option to stop this from happening.

Actual behavior

Fields in formData that are not defined in the schema are rendered as form inputs, and if they are arrays I see an error.

Version

v1.0.6

LucianBuzzo avatar Nov 19 '18 11:11 LucianBuzzo

Looking at the test cases this appears to be by design: https://github.com/mozilla-services/react-jsonschema-form/blob/master/test/ObjectField_test.js#L462 The documentation, however, makes no mention of this, the only mention of additionalProperties refers to defining additionalProperties as a schema object ( https://github.com/mozilla-services/react-jsonschema-form#expandable-option )

I feel that this should have been a major version bump, rather than a patch. The documentation should clearly describe how additionalProperties works.

LucianBuzzo avatar Nov 19 '18 11:11 LucianBuzzo

I've been pretty sloppy with version numbers recently so I apologize about that. 1.0.5, 1.0.6 and 1.0.7 all introduced new features so they should all at least have been minor version bumps. Oops. I would be happy to merge a PR that added some documentation about the additionalProperties feature.

I would say that generally (no additionalProperties present), I would expect fields to not be shown nor removed and not cause an error. If additionalProperties is present, I think it's reasonable for the user to care about the presence of additionalProperties and so I don't see a problem with rendering them. This is just an intuition, not backed by rigorous formal reasoning, though, so I'm open to discussion. I guess my question is, why do you have additionalProperties: true in the first place?

glasserc avatar Jan 10 '19 20:01 glasserc

@glasserc In the system I am working on, JSON schema is used to for modelling, filtering and rendering forms for documents. Using additionalProperties: true allows the document to be augmented with additional values that aren't defined in the schema. The behaviour of react-jsonschema-form when additionalProperties is set to true has been straightforward for me to work around, so this hasn't stopped me from developing with this module.

The error shown if the undefined field is an array appears to be a bug and some additional documentation for the additionalProperties behaviour would be a good idea.

LucianBuzzo avatar Jan 11 '19 09:01 LucianBuzzo

I understand that you want to augment your documents with values not defined in the schema. (I'm also assuming that "by default" you don't want them to be displayed in the form.) But why is additionalProperties: true necessary to do that? The JSON Schema validation spec says that if additionalProperties is absent, it should be the same as if it had been given an empty object, which should also cause it to accept non-schema values.

glasserc avatar Jan 11 '19 15:01 glasserc

Was there every some documentation added around how to use additionalProperties correctly?

I am having the same issue where form fields are appearing when the formData is empty because certain items are defined in the schema.

zammit123 avatar Jul 17 '19 08:07 zammit123

Wow, still reproducible in the latest playground

heath-freenome avatar Apr 09 '23 22:04 heath-freenome

This issue has been automatically marked as possibly close because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.

stale[bot] avatar Jul 09 '24 00:07 stale[bot]

@LucianBuzzo With the addition of the Fallback field feature, you can now have the form provide a better UI than what was there previously by enabling the feature. Closing....

heath-freenome avatar Nov 07 '25 20:11 heath-freenome