如何在 JSON 模式中禁止句号。

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

How to disallow a period in a JSON-schema pattern

问题

Sure, here's the translated content:

我想在JSON模式模式中的以下被禁止字符集中添加一个句号:

"pattern": "[^\"{}:, \t]+"

我的第一个想法是尝试:

"pattern": "[^\.\"{}:, \t]+"

但这会产生模式的解析错误。

我的下一个想法是尝试:

"pattern": "[^\\.\"{}:, \t]+"

但当我尝试验证我的YAML文件时,这并不具有所期望的属性。

我正在使用ajv作为我的JSON模式验证工具,并验证一个YAML文件。
英文:

I would like to add a period to the following banned character set in a JSON schema pattern:

"pattern": "[^\"{}:, \t]+"

My first thought was to try:

"pattern": "[^\.\"{}:, \t]+"

but this produces a parse error of the schema.

My next thought was to try:

"pattern": "[^\\.\"{}:, \t]+"

but this does not have the desired property when I try to validate my YAML file.

I am using ajv as my JSON schema validation tool, and I am validating a YAML file.

答案1

得分: 1

"As axiac and Diego D note, the period does not need to be escaped in the character class. However, in addition to that problem, the pattern was not matching anything because it lacked the prefix ^ and suffix $. The final pattern is:

"pattern": "^[^\"{}:., \t]+$""
英文:

As axiac and Diego D note, the period does not need to be escaped in the character class. However, in addition to that problem the pattern was not matching anything because it lacked the prefix ^ and suffix $. The final pattern is:

"pattern": "^[^\"{}:., \t]+$"

答案2

得分: 0

"." 在字符类中不是特殊字符。只有 ^-] 是特殊字符,它们必须在字符类中进行转义。<br>
^- 甚至可以在字符类中不进行转义,只要它们不被放在具有特殊意义的位置上。

你的成功尝试在字符类中添加了 .\

只需在字符类中的某处添加 .(不要在 ^ 之前),就可以了:

"pattern": "[^\"{}:., \t]+"
英文:

. is not a special character in character classes. Only ^, - and ] are special characters and have to be escaped in character classes.<br>
^ and - can even be used without escaping in a character class if they are not placed in positions where they have special meaning.

Your successful attempt added both . and \ in the character class.

Just add . somewhere in the character class (not before ^) and that's all:

&quot;pattern&quot;: &quot;[^\&quot;{}:., \t]+&quot;

huangapple
  • 本文由 发表于 2023年3月23日 10:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818819.html
匿名

发表评论

匿名网友

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

确定