koumoul-dev / vuetify-jsonschema-form

Create beautiful and low-effort forms that output valid data. Published on npm as @koumoul/vjsf.
https://koumoul-dev.github.io/vuetify-jsonschema-form/latest/
MIT License
548 stars 155 forks source link

wrong behavior with required fields of non-required nested objects #230

Open MOHACGCG opened 3 years ago

MOHACGCG commented 3 years ago

Issue: A model should be valid with an undefined optional nested obj with required fields (i.,e {stringProp:"a"} is a valid model for the schema below).

Feature request: Is it possible to remove an optional nested object from the model? in the schema below remove the nestedObj (i.e. set it to undefined). The array type already allows removal of items, so this is to support the same behavior to remove objects.

{
  "type": "object",
  "properties": {
    "required":["stringProp"]
    "stringProp": {
      "type": "string",
      "title": "I\"m a string",
      "description": "This description is used as a help message."
    },
    "nestedObj": {
      "type": "object",
      "properties": {
        "num": {
          "type": "number"
        },
        "str": {
          "type": "string"
        }
      },
      "required": ["num", "str"]
    }
  }
}
albanm commented 3 years ago

I am aware of this problem and I need to think about it. Initializing an empty object is built kinda deep in the way the lib works (necessary to bind the children properties on it). But the implementation of model binding and reactivity has changed quite a lot in 1.25 beta, maybe it has become easier to fix.

MOHACGCG commented 3 years ago

I have schema workaround for now (use a toggle switch) to turn the object on and off but would be nice to have this built in.

"allOf": [
        {
          "allOf": [
            {
              "properties": {
                "!__ nestedObj_toggle": {
                  "title": "NestedObj,
                  "type": "boolean",
                  "x-display": "switch",
                  "default": false
                }
              },
              "if": { "properties": { "!__ nestedObj_toggle": { "const": true } } },
              "then": {
                "properties": {
                  "nestedObj": { "$ref": "#/$defs/nestedObj" }
                }
              },
              "else": {}
            }
          ]
        },
...]