Json schema validation not working with multiple if else conditions

Hi I am pretty sure I am doing something wrong with my json schema, my usecase is that I add some elements to a table using documents and in this case the elements are added through name, value key pair. Now if the value of name is ‘name’ then it’s value pair should follow a regex validation, except for 1 particular scenario if the tableName where these are being added is header, in that case it should not follow this regex pattern.

my schema is ->

{
    "properties": {
        "elements": {
            "type": "array",
            "items": {
                "$ref": "#arrayElement"
            },
            "minItems": 1
        }
    },
"definitions": {
        "arrayElement": {
            "$id": "#arrayElement",
            "type": "object",
            "required": ["tableName"],
            "properties": {
                "tableName": {
                    "type": "string"
                },
                "elms": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "required": ["name", "value"],
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "value": {
                                "anyOf": [
                                    {
                                        "type": "string"
                                    },
                                    {
                                        "type": "number"
                                    },
                                    {
                                        "type": "boolean"
                                    }
                                ]
                            }
                        }
                    },
                    "minItems": 0
                },
                "ele2": {
                    "type": "object",
                    "$ref": "#objectElement"
                },
                "ele3": {
                    "type": "array",
                    "items": {
                        "$ref": "#arrayElement"
                    },
                    "minItems": 0
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "elms": {
                                "items": {
                                    "properties": {
                                        "name": {
                                            "const": "name"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "then": {
                        "allOf": [
                            {
                                "if": {
                                    "properties": {
                                        "tableName": {
                                            "const": "header"
                                        }
                                    }
                                },
                                "then": {
                                    "properties": {
                                        "elms": {
                                            "items": {
                                                "properties": {
                                                    "value": {
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                "else": {
                                    "properties": {
                                        "elms": {
                                            "items": {
                                                "properties": {
                                                    "value": {
                                                        "type": "string",
                                                        "pattern": "^[A-Za-z][A-Za-z0-9_.#/-]*$"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    },
                    "else": {
                        "properties": {
                            "elms": {
                                "items": {
                                    "properties": {
                                        "value": {
                                            "anyOf": [
                                                {
                                                    "type": "string"
                                                },
                                                {
                                                    "type": "number"
                                                },
                                                {
                                                    "type": "boolean"
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

Now this payload is passing through which should fail due to regex validation on name

"elements": [
        {
            "tableName": "row",
            "elms": [
                {
                    "name": "name",
                    "value": "__________________________"
                },
                {
                    "name": "format",
                    "value": "string"
                },
                {
                    "name": "length",
                    "value": 15
                },
                {
                    "name": "active",
                    "value": true
                }
            ]
        }
    ]

Thanks in Advance.

EDIT: To debug more I removed the condition on tableName all together just to see, if the basic condition on name is working or not, seems like that itself is not working. Could it be that the way I have represented properties.elms.items.properties.name is incorrect?

{
  "properties": {
    "elements": {
      "type": "array",
      "items": {
        "$ref": "#arrayElement"
      },
      "minItems": 1
    }
  },
  "definitions": {
    "arrayElement": {
      "$id": "#arrayElement",
      "type": "object",
      "required": ["tableName"],
      "properties": {
        "tableName": {
          "type": "string"
        },
        "elms": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["name", "value"],
            "properties": {
              "name": {
                "type": "string"
              },
              "value": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "number"
                  },
                  {
                    "type": "boolean"
                  }
                ]
              }
            }
          },
          "minItems": 0
        },
        "ele2": {
          "type": "object",
          "$ref": "#objectElement"
        },
        "ele3": {
          "type": "array",
          "items": {
            "$ref": "#arrayElement"
          },
          "minItems": 0
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "elms": {
                "items": {
                  "properties": {
                    "name": {
                      "const": "name"
                    }
                  }
                }
              }
            }
          },
          "then": {
            "properties": {
              "elms": {
                "items": {
                  "properties": {
                    "value": {
                      "type": "string",
                      "pattern": "^[A-Za-z][A-Za-z0-9_.#/-]*$"
                    }
                  }
                }
              }
            }
          },
          "else": {
            "properties": {
              "elms": {
                "items": {
                  "properties": {
                    "value": {
                      "anyOf": [
                        {
                          "type": "string"
                        },
                        {
                          "type": "number"
                        },
                        {
                          "type": "boolean"
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

EDIT -> Schema after Jason’s suggestion.

"allOf": [
                {
                    "if": {
                        "properties": {
                            "tableName": {
                                "not": { "const": "header" }
                            },
                            "elms": {
                                "type": "array",
                                "items": {
                                    "type": "object",
                                    "properties": {
                                        "name": { "const": "name" }
                                    },
                                    "required": ["name"]
                                }
                            }
                        },
                        "required": ["tableName", "elms"]
                    },
                    "then": {
                        "properties": {
                            "elms": {
                                "items": {
                                    "properties": {
                                        "value": {
                                            "type": "string",
                                            "pattern": "^[A-Za-z][A-Za-z0-9_.#/-]*$"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            ]

Your conditionals are way more complicated than they need to be.

Now if the value of name is ‘name’ then it’s value pair should follow a regex validation, except for 1 particular scenario if the tableName where these are being added is header, in that case it should not follow this regex pattern.

You can express this condition as a single schema.

{
  "type": "object",
  "properties": {
    "tableName": {
      "not": { "const": "header" }
    },
    "elms": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": { "const": "name" }
        },
        "required": ["name"]
      }
    }
  },
  "required": ["tableName", "elms"]
}

That’s your if schema. You’re then schema is correct. You don’t need else. No need for nesting.

4

I managed to do it by segregating the if conditions.

Would like to thank Jason for guiding me to the solution.

The “if” and “then” clauses seem to check both the tableName and the elms array, but since I am working with arrays and objects within arrays, I need to apply conditions individually inside the items definition for elms.

{
  "properties": {
    "elements": {
      "type": "array",
      "items": {
        "$ref": "#arrayElement"
      },
      "minItems": 1
    }
  },
  "definitions": {
    "arrayElement": {
      "$id": "#arrayElement",
      "type": "object",
      "required": ["tableName"],
      "properties": {
        "tableName": {
          "type": "string"
        },
        "elms": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["name", "value"],
            "properties": {
              "name": {
                "type": "string"
              },
              "value": {
                "anyOf": [
                  {
                    "type": "string"
                  },
                  {
                    "type": "number"
                  },
                  {
                    "type": "boolean"
                  }
                ]
              }
            }
          },
          "minItems": 0
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "tableName": {
                "not": { "const": "header" }
              }
            }
          },
          "then": {
            "properties": {
              "elms": {
                "items": {
                  "if": {
                    "properties": {
                      "name": { "const": "name" }
                    },
                    "required": ["name"]
                  },
                  "then": {
                    "properties": {
                      "value": {
                        "type": "string",
                        "pattern": "^[A-Za-z][A-Za-z0-9_.#/-]*$"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật