gpt4 book ai didi

validation - 允许模式中未知键但已验证值

转载 作者:行者123 更新时间:2023-12-03 04:15:55 25 4
gpt4 key购买 nike

验证键未知但值具有明确模式的字典的最佳方法是什么。例如:

data = {
'name': 'test',
'department': {
'unknown_key': {
'known_key': 'good',
'unknown_key': 'bad'
}
}}

我已经尝试过

schema = {
'name': {
'type': 'string'
},
'department': {
'type': 'dict',
'allow_unknown': {
'schema': {
'type': 'dict',
'schema': {'known_key': {'type': 'string'},
'must_have_key': {'type': 'string'}}}
},
}}

但是随着验证通过,这已经失败了。它应该在丢失的 must_have_keyunknown_key 上都失败。我定义错误了吗?

最佳答案

您可以使用valueschema规则来定义映射中任意值数据的规则:

schema = {
'name': {'type': 'string'},
'department': {
'type': 'dict',
'valueschema': {
'type': 'dict',
'schema': {
'known_key': {'type': 'string'},
'must_have_key': {'type': 'string', 'required': True}
}
}
}
}

关于validation - 允许模式中未知键但已验证值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50994398/

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