-
-
Notifications
You must be signed in to change notification settings - Fork 268
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
✨ Proposal: Support $ref-to-enum in object required
#1551
Comments
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. 🤔 |
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
}
} EDIT: this enum example is not sufficient. It passes if there is a single enum value. I still need a way to assert that all enum values are present, which I'm having trouble thinking of a construction for. |
I'm sympathetic to the problem you're trying to solve. It's definitely a real problem that I'd love to have a solution for. Unfortunately, I don't see any of these suggestions fitting into the JSON Schema architecture. Schemas are always evaluated against a JSON instance and they produce a true/false (valid/invalid) result and some annotations. All of your proposals require some kind alternate evaluation of a schema to extract a value from the schema. That's not something JSON Schema is designed to do and I don't think that's a good path to go down. Instead of trying to solve the specific problem of using the same array for {
"properties": {
"a": { "enum": { "$ref": "#/x-locales" } },
"b": {
"type": "object",
"additionalProperties": { "type": "boolean" },
"required": { "$ref": "#/x-locales" }
}
},
"x-locales": ["en", "fr", ...]
} But, there are other use-cases as well. {
"minLength": 5,
"maxLength": { "$ref": "#/minLength" }
} For those to work, it would require that we change |
Agreed 100% with respect to the possibilities listed in the OP. Even as the ignorant consumer that I am, they certainly feel like unidiomatic hackery. I think #1551 (comment) is the best I've got so far down the
Agreed here too, insofar as I think the ability to externalize and reference the array itself would help to solve the issue. Some additional thoughts, though, that lead me to feel that the required-as-schema may still be a worthwhile pursuit: To my mind, I want to define enums as sets (granted, you could consider it specifically the array part of the enum schema as the set). To validate a single value against the enum schema is to ask I don't have a complete thought on this yet, but I wonder if there's room for applying set operations within array schemas against enums-as-a-set that would support crafting schemas that could both conform to requirements for 'required', as well as unlock the potential for set composition/intersection. |
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.
The text was updated successfully, but these errors were encountered: