Default Value not displaying

Hello @sdirix ,

First of all, thank you so much for building such an awesome library. I’m using jsonforms 3.0.0 in one of my project and it seems like there is a bug. Please have a look at following JSON schema and it’s output from jsonforms:

{
    "title": "oneOf Default Value",
    "type": "object",
    "oneOf": [
        {
            "title": "Tab 1",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_1"
                    ],
                    "default": "Tab_1",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        },
        {
            "title": "Tab 2",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_2"
                    ],
                    "default": "Tab_2",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        },
        {
            "title": "Tab 3",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_3"
                    ],
                    "default": "Tab_3",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        }
    ]
};

In above schmea, there are three tabs and each tab has type as required field and a default value. But for some reason only the first tab is displaying the default value. Rest two tabs are empty. And since it is readonly it is problematic that use cannot even change it by their own will.

Here is how tab 2 renders:
11 -Tab 2 - correct

(Here, the type field is empty - despite defining default value in the schema)

Can you please confirm if there is anything wrong in the schema of it’s a but from jsonforms.

Thank you so much again for all the good work and help :smiley:

Hi @Dhruw,

Thanks for the kind words.

The “problem” with the default keyword in JSON Schema is that there is no associated semantic meaning to it, so any framework/library is free to handle it how they see it fit, e.g. when and how to set the value.

In JSON Forms itself we don’t have any special functionality implemented regarding the default properties. However internally we use AJV which offers some (limited) default functionality, i.e. it will set default values during the validation process in case they are undefined. See here for how to enable this functionality in JSON Forms.
Having said that, I think defaults within a oneOf construct like you defined are exactly one of the use cases which is not supported by AJV. The reason is because it is impossible for AJV to know which oneOf actually applies to your data.

However there is a way to accomplish your goal: You can implement a custom oneOf renderer in JSON Forms and then on tab change (or however the renderer visualizes the oneOf selection) you can just set the default value in the object.

Unrelated to the question at hand: Your JSON Schema is rather unconvential, in the sense of that all oneOf entries are exactly the same and just differ in the default value. Was this done to illustrate the problem or is that an actual schema you’re using? If it’s the latter then I would like to suggest to replace the whole oneOf with a simple enum containing three options.

1 Like

Hi @sdirix,
a couple of months ago I opened this issue regarding faulty validation and defaults when using the OneOfRenderer.

You’ve mentioned above that the defaults issue could be solved by creating a custom OneOfRenderer that handles defaults. However I’m a bit lost on how I could accomplish that.
In the default implementation I can see that inside the newSelection() method there is already a call to createDefaultValue() from the jsonforms core lib.

I’m not sure which part of the OneOfRenderer I would need to tweak in order to get defaults and validation working (in a fully generic way).
Aren’t the defaults and validation also handled down the line by the child renderers in the DispatchRenderer?

I’d be glad if you could give me a hint on where to start tackling this problem.
Thanks so much in advance :smiley:

1 Like

Hi @Yekt,

Aren’t the defaults and validation also handled down the line by the child renderers in the DispatchRenderer?

Defaults are almost always ignored in JSON Forms as described above. Validation of the form is handled centrally and not per renderer, i.e. each change triggers a new validation run and the determined errors are then stored in the form-wide state. Each renderer then has access to all these errors and tries to determine which of them are relevant to it.

You already linked the OneOfRenderer implementation. So to customize it, you could just copy the OneOfRenderer but replace the the createDefaultValue call with an own implementation which respects default values.

For the determined errors, that’s handled within the bindings, e.g. here. To customize the behavior you could just inject('jsonforms') in your custom renderer, access the jsonforms.core.errors yourself and try to better determine which errors fit in your use case.


Note that we just recently merged functionality to actually respect the defaults when creating a new instance in an array, see #2206. This is a @jsonforms/core change, so when consuming a later version of @jsonforms/core you would already get the improvement into the stock OneOfRenderer. If your oneOf also uses references (i.e. $ref) then you need to hand over the root schema parameter too. If it doesn’t then you’re already set.

However the bad news is that we are dropping Vue 2 support with the new release, so there is no build of @jsonforms/vue2-vuetify available against @jsonforms/core containing this improvement. The easiest way for you would be to implement the custom renderer as outlined above and then just copy the new default handling logic over into your custom renderer.

@sdirix Thanks a lot for your fast and thorough reply :smiley:

To be honest, this isn’t bad news at all, since Vue2 reaches end of life in two weeks.
Is Vuetify3 support planned or is it only vanilla HTML? I know there is a branch for that, however Vuetify3 is not mentioned here (also some of those links are dead).

The Vuetify3 branch is the only one which we intend to further support. However as there are some known issues, this will be tagged with experimental or preview for the foreseeable future. You can already consume it as @jsonforms/vue-vuetify. Once we do the stable release we’ll announce the deprecation of the Vue 2 based packages and update the website.

Any contribution effort to polish the Vuetify 3 renderers is welcome!

1 Like