How to allow null value to a key in JsonForms

I have created custom renderers in jsonforms and i want to set null to the key when there is no input from user, but we im going to set it as null in my custom renderer, jsonforms throwing me error such that null is not allowed to type string key

{
“instancePath”: “/header_content”,
“schemaPath”: “#/properties/header_content/type”,
“keyword”: “type”,
“params”: {
“type”: “string”
},
“message”: “must be string”,
“schema”: “string”,
“parentSchema”: {
“type”: “string”
},
“data”: null
}

1 Like

Hi @Mujahid,

If the property is typed as string then null is not a valid value. So instead of

"type": "string"

the schema should specify

"type": ["string", "null"]

in case that null values are allowed.

I have added the type as [“string”, “null”]
but still getting the same error, im using jsonforms v3.1.0

{
“instancePath”: “/attribute_category”,
“schemaPath”: “#/properties/attribute_category/enum”,
“keyword”: “enum”,
“params”: {
“allowedValues”: [
“ONE”,
“TWO”
]
},
“message”: “must be equal to one of the allowed values”,
“schema”: [
“ONE”,
“TWO”
],
“parentSchema”: {
“enum”: [
“ONE”,
“TWO”
],
“type”: [
“string”,
“null”
]
},
“data”: null
}

Hi @Mujahid,

This time the error is for an enum. An enum is a list of allowed values, so even if null is valid via the type, it’s still a validation error if null is not also in the list of allowed values. It’s just a different validation error.

So there it should look like this:

"type": ["string", "null"],
"enum": ["ONE", "TWO", null]

Note that these are not JSON Forms specific questions but just basic JSON Schema validation.

You can check via an online JSON Schema validator like this one whether your schema and data will produce a validation error. For more information about JSON Schema validation I can recommend this e-book.