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

Validate array content based on another enum value #414

Open
youssefabc opened this issue May 4, 2021 · 0 comments
Open

Validate array content based on another enum value #414

youssefabc opened this issue May 4, 2021 · 0 comments

Comments

@youssefabc
Copy link

youssefabc commented May 4, 2021

Hello everybody,

I have problem in my json schema,
I try to validate the content of array based on enum value.

Here my json schema:
`

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "motif": {
      "type": "object",
      "properties": {
        "subMotif": {
          "enum": [
            "mtf1",
            "mtf2",
			"mtf3"
          ]
        }
      },
      "required": [
        "subMotif"
      ]
    },
    "additionalData": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "cle": {
              "type": "string"
            },
            "valeur": {
              "type": "string"
            }
          },
          "required": [
            "cle",
            "valeur"
          ]
        }
      ]
    }
  },
  "anyOf": [
    {
      "if": {
        "properties": {
          "#/motif/subMotif": {
            "const": "mtf1"
          }
        }
      },
      "then": {
        "properties": {
          "additionalData": {
            "contains": {
              "properties": {
                "cle": {
                  "const": "key1"
                },
                "valeur": {
                  "minLength": 2
                }
              },
              "properties": {
                "cle": {
                  "const": "key2"
                },
                "valeur": {
                  "minLength": 3
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "#/motif/subMotif": {
            "const": "mtf2"
          }
        }
      },
      "then": {
        "properties": {
          "additionalData": {
            "contains": {
              "properties": {
                "cle": {
                  "const": "anotherKeyxx"
                },
                "valeur": {
                  "pattern": ""
                }
              },
              "properties": {
                "cle": {
                  "const": "anotherKeyyy"
                },
                "valeur": {
                  "pattern": ""
                }
              }
            }
          }
        }
      }
    }
  ],
  "required": [
    "motif"
  ]
}

`

this json it should be valid:

`

{
"motif": {
"subMotif": "mtf1"
},
"additionalData": [
{
"cle": "key1",
"valeur": "valeur1"
},
{
"cle": "key2",
"valeur": "aaa"
}
]
}

`,

`

{
"motif": {
"subMotif": "mtf2"
},
"additionalData": [
{
"cle": "anotherKeyxx",
"valeur": "valeur1"
},
{
"cle": "anotherKeyyy",
"valeur": "val"
}
]
}

`
This json is not valid:

`

{
  "motif": {
    "subMotif": "mtf1"
  },
  "additionalData": [
    {
      "cle": "anotherKeyxx",
      "valeur": "valeur1"
    },
    {
      "cle": "key2",
      "valeur": "valeur2"
    }
  ]
}

`

I need help to fix the schema.

Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant