skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search