Skip to content

Annotation output for propertyDependencies #1367

Open
@gregsdennis

Description

@gregsdennis

propertyDependencies was originally proposed as a replacement for a complex and verbose pattern to apply a schema to an instance based on the value of a property, similarly to OpenAPI's discriminator, but with more native JSON-Schema-like functionality. It was added to draft-next (post-2020-12) here.

Proposed as a replacement for this pattern, one could expect that it would work the same in all cases, however I added a test to the Suite recently to cover the interation between propertyDependencies and unevaluatedProperties:

{
  "$schema": "https://json-schema.org/draft/next/schema",
  "propertyDependencies": {
    "foo": {
      "foo1": {
        "properties": {
          "bar": true
        }
      }
    }
  },
  "unevaluatedProperties": false
}

This is supposed to be analogous to:

{
  "$schema": "https://json-schema.org/draft/next/schema",
  "if": {
    "properties": {
      "foo": { "const": "foo1" }
    }
  },
  "then": {
    "properties": {
      "bar": true
    }
  },
  "unevaluatedProperties": false
}

In the latter schema, unevaluatedProperties can "see into" /if to recognize that the foo property is declared. If the value of foo is foo1, then the /if subschema passes and produces an annotation that foo is a known property, so unevaluatedProperties subsequently ignores the property and passes validation.

However, as it is currently defined, propertyDependencies produces no annotations. That means, for the former schema, unevaluatedProperties wouldn't know about the foo property, so it would produce an error. Therefore the current definition fails to meet the explicit purpose of this keyword: a replacement for this common if/then, discriminator-like pattern.

This could be solved by requiring propertyDependencies to produce an annotation of the properties listed within it (just like properties does). (We would also need to update unevaluatedProperties to consider that annotation.)

If we maintain that propertyDependencies does not produce an annotation, then the author is required to define the property elsewhere:

{
  "$schema": "https://json-schema.org/draft/next/schema",
  "properties": {
    "foo": true // or an enum or something
  },
  "propertyDependencies": {
    "foo": {
      "foo1": {
        "properties": {
          "bar": true
        }
      }
    }
  },
  "unevaluatedProperties": false
}

I believe this counteracts the verbosity reduction benefit that propertyDependencies provides.

NOTE Interactions with other keywords, including unevaluatedProperties was not explored in the original proposal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    In Discussion

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions