JSON Schema: Ignore required property if not defined in properties

3.9k views Asked by At

Any way to ignore a property being required if it does not exist in the properties definition?

For example:

{
  "type": "object",
  "properties": {
    "billingAddress": {
      "type": "string"
    },
    "isSameAsBillingAddress": {
      "type": "boolean"
    }
    // Say I deleted the `mailingAddress` property
  },
  "required": [
    "billingAddress",
    "isSameAsBillingAddress",
    "mailingAddress" // ignore
  ]
}

From my understanding, this doesn't seem possible. I know this is kind of a silly question as someone may say to just remove the property name from the required array, but it becomes a bit more complicated when I am generating this JSON schema and also want to conditionally require a field if it exists.

For example:

{
  "type": "object",
  "properties": {
    "billingAddress": {
      "type": "string"
    },
    "isSameAsBillingAddress": {
      "type": "boolean"
    }
    // Delete `mailingAddress` property
  },
  "required": [
    "billingAddress",
    "isSameAsBillingAddress"
  ],
  "oneOf": [
    {
      "properties": {
        "isSameAsBillingAddress": {
          "enum": [true]
        }
      }
    },
    {
      "properties": {
        "isSameAsBillingAddress": {
          "enum": [false]
        }
      },
      // Would like to ignore...
      "required": ["mailingAddress"]
    },
  ]
}

Yes, I could write some code to go back and clean up this schema if the mailingAddress property gets deleted, but I was wonder if there was some JSON schema way of intelligently ignoring undefined properties?

Also from a JSON schema generator perspective, there just seems to be too many places a property name could be lurking, so any advice on how to structure this where code could easily clean up and generate a coherent JSON schema would be appreciated too.

3

There are 3 answers

0
Ether On BEST ANSWER

This seems like something better suited to a schema linter -- to warn you that a property definition is missing and perhaps accidentally deleted.

You can find a list of linters here -- https://json-schema.org/implementations.html#schema-linter -- if this check isn't implemented yet, it is likely fairly easy to do so.

1
Ethan On

no. required is intentionally independent of properties. required validates presence; properties validates child schemas if present.

conditionally require a field if it exists

I'm confused by what this could mean - it feels backwards to the point of required. if you only require a field to exist when it exists, why have required at all?

maybe you just want to delete your required keywords altogether?

1
spacether On

So I don't think that required property names that are not in properties should be ignored. See https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.4.3

In your example mailingAddress must be present and its value can be any type because additionalProperties defaults to empty object which allows any type value in.