我想指定一个字符串只能是四个值之一。我怎样才能使用 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/