Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Type Not Working With Enum #4422

Open
4 tasks done
aress31 opened this issue Dec 18, 2024 · 1 comment
Open
4 tasks done

Null Type Not Working With Enum #4422

aress31 opened this issue Dec 18, 2024 · 1 comment

Comments

@aress31
Copy link

aress31 commented Dec 18, 2024

Prerequisites

What theme are you using?

mui

Version

5.x

Current Behavior

When defining a property with an enum, e.g.:

{
  "schema": {
    "type": "object",
    "definitions": {
      "status": {
        "enum": ["active", "cancelled", "completed", "inactive", "pending"]
      }
    },
    "properties": {
      // --- SNIP ---
      "status": {
        "type": ["string", "null"],
        "$ref": "#/definitions/status"
      },
      // --- SNIP ---
    }
  },
  "uiSchema": {
    // --- SNIP ---
  }
}

Null values are still not accepted, see:

image

Expected Behavior

No response

Steps To Reproduce

No response

Environment

Latest everything.

Anything else?

No response

@aress31 aress31 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Dec 18, 2024
@nickgros
Copy link
Contributor

nickgros commented Jan 13, 2025

You can get null to appear as an option if you add null to the `"enum" property:

{
  "type": "object",
  "definitions": {
    "status": {
      "enum": [
        null,
        "active",
        "cancelled",
        "completed",
        "inactive",
        "pending"
      ]
    }
  },
  "properties": {
    "status": {
      "type": [
        "string",
        "null"
      ],
      "$ref": "#/definitions/status"
    }
  }
}

If you want to hide the null title, you can make null the default value for the property and either use enumNames in the uiSchema, or you should be able to reconstruct the enum using oneOf and const like in the below schema. However it looks like there is a bug where we don't use the title if it's an empty string, which we can track in #4448.

{
  "type": "object",
  "definitions": {
    "status": {
      "oneOf": [
        {
          "const": null,
          "title": ""
        },
        {
          "const": "active"
        },
        {
          "const": "cancelled"
        },
        {
          "const": "completed"
        },
        {
          "const": "inactive"
        },
        {
          "const": "pending"
        }
      ],
      "default": null
    }
  },
  "properties": {
    "status": {
      "type": [
        "string",
        "null"
      ],
      "$ref": "#/definitions/status"
    }
  }
}

We can discuss possible improvements & customizations to this functionality in your other issue #4421

@nickgros nickgros added question awaiting response and removed bug needs triage Initial label given, to be assigned correct labels and assigned labels Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants