多个If-Then-Else未验证JSON架构 [英] Multiple If-Then-Else not validating for JSON Schema

查看:7
本文介绍了多个If-Then-Else未验证JSON架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个程序,它接受一个JSON对象并根据输入的详细信息生成一个JSON模式文件。当我使用该程序为较小的JSON对象生成模式时,该模式正常工作并按预期进行验证。在此较小的架构中,只有一个If-Then-Else块。

但是,当我尝试生成一个使用几个If-Then-Else块的模式时,If-Then-Else验证似乎完全停止工作,并将允许任何内容通过。

为了更清楚起见,我将在下面发布一个示例。

JSON架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "question6-99": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "enum": ["Yes", "No"]
        }
      }
    },
    "question6-100": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "enum": ["Mr","Ms","Mrs","Miss","Dr","Rev","Sir","Lady","Lord","Prof", ""]
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "form_submission": {
      "type": "object",
      "properties": {
        "sections": {
          "type": "object",
          "properties": {
            "6": {
              "type": "object",
              "properties": {
                "questions": {
                  "type": "object",
                  "properties": {
                    "99": {
                      "$ref": "#/definitions/question6-99"
                    },
                    "100": {
                      "$ref": "#/definitions/question6-100"
                    }
                  },
                  "if": {
                    "properties": {
                      "99": {
                        "properties": {
                          "answer": {
                            "enum": [
                              "Yes"
                            ]
                          }
                        },
                        "required": [
                          "answer"
                        ]
                      }
                    },
                    "required": [
                      "100"
                    ]
                  },
                  "then": {
                    "properties": {
                      "100": {
                        "properties": {
                          "answer": {
                            "minLength": 1
                          }
                        }
                      }
                    }
                  },
                  "else": {
                    "properties": {
                      "100": {
                        "properties": {
                          "answer": {
                            "maxLength": 0
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "required": [
            "6"
          ]
        }
      }
    }
  }
}

正在验证JSON对象

{
  "form_submission": {
    "sections": {
      "1": {
        "questions": {
          "99": {
            "answer": "Yes",
          },
          "100": {
            "answer": "",
          }
        }
      }
    }
  }
}

对于上面的示例,如果使用模式来验证对象,则当问题99被回答为"是"时,问题100的答案必须得到回答。这可以正常工作。

但是,如果我随后尝试使用下面的架构,该架构对第二个JSON对象使用两个If-Then-Else块,则不会发生If-Then-Else验证。

我只是想知道我的架构代码结构是否有问题,使验证无法正常进行。

使用两个If-Then-Else的架构

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "question6-99": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string",
                    "minLength": 1,
                    "enum": ["Yes", "No"]
                }
            }
        },
        "question6-100": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string",
                    "enum": ["Mr", "Ms", "Mrs", "Miss", "Dr", "Rev", "Sir", "Lady", "Lord", "Prof", ""]
                }
            }
        },
        "question6-101": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string"
                }
            }
        }
    },
    "type": "object",
    "properties": {
        "form_submission": {
            "type": "object",
            "properties": {
                "sections": {
                    "type": "object",
                    "properties": {
                        "6": {
                            "type": "object",
                            "properties": {
                                "questions": {
                                    "type": "object",
                                    "properties": {
                                        "99": {
                                            "$ref": "#/definitions/question6-99"
                                        },
                                        "100": {
                                            "$ref": "#/definitions/question6-100"
                                        },
                                        "101": {
                                            "$ref": "#/definitions/question6-101"
                                        }
                                    },
                                    "required": ["99", "100", "101", "102", "103", "104", "105", "111"],
                                    "if": {
                                        "properties": {
                                            "99": {
                                                "properties": {
                                                    "answer": {
                                                        "enum": ["Yes"]
                                                    }
                                                },
                                                "required": ["answer"]
                                            }
                                        },
                                        "required": ["100"]
                                    },
                                    "then": {
                                        "properties": {
                                            "100": {
                                                "properties": {
                                                    "answer": {
                                                        "minLength": 1
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "else": {
                                        "properties": {
                                            "100": {
                                                "properties": {
                                                    "answer": {
                                                        "maxLength": 0
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "if": {
                                        "properties": {
                                            "99": {
                                                "properties": {
                                                    "answer": {
                                                        "enum": ["Yes"]
                                                    }
                                                },
                                                "required": ["answer"]
                                            }
                                        },
                                        "required": ["101"]
                                    },
                                    "then": {
                                        "properties": {
                                            "101": {
                                                "properties": {
                                                    "answer": {
                                                        "minLength": 1
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "else": {
                                        "properties": {
                                            "101": {
                                                "properties": {
                                                    "answer": {
                                                        "maxLength": 0
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "required": ["1"]
                }
            }
        }
    }
}

要验证的第二个架构

{
    "form_submission": {
        "id": "80035",
        "status": "Incomplete",
        "validated": true,
        "failure_reason": "",
        "sections": {

            "1": {
                "questions": {
                    "99": {
                        "answer": "Yes",
                        "web_validated": true,
                        "web_error_string": "",
                        "server_error_string": ""
                    },
                    "100": {
                        "answer": "",
                        "web_validated": true,
                        "web_error_string": "",
                        "server_error_string": ""
                    },
                    "101": {
                        "answer": "Yes",
                        "web_validated": true,
                        "web_error_string": "",
                        "server_error_string": ""
                    }
                },
                "name": "",
                "validated": true,
                "server_validated": true,
                "notes": ""
            }
        },
        "submitted_section_id": 11
    }
}

将Allof添加到架构

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "question6-99": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string",
                    "minLength": 1,
                    "enum": ["Yes", "No"]
                }
            }
        },
        "question6-100": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string",
                    "enum": ["Mr", "Ms", "Mrs", "Miss", "Dr", "Rev", "Sir", "Lady", "Lord", "Prof", ""]
                }
            }
        },
        "question6-101": {
            "type": "object",
            "properties": {
                "answer": {
                    "type": "string"
                }
            }
        }
    },
    "type": "object",
    "properties": {
        "form_submission": {
            "type": "object",
            "properties": {
                "sections": {
                    "type": "object",
                    "properties": {
                        "6": {
                            "type": "object",
                            "properties": {
                                "questions": {
                                    "type": "object",
                                    "properties": {
                                        "99": {
                                            "$ref": "#/definitions/question6-99"
                                        },
                                        "100": {
                                            "$ref": "#/definitions/question6-100"
                                        },
                                        "101": {
                                            "$ref": "#/definitions/question6-101"
                                        }
                                    },
                                    "required": ["99", "100", "101", "102", "103", "104", "105", "111"],
                                    "allOf": [
                                      {
                                        "if": {
                                            "properties": {
                                                "99": {
                                                    "properties": {
                                                        "answer": {
                                                            "enum": ["Yes"]
                                                        }
                                                    },
                                                    "required": ["answer"]
                                                }
                                            },
                                            "required": ["100"]
                                        },
                                        "then": {
                                            "properties": {
                                                "100": {
                                                    "properties": {
                                                        "answer": {
                                                            "minLength": 1
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        "else": {
                                            "properties": {
                                                "100": {
                                                    "properties": {
                                                        "answer": {
                                                            "maxLength": 0
                                                        }
                                                    }
                                                }
                                            }
                                        }},
                                      {
                                        "if": {
                                            "properties": {
                                                "99": {
                                                    "properties": {
                                                        "answer": {
                                                            "enum": ["Yes"]
                                                        }
                                                    },
                                                    "required": ["answer"]
                                                }
                                            },
                                            "required": ["101"]
                                        },
                                        "then": {
                                            "properties": {
                                                "101": {
                                                    "properties": {
                                                        "answer": {
                                                            "minLength": 1
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        "else": {
                                            "properties": {
                                                "101": {
                                                    "properties": {
                                                        "answer": {
                                                            "maxLength": 0
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                      }
                                    ]
                                }
                            }
                        }
                    },
                    "required": ["1"]
                }
            }
        }
    }
}

推荐答案

如果我们去掉复杂的部分,我认为问题就清楚了。

{
  "if": { ... },
  "then": { ... },
  "if": { ... },
  "then": { ... }
}

在JSON中,未定义重复键的值。其中一个if和一个then将被JSON分析器忽略。

您可以通过将if包装在allOf中来解决此问题。

{
  "allOf": [
    {
      "if": { ... },
      "then": { ... }
    },
    {
      "if": { ... },
      "then": { ... }
    }
  ]
}
最好始终将if/then/else包含在allOf中,即使您只有一个。这是因为根据定义,JSON对象是无序的。因此,某些工具可能会重新排列您的关键字,将ifthen分开,从而使架构难以破译。

{
  "definitions": {},
  "else": {},
  "if": {},
  "properties": {},
  "then": {},
  "type": "object",
}

这篇关于多个If-Then-Else未验证JSON架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆