Null type support

Hi, I have a question about schema support in the React Material renderer - I’m trying out JSON Forms / React for a project, and the schema has a lot of entries like this:

"name": {
  "oneOf": [
    {
      "type": "null"
    },
    {
      "type": "string"
    }
  ]
}

Which causes the renderer to return an error Error: Unknown type: {“type”:“null”}
And I see that uischema.ts doesn’t account for a null type.

I’m wondering - is this a design decision? I would look at adding the type to the core package (not sure how the renderer sets would expect to handle a ‘null’ type, however), or change the schema to something like

"name": {
  "type": [ "string", "null" ]
}

Which does render better. It feels like this with a combination of validation rules should handle the use case - effectively wanting to have a nullable field - but I wanted to make sure I wasn’t missing something with the "type": null support.

1 Like

Hi @yaffol!

so far type: "null" has been given a second-rate treatment as it’s not a common type found in schemas which specifically describe forms. It seems that at least the OneOfRenderer doesn’t handle it correctly as well as that we have no specific renderer for a type: "null" input.

The React Material renderers by default place undefined in the data object whenever an element is “deleted”, e.g. when the clear button is executed in the String inputs.

The renderers could be extended to check for additional types like you outlined, i.e. when "null" is defined as an additional type they could place null instead of undefined in the data object.

"name": {
  "type": [ "string", "null" ]
}

If you’d like to have this behavior without the modification of JSON Forms itself you can do this via custom renderers. You can basically reuse the existing renderers but modify the handleChange method given to them to place null instead of undefined in the data.

Of course the same support could be given to the two-element oneOf and anyOf constructs as they are semantically the same (as long as the entries are unqiue).

1 Like