webgme / svelte-jsonschema-form

A Svelte component for generating forms from JSON Schema.
MIT License
32 stars 2 forks source link

Flesh out `anyOf` support #9

Open romancow opened 1 year ago

romancow commented 1 year ago

Currently support for anyOf is a bit limited. I believe it's just a dropdown that switches child fields based on which "anyOf" option is selected. When a new one is selected, the previous set values persist. These should probably be "cleared out" whenever a new option is selected.

Actually, this would ideally be how the "oneOf" would work and "anyOf" would allow one to select multiple options and set the values for each of these (and clear out associated values when unselected). There are a few complications here to keep in mind - like there can be values for say, an object type, that are not part of the anyOf options. Also the different options could have overlapping values, where we probably want one field for this. Likewise you could have anyOfs without actual properties like outlined in issue #6 .

Probably want to look at how other similar libraries handle this (e.g. (react-jsonschema-form)[https://rjsf-team.github.io/react-jsonschema-form/] and (JSONForms)[https://jsonforms.io/]).

romancow commented 1 year ago

I think the best way to go about "full" anyOf support, would be to replace the current select dropdown with a similar "multiselect" dropdown. SMUI does not currently have a multiselect like this, so we'd have to create custom one, though I don't think this would be too hard (we're already patching the current select component). I've already created something similar for the webgme-taxonomy dashboard search filters.

I think then we could use the existing json-schema-merge-allof dependency to merge all the selected anyOfs with any properties. This would also address #6 . This "merged" schema" would only be present/referenced in the control itself, and not a change made to the schema object as a whole.

When an anyOf item is unselected, we would then recalculate the "merged" schema, then compare all the current property keys with the current data value, removing any keys in data nor present in the new schema.