gpt4 book ai didi

python - 我可以在 Python 中验证 JSON 模式中字符串的内容吗

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:09 25 4
gpt4 key购买 nike

我想指定一个字符串只能是四个值之一。我怎样才能使用 jsonschema 做到这一点图书馆?

示例代码:

"value_params": {
"required": ["positions", "userId"],
"properties": {
"userId": {"type": "integer"},
"positions": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["partnerUserId", "userType"],
"properties": {
"partnerUserId": {"type": "integer"},
"userType": {"type": "string"}
},
"additionalProperties": False
}
}
},
"additionalProperties": False
},

以上效果很好。我需要添加什么才能要求 userType 成为仅有的 4 个值之一?

最佳答案

看看这个 example schema .

使用enum将值限制为一组固定值:

"label": {
"type": "string",
"enum": ["value1", "value2", "value3", "value4"]
}

还可以定义正则表达式模式:

"label": {
"type": "string",
"pattern": "^value1|value2|value3|value4$"
}

关于python - 我可以在 Python 中验证 JSON 模式中字符串的内容吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327970/

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