gpt4 book ai didi

JSON Schema `required` 允许值为空字符串

转载 作者:行者123 更新时间:2023-12-04 12:11:11 26 4
gpt4 key购买 nike

我正在使用 JSON 模式模板来验证在线表单收到的数据。验证器的要求之一是它允许根据对其他问题给出的答案要求一些问题。

例如,如果问题是 Do you want a loan?用户回答 yes ,那么问题的答案What is the loan to be used for?需要设置为 required 以便用户必须提供答案。如果答案是 no那么第二个问题就不需要了。

我使用定义来定义我的问题,然后在下面的主要问题模式中引用它们。我通过使用 Draft-07 中提供的 if-then-else 功能读到了这一点,我可以使用它根据其他问题的答案来设置某些问题。

在这个特定的例子中,我希望发生的是,如果用户输入答案 Home improvements (General)对于问题 9,则问题 257 将设置为必需且必须回答,否则应引发错误。

目前,当我将此验证器输入 https://www.jsonschemavalidator.net/ 时它没有按预期工作。实际情况是,即使问题 9 的答案是“家庭装修(一般)”,问题 257 的答案也可以留空

如何更改我的架构以提供我想要的行为?

JSON 架构

{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"question3-9": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"enum": [
"Home improvements (General)",
"Other"
]
}
}
},
"question3-257": {
"type": "object",
"properties": {
"answer": {
"type": "string",
}
}
}
},
"type": "object",
"properties": {
"form_submission": {
"type": "object",
"properties": {
"sections": {
"type": "object",
"properties": {
"3": {
"type": "object",
"properties": {
"questions": {
"type": "object",
"properties": {
"9": {
"$ref": "#/definitions/question3-9"
},
"257": {
"$ref": "#/definitions/question3-257"
}
},
"if": {
"properties": {
"9": {
"properties": {
"answer": {
"enum": [
"Home improvements (General)"
]
}
}
}
}
},
"then": {
"required": [
"257"
]
}
}
}
}
},
"required": [
"3"
]
}
}
}
}
}

要验证的 JSON:
{
"form_submission": {
"sections": {
"3": {
"questions": {
"9": {
"answer": "Home improvements (General)",
},
"257": {
"answer": "",
}
}
}
}
}
}

更新如果-那么
"if": {
"properties": {
"9": {
"properties": {
"answer": {
"enum": [
"Home improvements (General)"
]
}
},
"required": ["answer"]
}
},
"required": ["9"]
},
"then": {
"257": {
"properties":{
"answer":{
"minLength": 1
}
}
}
}

最佳答案

您的问题是您正在等待 required检查键的值,它没有。
当前草案 7 规范要求:

An object instance is valid against this keyword if every item in thearray is the name of a property in the instance.


这意味着 required只检查对象的键是否存在。
它与值(value)无关。
对于字符串验证,请参见适用于字符串的验证关键字。我怀疑你想要 minLengthpattern (这是正则表达式)。
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.3

关于JSON Schema `required` 允许值为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54399509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com