anyOf not working

I’m using the react material JSON forms and in my JSON schema I have the following:

    "inputTable": {
        "description": "",
        "type": "object",
        "properties": {
			.
			.
			.
            "sourceTableType": {
                "description": "Whether the table is registered as an 'entity', or 'child' entity in Visual Investigator.",
                "type": "string",
                "enum": [
                    "entity",
                    "child"
                ]
            },
            "lastUpdatedAtTimeFieldName": {
                "description": "The name of the datetime field that records when the last change was made to the record in the Visual Investigator table.",
                "type": "string"
            }
			.
			.
			.
        },
        "anyOf": [
            {
                "properties": {
                    "sourceTableType": { "const": "entity" }
                },
                "required": ["lastUpdatedAtTimeFieldName"]
            }
        ],
        "required": [
            .
			.
			.
        ]
    }

where . . . indicates that there are other properties of my object that have been emitted to make the example more concise.

Essentially I’m trying to say that if sourceTableType === ‘entity’ then lastUpdatedAtTimeFieldName is required.

what happens when I use this schema is I get the following:

What am I doing wrong?

Hi @james-morris, this is what is happening here:

  • Because there is an anyOf in the JSON Schema, the MaterialAnyOfRenderer is taking over rendering the inputTable
  • The anyOf renderer outputs all properties “outside” of the anyOf as controls in a vertical layout.
  • The anyOf renderer also outputs a tabbed selection for each anyOf subschema. Here there is only one subschema, so also only one tab. The content of the tab is empty as we don’t generate controls for const properties.

So in case you would like to have a regular table I would suggest not using the anyOf keyword within your schema or reregistering the table renderer with a higher priority.

Overall the JSON Schema doesn’t have the desired effect. The schema has to adhere to the regular properties of inputTable as well as the only anyOf subschema which is defined. Therefore lastUpdatedAtTimeFieldName will always be required.

You’re probably looking for something like this:

{
  "type": "object",
  "properties": {
    "sourceTableType": {
      "type": "string",
      "enum": [
        "entity",
        "child"
      ]
    },
    "lastUpdatedAtTimeFieldName": {
      "description": "The name of the datetime field that records when the last change was made to the record in the Visual Investigator table.",
      "type": "string"
    }
  },
  "required": [],
  "if": {
    "properties": {
      "sourceTableType": {
        "const": "entity"
      }
    },
    "required": ["sourceTableType"]
  },
  "then": {
    "required": ["lastUpdatedAtTimeFieldName"]
  }
}

This will validate successfully for

{
  "sourceTableType": "child"
}

and

{
  "sourceTableType": "entity",
  "lastUpdatedAtTimeFieldName": "yesterday"
}

but not for

{
  "sourceTableType": "entity"
}

This schema will render like this by default
conditionalRequired

1 Like

That’s exactly what I wanted, thanks again Stefan!