JSON 架构 oneOf 位于 allOf 中

问题描述 投票:0回答:1

大家好,我正在制作一个 JSON 模式,我正在使用 https://www.jsonschemavalidator.net/https://jsonschema.dev/ 来验证它们,两者都标记了我同样的问题,并且不知道出了什么问题

这是我正在使用的 JSON

{
  "Cdtr": {
    "Id": {
      "PrvtId": {
        "Othr": {
          "Id": "123456"
          "SchmeNm": {
            "Cd": "NIDN"
          }
        }
      }
    }
  }
}

这是 JSON 架构


    {
    "allOf": [
        {
            "if": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "SchmeNm": {
                                                        "Cd": {
                                                            "enum": [
                                                                "NIDN",
                                                                "CCPT",
                                                                "ANRU"
                                                            ]
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "then": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "Id": {
                                                        "maxLength": 8,
                                                        "minLength": 6,
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "SchmeNm": {
                                                        "Cd": {
                                                            "const": "CUST"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "then": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "Id": {
                                                        "minLength": 6,
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "SchmeNm": {
                                                        "Cd": {
                                                            "const": "TXID"
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "then": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "Id": {
                                                        "oneOf": [
                                                            {
                                                                "maxLength": 8,
                                                                "minLength": 8
                                                            },
                                                            {
                                                                "maxLength": 11,
                                                                "minLength": 11
                                                            }
                                                        ],
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ],
    "description": "PERU C2C BANK",
    "properties": {
        "Cdtr": {
            "properties": {
                "Id": {
                    "properties": {
                        "PrvtId": {
                            "properties": {
                                "Othr": {
                                    "properties": {
                                        "Id": {
                                            "pattern": "^[0-9]+$",
                                            "type": "string"
                                        },
                                        "SchmeNm": {
                                            "properties": {
                                                "Cd": {
                                                    "enum": [
                                                        "NIDN",
                                                        "CCPT",
                                                        "TXID",
                                                        "ANRU",
                                                        "CUST"
                                                    ],
                                                    "maxLength": 4,
                                                    "minLength": 4,
                                                    "type": "string"
                                                }
                                            },
                                            "required": [
                                                "Cd"
                                            ]
                                        }
                                    },
                                    "required": [
                                        "Id",
                                        "SchmeNm"
                                    ]
                                }
                            },
                            "required": [
                                "Othr"
                            ]
                        }
                    },
                    "required": [
                        "PrvtId"
                    ]
                }
            },
            "required": [
                "Id"
            ]
        }
    },
    "required": [
        "Cdtr"
    ]
}

这种情况在第一个 IF 的基础上应该是“有效的”,但我在最后一次验证时收到错误

编辑: 架构版本: “$schema”:“http://json-schema.org/draft-07/schema#”,

json jsonschema json-schema-validator
1个回答
0
投票

您在定义

properties
if
SchmeNm
 语句中遗漏了一些 
Cd

定义
{
            "if": {
                "properties": {
                    "Cdtr": {
                        "properties": {
                            "Id": {
                                "properties": {
                                    "PrvtId": {
                                        "properties": {
                                            "Othr": {
                                                "properties": {
                                                    "SchmeNm": {
                                                        "properties": {  <<--- this was missing on all of your conditional statements. 
                                                            "Cd": {
                                                                "const": "TXID"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

修复该问题后,您的实例通过了

{
  keyword: 'https://json-schema.org/evaluation/validate',
  absoluteKeywordLocation: 'https://www.stackoverflow.com/schema/cdtr#',
  instanceLocation: '#',
  valid: true,
  errors: []
}
© www.soinside.com 2019 - 2024. All rights reserved.