【问题标题】:How to write a json schema using oneof for this json data如何为此 json 数据使用 oneof 编写 json 模式
【发布时间】:2022-08-03 23:18:37
【问题描述】:

JSON数据

{
  \"alert_details\": {
    \"event_date\": \"2021-04-26T12:30:80Z\",
    \"camount\": \"789\",
    \"profile\": {
        \"name\": {
           \"first_name\": \"xxxx\",
           \"last_name\": \"xxxx\",
           \"middle_name\": \"xxx\"
        }
    },
    \"check_cash_date\": \"2021-04-26\",
    \"profile_address\": {
      \"name\": \"xxxxx\",
      \"street_address\": \"xxxxxx\",
      \"city\": \"xxxx\",
      \"state\": \"CA\",
      \"zip_code\": \"90021\"
    },
   \"alert_id\": {
      \"id\": \"abc123\",
      \"subject\": \"abc123\",
      \"abc_id\": \"abc123\"
    }
  },
  \"alert_sub_type\": \"check change\",
  \"alert_type\": \"change\",
  \"provider\": \"ABCD\",
  \"view_date\": \"2020-11-03T10:15:30Z\",
  \"status\": \"Read\"
}

{
  \"alert_details\": {
    \"event_date\": \"2020-11-03T10:15:30Z\",
    \"account_number\": \"*********xxx\",
    \"check_start_number\": \"2\",
    \"myprofile\": {
        \"name\": {
           \"first_name\": \"xxxx\",
           \"last_name\": \"xxxx\",
           \"middle_name\": \"M\"
        }
    },
    \"order_shipped_date\": \"2021-04-23\",
    \"myprofile_address\": {
      \"name\": \"xxxxx\",
      \"street_address\": \"xxxxx\",
      \"city\": \"xxxx\",
      \"state\": \"xxxx\",
      \"zip_code\": \"90021\"
    },
    \"quantity_ordered\": \"12\",
    \"alert_id\": {
      \"id\": \"abc123\",
      \"subject\": \"abc123\",
      \"abc_id\": \"abc123\"
    }
  },
  \"alert_sub_type\": \"review Check\",
  \"alert_type\": \"review\",
  \"provider\": \"abcd\",
  \"view_date\": \"2020-11-03T10:15:30Z\",
  \"status\": \"Read\"
}

不明白如何使用一个并使用此有效负载创建模式,我是使用 oneof 编写模式的新手,我使用 oneof 编写了此模式:

  {
    \"description\" : \"schema validating people and vehicles\",
    \"type\" : \"object\",
    \"oneOf\": [
            { \"$ref\":\"#/definitions/checkchange\" },
            { \"$ref\":\"#/definitions/reviewcheck\" }
        ],
      
     \"definitions\": {
       \"checkchange\":{
        \"type\": \"object\",
        \"properties\": {
            \"alert_details\": {
                \"$ref\": \"#/definitions/AlertDetails\"
            },
            \"alert_sub_type\": {
                \"type\": \"string\"
            },
            \"alert_type\": {
                \"type\": \"string\"
            },
            \"provider\": {
                \"type\": \"string\"
            },
            \"view_date\": {
                \"type\": \"string\",
                \"format\": \"date-time\"
            },
            \"status\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"alert_details\",
            \"alert_sub_type\",
            \"alert_type\",
            \"provider\",
            \"status\",
            \"view_date\"
        ],
        \"title\": \"Welcome4\"
    },
    \"AlertDetails\": {
        \"type\": \"object\",
        \"properties\": {
            \"event_date\": {
                \"type\": \"string\"
            },
            \"camount\": {
                \"type\": \"string\",
                \"format\": \"integer\"
            },
            \"profile\": {
                \"$ref\": \"#/definitions/Profile\"
            },
            \"check_cash_date\": {
                \"type\": \"string\",
                \"format\": \"date\"
            },
            \"profile_address\": {
                \"$ref\": \"#/definitions/ProfileAddress\"
            },
            \"alert_id\": {
                \"$ref\": \"#/definitions/AlertID\"
            }
        },
        \"required\": [
            \"alert_id\",
            \"camount\",
            \"check_cash_date\",
            \"event_date\",
            \"profile\",
            \"profile_address\"
        ],
        \"title\": \"AlertDetails\"
    },
    \"AlertID\": {
        \"type\": \"object\",
        \"properties\": {
            \"id\": {
                \"type\": \"string\"
            },
            \"subject\": {
                \"type\": \"string\"
            },
            \"abc_id\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"abc_id\",
            \"id\",
            \"subject\"
        ],
        \"title\": \"AlertID\"
    },
    \"Profile\": {
        \"type\": \"object\",
        \"properties\": {
            \"name\": {
                \"$ref\": \"#/definitions/Name\"
            }
        },
        \"required\": [
            \"name\"
        ],
        \"title\": \"Profile\"
    },
    \"Name\": {
        \"type\": \"object\",
        \"properties\": {
            \"first_name\": {
                \"type\": \"string\"
            },
            \"last_name\": {
                \"type\": \"string\"
            },
            \"middle_name\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"first_name\",
            \"last_name\",
            \"middle_name\"
        ],
        \"title\": \"Name\"
    },
    \"ProfileAddress\": {
        \"type\": \"object\",
        \"properties\": {
            \"name\": {
                \"type\": \"string\"
            },
            \"street_address\": {
                \"type\": \"string\"
            },
            \"city\": {
                \"type\": \"string\"
            },
            \"state\": {
                \"type\": \"string\"
            },
            \"zip_code\": {
                \"type\": \"string\",
                \"format\": \"integer\"
            }
        },
        \"required\": [
            \"city\",
            \"name\",
            \"state\",
            \"street_address\",
            \"zip_code\"
        ],
        \"title\": \"ProfileAddress\"
    },
       \"reviewcheck\":{
        \"type\": \"object\",
        \"properties\": {
            \"alert_details\": {
                \"$ref\": \"#/definitions/AlertDetails\"
            },
            \"alert_sub_type\": {
                \"type\": \"string\"
            },
            \"alert_type\": {
                \"type\": \"string\"
            },
            \"provider\": {
                \"type\": \"string\"
            },
            \"view_date\": {
                \"type\": \"string\",
                \"format\": \"date-time\"
            },
            \"status\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"alert_details\",
            \"alert_sub_type\",
            \"alert_type\",
            \"provider\",
            \"status\",
            \"view_date\"
        ]
    },
    \"AlertDetails\": {
        \"type\": \"object\",
        \"properties\": {
            \"event_date\": {
                \"type\": \"string\",
                \"format\": \"date-time\"
            },
            \"account_number\": {
                \"type\": \"string\"
            },
            \"check_start_number\": {
                \"type\": \"string\",
                \"format\": \"integer\"
            },
            \"myprofile\": {
                \"$ref\": \"#/definitions/Myprofile\"
            },
            \"order_shipped_date\": {
                \"type\": \"string\",
                \"format\": \"date\"
            },
            \"myprofile_address\": {
                \"$ref\": \"#/definitions/MyprofileAddress\"
            },
            \"quantity_ordered\": {
                \"type\": \"string\",
                \"format\": \"integer\"
            },
            \"alert_id\": {
                \"$ref\": \"#/definitions/AlertID\"
            }
        },
        \"required\": [
            \"account_number\",
            \"alert_id\",
            \"check_start_number\",
            \"event_date\",
            \"myprofile\",
            \"myprofile_address\",
            \"order_shipped_date\",
            \"quantity_ordered\"
        ],
        \"title\": \"AlertDetails\"
    },
    \"AlertID\": {
        \"type\": \"object\",
        \"properties\": {
            \"id\": {
                \"type\": \"string\"
            },
            \"subject\": {
                \"type\": \"string\"
            },
            \"abc_id\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"abc_id\",
            \"id\",
            \"subject\"
        ],
        \"title\": \"AlertID\"
    },
    \"Myprofile\": {
        \"type\": \"object\",
        \"properties\": {
            \"name\": {
                \"$ref\": \"#/definitions/Name\"
            }
        },
        \"required\": [
            \"name\"
        ],
        \"title\": \"Myprofile\"
    },
    \"Name\": {
        \"type\": \"object\",
        \"additionalProperties\": false,
        \"properties\": {
            \"first_name\": {
                \"type\": \"string\"
            },
            \"last_name\": {
                \"type\": \"string\"
            },
            \"middle_name\": {
                \"type\": \"string\"
            }
        },
        \"required\": [
            \"first_name\",
            \"last_name\",
            \"middle_name\"
        ],
        \"title\": \"Name\"
    },
    \"MyprofileAddress\": {
        \"type\": \"object\",
        \"properties\": {
            \"name\": {
                \"type\": \"string\"
            },
            \"street_address\": {
                \"type\": \"string\"
            },
            \"city\": {
                \"type\": \"string\"
            },
            \"state\": {
                \"type\": \"string\"
            },
            \"zip_code\": {
                \"type\": \"string\",
                \"format\": \"integer\"
            }
        },
        \"required\": [
            \"city\",
            \"name\",
            \"state\",
            \"street_address\",
            \"zip_code\"
        ],
        \"title\": \"MyprofileAddress\"
    }

}}

验证对我不起作用,例如,在此架构中,我提供了必填字段“alert_id”,如果我在 JSON 数据中删除此字段,验证仍然会成功而不是失败并出现一些错误

https://www.jsonschemavalidator.net/s/Tqh2XRMI。 ---> 在这个,我保留
aaaprofile而不是个人资料aaaalert_id而不是 alert_id

它应该引发验证错误,但我没有收到任何验证错误

  • 你好。具体是什么问题。你想要或期望发生什么?说“验证不起作用”并不能告诉我们您需要什么验证。
  • 对于 JSON Schema,您可以在此处找到学习资源:json-schema.org/understanding-json-schema 基于 Web 的游乐场:json-schema.org/implementations.html#validator-web%20(online) 和有用的 slack 频道:json-schema.org/slack(如果您有不属于 StackOverflow 的问题)
  • @Relequestual 验证对我不起作用,例如,在此架构中,我将必填字段提供为“alert_id”,如果我在 JSON 数据中删除此字段,验证仍然会成功而不是因某些错误而失败,我怀疑我编写模式的方式可能是错误的
  • 请编辑您的问题,说明正在发生的事情、您期望发生的事情以及原因。 =]
  • @Relequestual 更新了我的问题和期望

标签: json validation jsonschema json-schema-validator


【解决方案1】:

您在第 4 行有一个错字。它应该是 oneOf 而不是其中一个:

"oneOf": [
  { "$ref":"#/definitions/checkchange" },
  { "$ref":"#/definitions/reviewcheck" }
]

在此更改之后,验证会引发有关缺少必需属性的错误。

【讨论】:

  • 这是一个很好的收获,在从 oneof 更改为 oneOf 之后,验证仍然无法正常工作,可能架构可能写错了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2019-06-29
  • 2017-04-26
  • 2020-01-30
  • 1970-01-01
  • 2017-12-31
相关资源
最近更新 更多