Feature Request: Constraint for single occurrence of a property structure in JSON Schema #1488
Labels
proposal
Initial discussion of a new idea. A project will be created once a proposal document is created.
Problem Statement
The JSON Schema specification currently lacks support for ensuring that a specific property structure is defined once and only once within an object with dynamically named properties. This capability is essential for JSON representations of relational database tables, which require exactly one primary key column.
Reference to Related GitHub Issue
This feature request is related to ongoing discussions, specifically mentioned in json-schema-org/json-schema-spec#1358, which addresses the need to apply the
contains
keyword to object structures.JSON Schema (Current)
Here is the schema that demonstrates the current validation approach, which allows dynamic keys to be associated with either a
primaryKeyProperty
or atextProperty
, but lacks the ability to enforce one and only one instance ofprimaryKeyProperty
:Example JSON Instances
Instance that Should Pass Validation:
Instance that Incorrectly Passes Validation:
The above instance should fail validation because it has more than one occurrence of the
primaryKeyProperty
structure, which is against the intended constraint.Limitations of Current Keywords
The existing keywords within JSON Schema do not provide a way to limit the number of occurrences of the
primaryKeyProperty
structure within an object. ThepatternProperties
andoneOf
are currently used to validate individual properties but cannot enforce the rule thatprimaryKeyProperty
must be limited to a single occurrence within the object.Proposed Solution
To address this limitation, we propose the introduction of a new keyword or the extension of an existing keyword to perform this validation. Possible solutions could be:
objectContains
, which would ensure that a specific structure appears exactly once within an object.contains
keyword to validate that an object contains a particular property structure only once.Conclusion
Enforcing that a
primaryKeyProperty
structure appears once and only once within an object's properties is a crucial feature for JSON Schema, enhancing its ability to model certain data structures that resemble relational databases. Thank you to @gregsdennis and @jdesrosiers for their guidance on this issue (https://json-schema.slack.com/archives/C5CF75URH/p1707258032879409).The text was updated successfully, but these errors were encountered: