onChange-behaviour & validation in a simple example

onChange-behaviour & validation in a simple example

I just started using JSON-Forms 3.2.1 (vue-vanilla) and Vue 3.4.15 and “<script setup>”. Beside other topics I was looking at the event-handling while using the form (type in / change form content) and handling of errors and experienced a behaviour I don’t understand. It might be this is by design and I need to take care of something I’m not aware of but for me it’s hard to distinguish this right now.

I compiled this sample for you:

Script-code that feeds…

<script setup lang="ts">
import { JsonForms } from '@jsonforms/vue'
import type { JsonFormsChangeEvent } from '@jsonforms/vue'
import { vanillaRenderers } from '@jsonforms/vue-vanilla'

const schema = JSON.parse(`{
  "properties": {
    "i_lastname": {
      "title": "Lastname",
      "type": "string",
      "default": "Mustermann",
      "minLength": 3
    }
  }
}`)

const uischema = JSON.parse(`{
  "type": "HorizontalLayout",
  "elements": [
    {
      "type": "VerticalLayout",
      "elements": [
        {
          "type": "Control",
          "scope": "#/properties/i_lastname"
        }
      ]
    }
  ]
}`)

let data = {
  i_lastname: ''
}

const renderers = Object.freeze([
  ...vanillaRenderers
])
function  onChange(event: JsonFormsChangeEvent) {
  data = event.data
  console.log('onChange -> event: ', event)
}
</script>

…the template:

<template>
  <div>
    <h1>JSON-Forms with Vue 3.4 and "&lt;script setup&gt;"</h1>
  </div>
  <div class="myform">
    <form  method="post" enctype="multipart/form-data">
      <json-forms
        :data="data"
        :renderers="renderers"
        :schema="schema"
        :uischema="uischema"
        @change="onChange"
      />
    </form>
  </div>
</template>

Questions:

  1. When I load the “page” there’s already the onChange() event fired and the naturally empty form is validated. This of course results in the error-message “must NOT have fewer than 3 characters”. When thinking about form&user-interaction there was no change so far. Is this something I need to handle myself because it is by design? As long as I programmed form-validations myself I used events on form-field-level like “onkeyup” to trigger validations which makes much sense looking at what a user actually does using the form. Could or should I get into how/when JSON Forms triggers the validation to change the trigger or is there simply something I get wrong?
    The data the event contains looks fine, see the screenshot in the bottom.

  2. So I enter letters to the field and as the string-component loses the focus the validation runs and the error disappears. Again: Requiring to lose the focus seems inappropriate.
    Again: The data the event contains looks fine, see screenshot see the screenshot in the bottom.

  3. Now I return to the text input and empty it, i.e. I delete the content and leave it. Result: The event.data-object is empty, no “event.data.i_lastname”-attribute anymore and especially NO error. How that?

Before I post the console-output to demonstrate the steps above I’d like to kindly ask you to give me some advice.

Regards,
Ruediger.

Hi @Ruediger,

Welcome to the forum :slight_smile:

Yes, this is by design. JSON Schema based validation is non-trivial, therefore we delegate this to AJV. Initially, and on every change, we validate the whole data object against the JSON Schema. AJV uses a code generation mechanism which results in very fast validations. Rendering itself already takes much more time.

Simple JSON Schema validations (like maxLength, pattern etc.) could be implemented this way. So if you only use very simple JSON Schemas you could turn off JSON Forms’ built-in validation and roll your own in custom renderers.

Note that JSON Forms has some built-in (form-wide) error customization via validationMode. You can find the docs here. Developers typically use this to implement a “show-all-errors-on-submit” behavior.

A field-level based error display could be implemented via custom renderers. Each renderer could maintain a “touched” state and simply don’t show errors until it was touched by the user. See here for a community PR implementing this behavior for the vue-vuetify renderer set.

There are many different approaches to form behavior. From reacting to every key press up to rendering additional checkmark buttons to actually change a value.

JSON Forms is very flexible here, therefore our renderer sets are also implemented in various ways. For vue-vanilla we settled on @change. You can use custom renderers (for example by copying the existing ones) and then bind to @input instead.

We implemented our string renderers so that their associated attribute is removed when their input is cleared, see here. This approach plays well with JSON Schema’s required validations.

The reason you don’t see an error anymore in the UI is because in JSON Schema, a validation specified for an undefined value never applies. As the attribute is gone, the minLength no longer applies.

To see an error here you can use the required property on the parent object like this:

  "properties": {
    "i_lastname": {
      "title": "Lastname",
      "type": "string",
      "default": "Mustermann",
      "minLength": 3
    }
  },
  "required": ["i_lastname"]

I can recommend hands-on JSON Schema validators like this one to explore JSON Schema’s validation behavior.

If you would like to see a different string input behavior, like storing an empty string or null into the data when cleared, then you can achieve this via custom renderers.


Note that writing custom renderers is not an exception but rather the usual way of leveraging JSON Forms for more advanced developers who require specific behaviors. The off-the-shelf renderers are used as a starting point and then exchanged over time.

Hello @sdirix,

thank you for your long reply.

Taking into account my needs and situation again and again I decided not to proceed with your toolset. I’m not able to get an understanding of how your software works by just reading your code (the docs don’t suffice) and I’m stopped by errors as soon as I try to get deeper. After cloning your Github-repo I don’t even get “/packages/vue-vanilla/” running to have look at running sample with the option to see code on a certain (live) state, to debug, to observe and to learn.
So: Cool tool but not for me right now :slight_smile:

Regards,
Rüdiger.

Hi @Ruediger,

That’s sad to hear. If you want to, you can report any issue you encountered when going trough the developer setup so we can improve the situation for other potential adopters.

Note that developers usually aren’t required to compile JSON Forms from scratch. It should be possible to inspect and debug the JSON Forms sources even when they’re only consumed as dependencies.

Thanks for the feedback!

Hi @sdirix,

I did step by step what is written in the READMEs, first jsonforms/README.md at master · eclipsesource/jsonforms · GitHub and second jsonforms/packages/vue-vanilla/README.md at master · eclipsesource/jsonforms · GitHub. If you or a colleague may invest 10 minutes you should see what I saw.

Interesting to read your hint in your second paragraph. Just - how? :slight_smile: No, the question was not serious as it is like with your tool-set: I am not (yet) part of the target group of this tool-set.

Best, Rüdiger.