rjsf-team/react-jsonschema-form

oneOf + const is handled differently when nested in a property vs as a root object property

Open

#4,963 建立於 2026年2月19日

在 GitHub 查看
 (9 留言) (0 反應) (0 負責人)TypeScript (13,175 star) (2,136 fork)batch import
bughelp wanted

描述

Prerequisites

What theme are you using?

mantine

Version

6.3.1

Current Behavior

It's possible there's something I don't understand about JSON Schema or RJSF here, but this seems like a bug to me.

This is with the following feature flag:

experimental_defaultFormStateBehavior={{
  constAsDefaults: 'always',
}}

The first schema will have the behavior of enforcing food equals "fish" if animal equals "Cat". Even if the user uses the dropdown to select a different value, it forces it back to the const value. The second schema is essentially the same schema, except nested inside a property nested. The above behavior is no longer observed.

Validation is not broken, but the UX behavior changes.

Working playground with the schema below:

{
  "type": "object",
  "properties": {
    "animal": {
      "enum": [
        "Cat",
        "Fish"
      ]
    },
    "food": {
      "type": "string",
      "enum": [
        "meat",
        "grass",
        "fish"
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "animal": {
            "const": "Cat"
          }
        }
      },
      "then": {
        "properties": {
          "food": {
            "const": "fish"
          }
        },
        "required": [
          "food"
        ]
      }
    },
    {
      "required": [
        "animal"
      ]
    }
  ]
}

Bugged playground with the schema below:

{
  "type": "object",
  "properties": {
    "nested": {
      "type": "object",
      "properties": {
        "animal": {
          "enum": [
            "Cat",
            "Fish"
          ]
        },
        "food": {
          "type": "string",
          "enum": [
            "meat",
            "grass",
            "fish"
          ]
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "animal": {
                  "const": "Cat"
                }
              }
            },
            "then": {
              "properties": {
                "food": {
                  "const": "fish"
                }
              },
              "required": [
                "food"
              ]
            }
          },
          {
            "required": [
              "animal"
            ]
          }
        ]
      }
    }
  }
}

Expected Behavior

Consistent UX behavior whether the allOf is the root object or a nested object property.

Steps To Reproduce

Observable in the plaground via the two links above

Environment

- OS: Windows 11
- Node: 22.13.1
- npm: 11.7.0

Anything else?

No response

貢獻者指南

oneOf + const is handled differently when nested in a property vs as a root object property · rjsf-team/react-jsonschema-form#4963 | Good First Issue