postman 如何在某些条件下验证JSON

8ulbf1ek  于 2023-10-18  发布在  Postman
关注(0)|答案(2)|浏览(139)

有一些json。

{
  "headerInfo": {
    "id": 50
  "characteristics": [
    {
      "id": "123456789",
      "type": "float",
      "multiValued": false,
      "numberDecimals": 6,
      "length": 10,
      "caseSensitive": false,
      "entryFieldMask": "_,___.______",
      "unitOfMeasure": "P1",
      "additionalValues": true
    },
    {
      "id": "15151511",
      "type": "float",
      "multiValued": false,
      "numberDecimals": 0,
      "possibleValues": [
        {
          "valueLow": "60",
          "intervalType": "1"
        },
        {
          "valueLow": "80",
          "intervalType": "1"
        }
      ],
      "length": 3,
      "caseSensitive": false,
      "entryFieldMask": "___",
      "unitOfMeasure": "P1",
      "additionalValues": false
    },
    {
      "id": "489481561",
      "type": "string",
      "multiValued": false,
      "possibleValues": [
        {
          "id": "A"
        }
        {
          "id": "§"
        }
      ],
      "length": 1,
      "caseSensitive": true,
      "entryFieldMask": "",
      "additionalValues": false,
      "reference": {
        "name": "ghnfgjghjghk",
        "writable": false
      }
    },
    {
      "id": "4616616161",
      "type": "date",
      "multiValued": false,
      "length": 10,
      "caseSensitive": false,
      "entryFieldMask": "DD.MM.YYYY",
      "additionalValues": true,
      "reference": {
        "name": "bfhwbvbjewbv",
        "writable": false
      }
    }
  ]
}

如何使用以下条件生成json schema:

  • “reference”和“possibleValues”属性是可选的。
  • 如果“type”属性等于“float”值,则需要“numberDecimals”属性,否则不需要。

我尝试使用anyOf实现这一点,但在这种情况下,anyOf中的所有内容都没有被正确验证,尽管在postman中验证是成功的。

2exbekwf

2exbekwf1#

Jeremy的回答很有帮助,但还需要进一步完善,即在oneOf中添加“required”属性。以下是最终结果:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "headerInfo": {
            "properties": {
                "id": {
                    "type": "number"
                }
            }
        },
        "characteristics": {
            "type": "array",
            "uniqueItems": true,
            "items": {
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string"
                    },
                    "multiValued": {
                        "type": "boolean"
                    },
                    "numberDecimals": {
                        "type": "number"
                    },
                    "length": {
                        "type": "number"
                    },
                    "caseSensitive": {
                        "type": "boolean"
                    },
                    "entryFieldMask": {
                        "type": "string"
                    },
                    "unitOfMeasure": {
                        "type": "string"
                    },
                    "additionalValues": {
                        "type": "boolean"
                    },
                    "reference": {
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "writable": {
                                "type": "boolean"
                            }
                        }
                    },
                    "possibleValues": {
                        "type": "array",
                        "uniqueItems": true,
                        "items": {
                            "oneOf": [{
                                    "required": [
                                        "id"
                                    ],
                                    "properties": {
                                        "id": {
                                            "type": "string"
                                        }
                                    }
                                },
                                {
                                    "required": [
                                        "valueLow",
                                        "intervalType"
                                    ],
                                    "properties": {
                                        "valueLow": {
                                            "type": "string"
                                        },
                                        "intervalType": {
                                            "type": "string"
                                        }
                                    }
                                }
                            ]
                        }
                    }
                },
                "if": {
                    "properties": {
                        "type": {
                            "const": "float"
                        }
                    }
                },
                "then": {
                    "required": [
                        "numberDecimals"
                    ]
                },
                "required": [
                    "id",
                    "type",
                    "multiValued",
                    "length",
                    "caseSensitive",
                    "entryFieldMask",
                    "additionalValues"
                ]
            }
        }
    }
}
7eumitmz

7eumitmz2#

您可以将if, then语法用于JSON Schema draft-07或更新版本。
jist是你定义整个模式,然后在你想要定义required属性的对象模式中,你添加if, then语法来检查值是否匹配条件,然后分配then约束。
在你的例子中,我们检查iftype的常数值是float,如果为真,则为required:["numberDecimals"]

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "headerInfo": {
      "properties": {
        "id": {
          "type": "number"
        }
      }
    },
    "characteristics": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "multiValued": {
            "type": "boolean"
          },
          "numberDecimals": {
            "type": "number"
          },
          "length": {
            "type": "number"
          },
          "caseSensitive": {
            "type": "boolean"
          },
          "entryFieldMask": {
            "type": "string"
          },
          "unitOfMeasure": {
            "type": "string"
          },
          "additionalValues": {
            "type": "boolean"
          },
          "reference": {
            "properties": {
              "name": {
                "type": "string"
              },
              "writable": {
                "type": "boolean"
              }
            }
          },
          "possibleValues": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "oneOf": [
                 {
                   "properties": {
                      "id": {
                        "type": "string"
                      }
                   }
                 },
                 {
                   "properties": {
                      "valueLow": {
                         "type": "string"
                      },
                      "intervalType": {
                         "type": "string"
                      }
                   }
                 }
               ]
            }
          }
        },
        "if": {
          "properties": {
            "type": {
              "const": "float"
            }
          }
        },
        "then": {
          "required": [
            "numberDecimals"
          ]
        },
        "required": [
          "id",
          "type",
          "multiValued",
          "length",
          "caseSensitive",
          "entryFieldMask",
          "additionalValues"
        ]
      }
    }
  }
}

相关问题