[英]Can I validate the content of a string in my JSON schema in Python
I want to specify that a string can only be one of four values. 我想指定一个字符串只能是四个值之一。 How can I do that using the jsonschema library? 如何使用jsonschema库做到这一点 ?
Sample code: 样例代码:
"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
},
The above works fine. 以上工作正常。 What would I need to add to require userType
be one of only 4 values? 我需要添加什么以要求userType
是仅4个值之一?
Take a look at this example schema . 看一下这个示例架构 。
Use enum
to restrict a value to a fixed set of values: 使用enum
将一个值限制为一组固定值:
"label": {
"type": "string",
"enum": ["value1", "value2", "value3", "value4"]
}
It is also possible to define a regex pattern: 也可以定义一个正则表达式模式:
"label": {
"type": "string",
"pattern": "^value1|value2|value3|value4$"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.