Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties that are propagated to subschemas attached by composition should not be displayed #173

Open
pspot2 opened this issue Oct 27, 2022 · 0 comments

Comments

@pspot2
Copy link

pspot2 commented Oct 27, 2022

Consider the following schema:

Click to expand
{
    "$id": "file:///home/eub-user/schema_test1.json",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "root",
    "description": "Root-level schema",
    "type": "object",
    "required": [
        "test_property01"
    ],
    "properties": {
        "test_property01": {
            "description": "Test property 01",
            "enum": [
                "myvalue1",
                "myvalue2"
            ]
        }
    },
    "allOf": [
        {
            "if": {
                "properties": {
                    "test_property01": {
                        "const": "myvalue1"
                    }
                },
                "required": [
                    "test_property01"
                ]
            },
            "then": {
                "description": "TBD",
                "required": [
                    "test_propertyA"
                ],
                "additionalProperties": false,
                "properties": {
                    "test_property01": true,
                    "test_propertyA": {
                        "description": "Test property A",
                        "type": "string"
                    }
                }
            }
        },
        {
            "if": {
                "properties": {
                    "test_property01": {
                        "const": "myvalue2"
                    }
                },
                "required": [
                    "test_property01"
                ]
            },
            "then": {
                "description": "TBD",
                "required": [
                    "test_propertyB"
                ],
                "additionalProperties": false,
                "properties": {
                    "test_property01": true,
                    "test_propertyB": {
                        "description": "Test property B",
                        "type": "string"
                    }
                }
            }
        }
    ]
}

This schema defines one fixed property (test_property01) and then another property using conditional schema composition. The additional property depends on the value of test_property01:

  • if test_property01 = "myvalue1", then the additional property test_propertyA is added
  • if test_property01 = "myvalue2", then the additional property test_propertyB is added

Important additional constraint: no other properties are allowed in the whole schema. In other words, this schema will validate an instance in one of the following cases:

  1. If test_property01 = "myvalue1", the instance contains only

    test_property01
    test_propertyA

    and no other properties.

  2. If test_property01 = "myvalue2", the instance contains only

    test_property01
    test_propertyB

    and no other properties.

The constraint "no other properties" is achieved by adding additionalProperties: false to every conditional branch. In order for this to work, however, the existing property test_property01 must be "propagated" to every branch by adding it to the properties block of every branch as "test_property01": true.

Expectation: the property test_property01 is displayed only once in the rendered document. It is omitted from the list of properties in every allOf branch because it is already defined in the parent schema and is merely propagated from it.

Actual result: the rendered document displays the property test_property01 three times: once in the root schema and once in every allOf branch.

Click to expand

root

Title: root

Type combining
Additional properties Any type: allowed

Description: Root-level schema

Property Pattern Type Deprecated Definition Title/Description
+ test_property01 No enum (of string) No - Test property 01
All of(Requirement)
item 0
item 1

Property root > allOf > item 0

Type object
Additional properties Any type: allowed

If (test_property01 = "myvalue1")

Type object
Additional properties Not allowed

Description: TBD

Property Pattern Type Deprecated Definition Title/Description
- test_property01 No object No - -
+ test_propertyA No string No - Test property A

Property root > allOf > item 0 > then > test_property01

Type object
Additional properties Any type: allowed

Property root > allOf > item 0 > then > test_propertyA

Type string

Description: Test property A

Property root > allOf > item 1

Type object
Additional properties Any type: allowed

If (test_property01 = "myvalue2")

Type object
Additional properties Not allowed

Description: TBD

Property Pattern Type Deprecated Definition Title/Description
- test_property01 No object No - -
+ test_propertyB No string No - Test property B

Property root > allOf > item 1 > then > test_property01

Type object
Additional properties Any type: allowed

Property root > allOf > item 1 > then > test_propertyB

Type string

Description: Test property B

Required Property root > test_property01

Type enum (of string)

Description: Test property 01

Must be one of:

  • "myvalue1"
  • "myvalue2"

Generated using json-schema-for-humans on 2022-10-27 at 22:37:55 +0000

Note: the not-yet-supported unevaluatedProperties: false removes the need to propagate properties. However, in many cases the older construct presented above is more preferable because validators produce cleaner error messages (without any cascaded secondary errors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant