Open etinquis opened 3 hours ago
None of the possibilities above consider any form of composition of multiple enums, which may also be a nice feature if it's possible.
An potential alternate proposal:
Adjust the semantics of required such that it behaves similarly to propertyNames.
That is, required is a string-validating schema that evaluates against the properties present on an object, and where 'valid' resolves to required and 'invalid' resolves to not-required.
The current list definition is substituted with an enum schema with the inline values, which might provide backwards-compatibility?
EDIT: Or maybe that doesn't make sense. 🤔 It'd have to intersect with the properties defined, and somehow still be able to identify missing keys.
Alternative alternative proposal:
required is a string array-validating schema over all present property names, with the current list case being constructed via contains.
I assume this wouldn't surface errors very clearly.
EDIT: e.g.
{
"required": ["property1", "property2"]
}
potentially equivalent to
{
"required": {
"type": "array",
"allOf": [
{
"contains": {
"const": "property1"
}
},
{
"contains": {
"const": "property2"
}
}
],
"additionalItems": true,
"uniqueItems": true
}
}
and I believe the enum case could be
{
"required": {
"type": "array",
"items": {
"$ref": "/enum.json"
},
"additionalItems": false,
"uniqueItems": true
}
}
Describe the inspiration for your proposal
High-Level goal: I want to be able to consolidate and re-use what are practically-speaking a static set of property names or object keys within a schema such that I am able to maintain the list in one place and reference them as either object property names or values, depending on context.
[My examples will assume I'm trying to define a schema for a localization data structure]
I start with a few enum schemas containing my keys.
I can reference this enum as values for properties on objects as so
I can reference this enum to define property names on objects as so
Now I want to define my 'source-of-truth' translated string file format schema, where I want validation to fail if any property is missing:
Describe the proposal
Possibilities:
An 'overload' on
required
directly allowing references to enum schemasA new
required
-adjacent property allowing references to enum schemasSome way to define propertyNames from an enum in a way that also makes them all required
Describe alternatives you've considered
The only way I understand there to be to implement this currently to duplicate and inline the values separately into 'required'.
This imposes a potentially significant maintenance burden whereby changes to the enum must be duplicated in the subset of places where the full set should be required. Failure to propagate those changes also risks falsely validating schemas 'silently' or failing what should be valid schemas without some sort of linting procedure that is capable of identifying this pattern.
Additional context
https://github.com/orgs/json-schema-org/discussions/818 was my original Q&A discussion post looking for whether this was possible.