-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Descriptions for each enum value #47
Comments
this is discussed previously at json-schema-org/json-schema-spec#57 |
@notEthan, thanks for the link! Haven't seen it before and nobody was pointing to it :-( Although now I see all the pros and cons, it still looks like the suggested in 2016 approach with using Also, currently it's quite easy to find all the enums - you just look for "enum". And if some of my enums are Please consider if this can have a cleaner solution in a future version of JSON schema. |
Also note that we strive for backward compatibility, and changing I suggest proposing a new keyword. Or better yet, define said keyword in a vocab. |
The short answer there is enums can be any type, including an object, not just a string -- so what this construct is actually saying is "the value should be a literal object with two properties, 'value' and 'description'". We would need a new keyword to provide enum values and titles/descriptions side by side.. but that's basically duplicating "oneOf", only less generically (which arguably makes it easier for tools to recognize, but also clutters the syntax). |
The other syntax { "enum": { "D": "Suspicious transaction", ... } } is viable because the value is presented as an object rather than an array, and so can be distinguished from the current format. This can be extended to support more metadata as well: { "enum": { "D": { "description": "Suspicious transaction", ... }, ... } though the semantics of this format differs from other keywords that contain subschemas (e.g. I still think that this needs to be defined in an external vocabulary, which would mean that implementation support for it would be sparse, and it definitely should not reuse the |
My big concern with the object form of i.e. the following would not be a valid schema in most JSON parsers
or
meaning we limit enumerations to only numbers and strings (and some JSON parsers don't support numbers as keys) when we want to use the descriptive Having another enum like keyword to describe the behaviour would be helpful for a number of cases, especially the above OpenAPI example, and things like UI generation. |
The The problem is that it's difficult for documentation generators to detect when a {
"docHint": "enum",
"anyOf": [
{ "const": "a", "description": "A" },
{ "const": "b", "description": "B" },
{ "const": "c", "description": "C" }
]
}
I don't think anyone on our team has experience with documentation generators, but if this option is the direction this proposal goes, we'd be excited to assist anyone who wants to take on the task of defining a JSON Schema vocabulary for documentation generators. |
I would like to add that |
That's a good point about validation errors. A hint keyword wouldn't change the standard output, but it could be used when processing the standard output to produce user friendly error messaging. |
I have written multiple documentation generators or tools to support them, and I would prefer something like @jdesrosiers's |
Are you working on the Understanding JSON Schema update? I feel like this would be a great topic to cover. A major motivation for the output format (both the error and annotation side) was to enable better error reporting for complex structures without the spec having to mandate the specifics. |
I am. And I agree. |
One downside to this (and it might have already been mentioned) is that this only supports string-type values in the enum since the values have to be keys. This is contrary to the existing support for |
@gregsdennis you're referring to the |
Yes, the enum object has this limitation. I haven't read through this completely. |
An edge case (which I'm good at) is in the implication that a JSON-Schema can be used to infer a form structure, which is a function to which I am actually putting it. By judiciously using (I've basically leant on JSON-Schema as the format for all schema definitions within several systems I've written, since all structured data I've come across so far has fitted it.) Anyway, there are several form controls that list options for the user to select, and all of them are better if there is a human-readable label to go along with the machine-expected const value. I have been using Is there any reason there couldn't be a formal version of this? Similarly, there is no reason I couldn't use a doc hint and a bit of extra logic to turn an anyOf into a select box instead of fieldsets like I currently do, so I'm not strongly invested in this given there's a workaround. |
I came to a similar idea, but using the term "descriptions" (plural) |
@drumphil Your approach doesn't solve the problem stated in the initial post:
The descriptions are only linked to the enum values by index. It's easy to mess up and not immediately apparent with a few more values. |
No, and in fact we created vocabularies so people could formally describe and require 3rd-party extensions. I'm going to move this over to the vocabularies repository where we keep extension ideas. There are also a couple of repos under this org for various specific vocabularies which folks might want to check out. We don't plan to add this sort of thing to the core standard anytime soon — we added vocabularies so that there could be a way to use keywords interoperably without them having to go in the main spec. |
vscode uses that method. it calls the entry https://github.com/Microsoft/vscode/wiki/Setting-Descriptions |
TL;DR Moving the discussion from OAI/OpenAPI-Specification#348 to JSON Schema folks.
Hi All,
For enums, we can now only list the values, for example:
But it's a very common practice to explain what these values are, and if there are any important considerations for each value. Because of the current design, we need to duplicate all these values in the description of the enum, which leads to confusion and mistakes. For example:
It would be much nicer to have either this:
Or better this:
According to https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values, the
enum
keyword is the one that "is used to restrict a value to a fixed set of values". Thus, if somebody wants to find all the enums in a schema (manually or programmatically), currently they can just search for "enum" and get all the enum instances.If we encourage people to use
oneOf+const
for enums (as was also suggested in OAI/OpenAPI-Specification#348), it will become problematic to find all enums and also to support in relevant tooling.I personally really like the idea of the following format, but not sure what's your reasoning against it:
In general, this issue seems to be super-important for proper adoption of JSON Schema and OpenAPI formats by the documentation communities. Currently, we have to struggle a lot to keep the description in sync with the actual lists of enum values, and see a lot of problems caused by that (e.g. some values are missing, some exposed by mistake, some have spelling errors or mistakenly follow PascalCase instead of camelCase).
Also, the number of comments, votes and mentiones in the original thread OAI/OpenAPI-Specification#348 really speaks to the idea of this problem being important. Please, consider extending JSON schema to properly support descriptions of enum values.
The text was updated successfully, but these errors were encountered: