Help with default values

Hello!

I’m on 2.3.2 and can’t seem to get default values to appear. I notice that there are default values set in schema in this example but it is unused Layout Example - JSON Forms.

Upon loading, I’m met with empty fields that are not populated with default values,

Here is part of my schema written in yaml.

        nationality:
          default: US
          enum:
          - DE
          - IT
          - INDO
          - JP
          - US
          - RU
          type: string
        number_field:
          default: 6
          maximum: 10
          minimum: 5
          type: number
        rico_field:
          type: string
        since_sync:
          type: string
        test_field:
          default: default value
          type: string

And my component

        <JsonForms
            data={data}
            schema={schema}
            uischema={uischema}
            renderers={materialRenderers}
            cells={materialCells}
            onChange={({ errors, data }) => {console.log(data); updateData(errors, data)}}
        />

[original thread by Ricardo Guntur]

Hi @ricardo-guntur, we let Ajv handle the default values. As we require certain Ajv settings you can use the exported Ajv factory createAjv(options) and set useDefaults: true. The customized Ajv can then be passed into the JsonForms component, e.g.

const handleDefaultsAjv = createAjv({useDefaults: true});
// [...]
        <JsonForms
            data={data}
            schema={schema}
            uischema={uischema}
            renderers={materialRenderers}
            cells={materialCells}
            onChange={({ errors, data }) => {console.log(data); updateData(errors, data)}}
            ajv={handleDefaultsAjv}
        />
1 Like