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
538 stars 154 forks source link

anyOf without a sub-schema throws an error #364

Open patrick-dennis opened 1 year ago

patrick-dennis commented 1 year ago

First of all this library is awesome, thanks for all of your hard work.

I have a schema with an anyOf with the sole purpose of being used for validation, specifically validating that one or another property is present in the model. When VSJF tries to render this, the form is never created and I see this stack trace:

loggingService.js?63fe:29 TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at VueComponent.subSchemasConstProp (main.js?530e:2:42034)
    at Watcher.get (vue.esm.js?deef:4504:1)
    at Watcher.evaluate (vue.esm.js?deef:4606:1)
    at VueComponent.computedGetter [as subSchemasConstProp] (vue.esm.js?deef:4860:1)
    at VueComponent.renderObjectContainer (main.js?530e:2:49284)
    at Proxy.render (main.js?530e:2:134921)
    at Vue._render (vue.esm.js?deef:3578:1)
    at VueComponent.updateComponent (vue.esm.js?deef:4090:1)
    at Watcher.get (vue.esm.js?deef:4504:1)

Here is my schema:

{
  additionalProperties: false,
  anyOf: [
    {
      required: ["enabled"]
    },
    {
      required: ["second_enabled"]
    }
  ],
  description: "this is the description",
  properties: {
    enabled: {
      default: false,
      description: "enables/disables something",
      title: "Enabled",
      type: "boolean"
    },
    second_enabled: {
      default: false,
      description: "second enables/ disables something else",
      title: "Second Enable",
      type: "boolean"
    }
  },
  type: "object"
}

I've been able to reproduce this here: https://codepen.io/pdennistech/pen/BarWjGx

Looking at the source code based on the error, it appears that the function subSchemasConstProp() expects that the anyOf sub-schema will have a properties object and blows up when calling Object.keys() with a null object.

As a work around, I am going to investigate a different way of writing the json schema to achieve the same validation results.