英文:
enum in json-schema with a (RegEx) pattern
问题
以下是翻译好的部分:
在我的 JSON 数据集中,某个特定类别应该被限制为一组有限的值,因此我将它们放在 enum 中的列表中。存在两个问题:
- 在某些情况下,需要向值添加一个星号;这可以适用于所有值,例如
water和water*都是有效的。 - 其中一个值包含由括号中的两个字符表示的附加信息,例如
value(ab)。
对于这两个问题,最容易使用正则表达式模式,但是否可以将 enum 与 pattern 结合使用呢?(我知道可以将所有可能的值放在 pattern 中,用 | 分隔,但这并不理想。)
{
"category": {
"enum": ["water", "beer", "wine", "soda([a-z]{0,2})"]
}
}
有效的值包括 water、water*、beer、beer*、wine、wine*、soda()、soda()*,以及最后两个中括号之间的任意两个小写字母的组合。
英文:
A certain category in my json dataset should be restricted to a limited number of values, so I put them in a list in enum. There are two issues:
- In some cases it is necessary to add an asterisk to the value; this can apply to all values, for instance
waterandwater*are both valid. - One of the values contains additional information indicated by two characters in between brackets, for instance
value(ab).
For both issues, it is the easiest to use RegEx patterns, but is it possible to combine enum with pattern? (I know it is possible to put all possible values in a pattern separated by |, but that is not ideal.)
{
"category": {
"enum": ["water", "beer", "wine", "soda([a-z]{0,2})"]
}
}
Valid values are water, water*, beer, beer*, wine, wine*, soda(), soda()*, and the last two with any combination of two lowercase letters in between the brackets.
答案1
得分: 3
You can use anyOf with pattern.
{
"type": "string",
"anyOf": [
{ "pattern": "^water.*" },
{ "pattern": "^beer.*" },
...
]
}
英文:
You can use anyOf with pattern.
{
"type": "string",
"anyOf": [
{ "pattern": "^water.*" },
{ "pattern": "^beer.*" },
...
]
}
``
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论