JSON模式,其中ipv4/ipv6/主机名格式用作键。

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

JSON Schema with ipv4/ipv6/hostname format as keys

问题

如果我有一个像这样的JSON文档:

{
  "142.250.193.14": {},
  "2404:6800:4002:819::200e": {},
  "google.com": {},
}

即,它具有IPv4/IPv6地址和主机名作为键,而值与问题无关。我该如何定义一个JSON模式,使用内置格式来检查这些内容,我尝试使用patternProperties,但为所有这些内容编写正则表达式非常复杂。

英文:

If I have a JSON document like:

{
  "142.250.193.14": {},
  "2404:6800:4002:819::200e": {},
  "google.com": {},
}

i.e., it has ipv4/ipv6 addresses and hostnames as keys and the values are irrelevant to the question. How can I define a JSON schema that uses the built-in formats to check for these, I tried using patternProperties but a regex for all of these is quite complex.

答案1

得分: 1

那是propertyNames关键字,它允许您使用模式来定义属性名称的结构,当属性名称比模式允许的更复杂时。

https://json-schema.org/understanding-json-schema/reference/object.html#property-names

{
  "propertyNames": {
    "anyOf": [
      { "format": "ipv4" },
      { "format": "ipv6" },
      { "format": "hostname" }
    ]
  }
}

您需要确保您的评估器正在验证格式,因为默认情况下规定它是被禁用的。

英文:

That's the propertyNames keyword, which lets you use a schema to define the structure of the property name, when it's more complicated than a pattern would allow for.

https://json-schema.org/understanding-json-schema/reference/object.html#property-names

{
  "propertyNames": {
    "anyOf": [
      { "format": "ipv4" },
      { "format": "ipv6" },
      { "format": "hostname" }
    ]
  }
}

You need to make sure that your evaluator is validating formats, as that's specified to be disabled by default.

答案2

得分: -1

这里有一些接近的IPv6正则表达式示例,例如regexr中的这个示例。这些可以作为JSON的起点。

英文:

Don't have a full answer for you, but there are a bunch of IPV6 regex examples that come close, like this one from regexr. Those could be a starting point for the JSON.

huangapple
  • 本文由 发表于 2023年3月12日 13:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711268.html
匿名

发表评论

匿名网友

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

确定