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

Conditional Logic Based on Value Being Present in an Array #3906

Closed
1 task done
zmsmith opened this issue Oct 13, 2023 · 2 comments
Closed
1 task done

Conditional Logic Based on Value Being Present in an Array #3906

zmsmith opened this issue Oct 13, 2023 · 2 comments
Labels
needs triage Initial label given, to be assigned correct labels and assigned question

Comments

@zmsmith
Copy link

zmsmith commented Oct 13, 2023

Prerequisites

What theme are you using?

core

What is your question?

Given the If/Then/Else example (below for reference), if animal was an array, how could I write conditional logic to check if "Cat" appears in animal (as opposed to strict equality)?

{
  "type": "object",
  "properties": {
    "animal": {
      "enum": [
        "Cat",
        "Fish"
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "animal": {
            "const": "Cat"
          }
        }
      },
      "then": {
        "properties": {
          "food": {
            "type": "string",
            "enum": [
              "meat",
              "grass",
              "fish"
            ]
          }
        },
        "required": [
          "food"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "animal": {
            "const": "Fish"
          }
        }
      },
      "then": {
        "properties": {
          "food": {
            "type": "string",
            "enum": [
              "insect",
              "worms"
            ]
          },
          "water": {
            "type": "string",
            "enum": [
              "lake",
              "sea"
            ]
          }
        },
        "required": [
          "food",
          "water"
        ]
      }
    },
    {
      "required": [
        "animal"
      ]
    }
  ]
}
@zmsmith zmsmith added needs triage Initial label given, to be assigned correct labels and assigned question labels Oct 13, 2023
@magaton
Copy link

magaton commented Oct 13, 2023

Hi @zmsmith,
If you want to check whether a value is in the array, instead of:

"if": {
        "properties": {
          "animal": {
            "const": "Cat"
          }
        }
      }

you should say:

"if": {
                "properties": {
                  "animal": {
                    "contains": {
                      "const": "Cat"
                    }
                  }
                }
              }

@zmsmith
Copy link
Author

zmsmith commented Oct 13, 2023

Thank you!

@zmsmith zmsmith closed this as completed Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Initial label given, to be assigned correct labels and assigned question
Projects
None yet
Development

No branches or pull requests

2 participants