Home > Software engineering >  How can I make patternProperties take a specific format in JSON schema?
How can I make patternProperties take a specific format in JSON schema?

Time:12-20

I have the following schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": {
        "type": "string"
    }
}

I'm trying to make the key of additionalProperties conform to a URL, so it would validate the following:

{
    "https://google.com": "google"
}

but not this:

{
    "google": "website"
//   ^^^^^^ this is not a url
}

How can I do this?

CodePudding user response:

You can specify the format of property names with patternProperties, which takes a pattern just like the pattern keyword.

If you want to do something more complicated, you can use propertyNames, which takes a schema that the property name must validate against. That might be what you want here:

{
  "propertyNames": {
    "format": "iri"
  },
  "additionalProperties": {
    .. schema for the property values themselves
  }
}

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

  • Related