enum in json-schema with a (RegEx) pattern

huangapple go评论63阅读模式
英文:

enum in json-schema with a (RegEx) pattern

问题

以下是翻译好的部分:

在我的 JSON 数据集中,某个特定类别应该被限制为一组有限的值,因此我将它们放在 enum 中的列表中。存在两个问题:

  1. 在某些情况下,需要向值添加一个星号;这可以适用于所有值,例如 waterwater* 都是有效的。
  2. 其中一个值包含由括号中的两个字符表示的附加信息,例如 value(ab)

对于这两个问题,最容易使用正则表达式模式,但是否可以将 enumpattern 结合使用呢?(我知道可以将所有可能的值放在 pattern 中,用 | 分隔,但这并不理想。)

{
"category": {
"enum": ["water", "beer", "wine", "soda([a-z]{0,2})"]
}
}

有效的值包括 waterwater*beerbeer*winewine*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:

  1. In some cases it is necessary to add an asterisk to the value; this can apply to all values, for instance water and water* are both valid.
  2. 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>



huangapple
  • 本文由 发表于 2023年5月17日 20:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定