remoteoss / json-schema-form

https://json-schema-form.vercel.app
MIT License
70 stars 7 forks source link

chore(fieldset): ignore values not matching the field type #44

Closed sandrina-p closed 10 months ago

sandrina-p commented 10 months ago

chore(fieldsets): ignores values not matching the field type

Steps to reproduce

Given a JSON Schema that expects a fieldset...

{
  properties: {
    a_fieldset: {
      title: 'Fieldset title',
      description: 'Fieldset description',
      'x-jsf-presentation': {
        inputType: 'fieldset',
      },
      properties: {
        id_number: mockTextInput,
        tabs: mockNumberInput,
      },
      required: ['id_number'],
      type: 'object',
    },
  },
  required: ['a_fieldset'],
}

And initialValues that do not match the fieldset structure (object)

onst result = createHeadlessForm(schemaInputTypeFieldset, {
  initialValues: {
    a_fieldset: 'foo', // should be an object instead of string
  },
});

The json-schema-form fails with an error:

TypeError: Cannot create property 'id_number' on string 'foo'

Solution

Check the fieldset's value type. If it's not an object, fallback to an empty object