[v5 proposal] choices (improvement to enum) #211
Comments
What about this proposition? https://github.com/json-schema/json-schema/wiki/enumNames-(v5-proposal) |
And I think this idea is better as you can have a name (for code generation) and a description: The problem with code generation: |
Too verbose and overcomplicated for my tastes, but as far as it is now almost anything would be better than enum. |
I like https://github.com/json-schema/json-schema/wiki/enumNames-%28v5-proposal%29 better. It's easier to maintain BWC with this. Suppose you were to follow the recommendation on the front end. The back end cares not for |
@sam-at-github suppose you have a list of 50, 100 or 200 entries with raw values that differ quite a lot from human readable values, with the |
I like the fact that the
The advantage is that its BWC. Since Json Schema ignores values it does not understand, the front end can exploit the |
Could you explain to me what's the difference between: {
enum: [0,1,2,3],
enumLut: [[0, "a"], [1, "b"], [2, "b"]]
} and my proposal: {
enum: ["UTC", "GMT0", "EAT-3"],
choices: [
["UTC", "Coordinated Universal Time"],
["GMT0", "Africa/Abidjan"],
["EAT-3", "Africa/Addis Ababa"],
]
} They're identical, the difference is just in the wording |
If you say they are identical then they are. Sorry I misunderstood this part of your proposal:
When you put, |
@sam-at-github I did not understand your last question. Regarding the keyword,
Ruby on rails which uses the same concept of |
Hmm, Ok so I think maybe your proposal and my interpretation is not actually identical then. Given:
You say "if both enum and choices would be present, choices would be used.". I was thinking I think your saying, given the schema:
And json:
A v5 json schema validator should validate the json successfully because it knows what
Hmmm, yeah but there is a difference. And yeah, actaully I think "choices" is the best name for the concept, but again its not exactly the same as enum. |
I'm mostly a lurker, not a contributor, just trying to follow this discussion. I don't think this is in the current schema, but I would have thought/proposed that you might get something like this for an
(Since the metadata isn't really part of the validation, I would have thought it'd be in a Which would then have something like this as valid:
Hopefully I'm not way off base on what's being discussed. On February 7, 2016 1:45:06 PM CST, sam-at-github notifications@github.com wrote:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
I'm mostly a lurker, not a contributor, just trying to follow this discussion. I don't think this is in the current schema, but I would have thought/proposed that you might get something like this for an
(Since the metadata isn't really part of the validation, I would have thought it'd be in a Which would then have something like this as valid:
Hopefully I'm not way off base on what's being discussed. On February 7, 2016 1:45:06 PM CST, sam-at-github notifications@github.com wrote:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
@saibotsivad Yeah that is simimlar to the current workaround mentioned in enunNames:
|
Another mostly-lurker here.
Specifically, I propose that these two schemas be considered valid JSON Schemas with identical semantics. I believe that this (along with many slight variations thereof) achieves the desired goals of having the human-readable names clearly visually associated with their corresponding values, maintaining complete backwards compatibility, and making it easy for code to either ignore or utilize the human-readable values as they wish. |
Hi, I am very new to this discussion, but I would caution against introducing UI oriented things in the schema. We tried to introduce UI annotations in a generic xsd-based model layer and it didn't take at all due to a simple fact that UI had to be localized. I.e. literal labels did not make sense at all. We thought about resource ids but even then that would presume that the client used an id-based localization mechanism. Hope I understand the issue correctly. |
@YurySk well there are quite a few other proposal which your suggestion would be against, like #119. |
What it's currently missing from JSON Schema is an ordered list of values with a relative human readable label. |
I on the other hand do not like that schema would contain user readable strings. Especially because those strings are often useless: they have to be translated, in some languages translations depend on the context (like number, of verb case). So then you end up with a mapping table between those second strings to translations. Why then not simply use raw enum values for translations to begin with? So, I would claim that if user-friendly strings are needed, use translation engine in your platform and use raw enum values as keys for translation. |
I also discourage introducing the The alternative proposal of @Ixrec is a better idea, but I'm still not sure if it is needed. The json schema spec already provides 2 ways to add metadata to the schema which doesn't directly affect the validation process:
|
@nemesisdesign |
Another argument for considering this part of the validation spec, is that if validation fails, the human readable value can be included in the validation error message. @YurySk the human readable string in english can be passed to the i18n framework, this is currently how django does it, although not every framework might do it this way, but generally speaking, if the framework is using |
Hi! For me, JSON schema has always been about definition and validation, and definition and validation to a reasonable degree. This has kept it from immediately growing to the size and complexity of XML schema. That kind of complexity has very few use cases where it is actually necessary. So I am not supporting this, for these reasons:
|
@nicklasb have you actually read the current JSON-Schema validation RFC? See the following section bold added by me:
Same applies for the Exactly for the reason that JSON-Schema is GREAT, we should improve it to make it simpler to achieve this kind of features. The good thing about |
I do not agree, that one thing can be used in a ui, does not in any way
|
@nicklasb I did not understand your last message, please explain it in a better way. I spent time answering to your comment, I would like you to do the same. Thanks. |
@nemesisdesign And second, that is my answer. What I am saying is that I do not agree with your reasoning. That the title and description of a field can be used in a user interface, doesn't make it right to add things that are specifically for UI functionality. WRT default values, they are certainly not only of use in a UI, a default value is part of the data definition, just as there are default values of fields in a RDBMS database. I think that It is really important to define a clear scope for a standard and stay with it. So far, JSON schema is easy to implement and compact. I am not saying that enum descriptions would kill it. I am just saying that there is no use for it, and that this problem is completely natural to solve in other places than the schema definition. WRT to the earlier l8n comparison, one should not equate l8n on static UI elements in a site like "menu" and so on which will never change and doesn't have an underlying Id, with Id's in a data definition that might. I would not use the UI l8n functionality to translate those, when they are likely defined in the database. But that is a minor objection. |
@ nicklasb, well put. I have been trying to say the same thing, the concern separation needs to be kept crystal clear - JSON schema is (or rather should be) first and foremost an expression of constraints on a data structure. Creeping all sorts of cool features in is a sure way to blur the boundaries, increase an implementation effort and decrease clarity of the purpose. The more of that is done the greater the chance of people abandoning it like many other great things the industry has created over the years. BTW, it's a mistake to think that even V4 is super-easy to implement. If you want user-friendly tooling it's not easy at all, since the spec allows to create contradictory and unsatisfiable schemas in all sorts of weird and wonderful ways. Trapping all that and issuing warnings adds to an effort significantly. I am definitely not looking forward to implementing any obscure stuff that my users have no use for just to remain spec-compliant. |
I would certainly agree that it doesn't seem like an easy implementation, but I would say it is far easier than the full extent of, say, XML schema. If I may digress, the cool thing with JSON schema is exactly its looseness. I am not sure that warnings, however helpful they may be, are in that vein. For example, I recently (approximately and tentatively) converted the schema.org definitions to json schema, and basically, that is going too far. On the other hand, one could say that schema.org goes too far. :-) But anyway, when you start defining really complex formats, I'd say you move out of the JSON form realm and into XML schema. And to some extent you are also moving back about 15 years, because I have been involved in those kinds (even that, especially) of implementations, and I think that they would have benefitted from being broken up instead of bundled, it doesn't feel like that is how I would choose to work today. |
I think warnings are important for an interactive tool, they just keep your life easier. I am not sure I like 'go to XML when your stuff gets complex' reasoning. Oftentimes, there's simply no XML support in your environment. Or your tool may be so general-purpose that you don't even control the schemas at all, users contribute them. Or you grow into complexity gradually... |
Well, there are some that may be useful. But I'd wager that those warnings takes a fair amount lot of time to implement, perhaps even close to the time taken to implement the actual validation. So perhaps it is not fair to include that time in how long it takes to implement a format, as to how want to warn that depends on your requirements, not the standard itself. I mean, I could think up a million* such cases for XML schema as well, in addition you have the security vulnerabilities with more complex standards to guide people around. Sorry, I just realized that am so far off topic now. I should not discuss everything. :-) * Not literally :-) |
Sure, it's not fair to include all the user-friendlines into the equation. It's just sooner or later that needs to be done and depending of the spec it may or may not be easy. In Json schema it's rather on the 'not' side. |
I bet it is. |
I insist that this argument on this point is logically invalid: Same applies for Other UI specific proposals would be better off included in a separate UI-centric specification, but I believe this is not the case, unless we would support a different proposal like |
What about a compromise? JSON schema allows extra properties to exist in schema objects, so we could simple have "pure validation" and "extended" JSON schema? |
@mitar, yes that is possible, there is a JSON-schema implementation that does this; it works but I dislike that solution because it doesn't bring the advantage of decoration and documentation of the schema. I'll try to explain what I mean with the following example (timezone values used by OpenWRT): {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"enum": [
"AMT4AMST,M10.3.0/0,M2.3.0/0",
"CST6CDT,M4.1.0,M10.5.0",
"VET4:30",
"GFT3",
"EST5",
"CST6CDT,M3.2.0,M11.1.0",
"MST7MDT,M4.1.0,M10.5.0",
"CST6",
"AMT4AMST,M10.3.0/0,M2.3.0/0",
"AST4",
"GMT0",
"PST8PDT,M3.2.0,M11.1.0",
"MST7"
]
}
}
} I had to cut the schema for brevity, so I did not include the timezones of the entire globe, but I ask you for a moment to use your immagination to put there the timezones of the entire globe. Choices instead would give us this advantage (just like the properties {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"choices": [
["AMT4AMST,M10.3.0/0,M2.3.0/0", "America/Campo Grande"],
["CST6CDT,M4.1.0,M10.5.0", "America/Cancun"],
["VET4:30", "America/Caracas"],
["GFT3", "America/Cayenne"],
["EST5", "America/Cayman"],
["CST6CDT,M3.2.0,M11.1.0", "America/Chicago"],
["MST7MDT,M4.1.0,M10.5.0", "America/Chihuahua"],
["CST6", "America/Costa Rica"],
["AMT4AMST,M10.3.0/0,M2.3.0/0", "America/Cuiaba"],
["AST4", "America/Curacao"],
["GMT0", "America/Danmarkshavn"],
["PST8PDT,M3.2.0,M11.1.0", "America/Dawson"],
["MST7", "America/Dawson Creek"],
]
}
}
} But I admit that if we cannot get consensus on something like |
OK, you convinced me. :-) It is not a question of just schema being used for UI, but also having schema be descriptive enough to be understandable. Consumers of schemas can be also people. |
@mitar wow, that's an achievement :-) |
Well, I cannot speculate on the original intentions of the authors of the RFC, so I won't. But I do know that a schema it not only about validation, but also about definition.
The same does not apply to @mitar
In the json-schema-form project we are currently trying to create a form standard to be used by the different implementations (angular-schema-form, react-schema-form and future *-schema-form, some will actually be desktop implementations), and when you do that, the importance of clearly separating data and UI definitions becomes evident. With good, clean separation, a schema can be reused all over an organization(or even a sector) and UI forms becomes optional. Without it, well then it won't work. |
And then there are the practical reasons: Defining the schema and the UI is usually two completely different projects and often involving different people. You want to be able to freely change the UI without having to change the data definition. It would drive people crazy to have schema version increment constantly without there being any actual difference, not to mention that the commit history of the schema would be filled with UI-changes. And if you want to make breaking changes to the UI, definitions, should you make a new major version of the schema too? They are two different things, and for many reasons. |
@mitar That is not my point, this happens regardless of versioning scheme. It is a plain and clear separation of concern. (with regards to choices, they are actually often not even defined in the UI-definition but fetched from the backend, so that is how far from the schema they end up) |
In regards to default. I don't think it's so much a UI thing. It's just an indication of a value that can be used to fill a missing piece of info. That can just easily happen in batch mode when data is being processed. Is there a particular reason enum labels cannot be expressed as a non-schema attribute of type array at an application level. Why standardtize it? Not every application will even use a construct like that. I'm fact, I bet that most won't. The task of standardizing UI definitions is a separate one. After all, why not standardize everything. For example I could probably make a convincing case that we need to out data storage attributes in as well. For example, a table name and field where an element to be stored by a generic DBMS storage facility. My point is that all of that can be already successfully achieved in JSON Schema as well as XSD. All that needs to be said at the spec level is that a non-schema information in a schema is OK. Then a number of extending specs can be created as public standards or application level conventions. |
Having said all this, I cannot disagree with the issue of documenting enum values. This issue is made worse by lack of comments in JSON. EDIT: obviously this wouldnt work because we would need an array of such objects and this would make it indistinguishable from the current flavor. |
A question. Say 'choices' are added. I presume enum is still supported. How would we reconcile it if both are present? |
I no issue at all with not documenting enum values on each value. A description is sufficient. If not, there is something wrong. In normal coding, the typical enum notation is This because either:
For example country codes. Are they good in an enum? No. Because in any non-trivial system design, country codes are used all over and have their own storage location, be it a table or top node or whatever. Either way, there is no point in putting extra features into the schema to provide that information as:
No UI related elements in JSON Schema. Please. Look! You are making me saying "please", now. :-) |
I tried to make a distinction between documentation purposes and UI one. I don't like the idea of standardizing UI bits. |
That is an interesting idea. So yea, does enums mean that all values are of the same type? Do choices mean the same? We could really go in the way that if values of enum schema definition are just strings, then you can add them as they are, but otherwise you can have subschema which defines not just the value, but also type, and description. |
Actually, I have started commenting my JSON schemas lately. As I realized all the parsers I used accepted it. |
How? |
{ First I just did it temporarily, but know I am thinking of not removing it. |
This is useful, thanks! |
Actually I was partially wrong there, JSmin was used before the parser, and I didn't see that, sorry. But it is kind of useful. (it makes the schemas non-portable....but on the other hand I cache them in the system, so they aren't visible there anyway) |
Apparently Crockford himself is ok with it: (and he is developing a JSmin: http://crockford.com/javascript/jsmin |
I'm currently using a custom[0] implementation of JSON Schema similar with @saibotsivad 's proposal. Separate fields for the enum description/meta data seem very odd and not orthogonal with the rest of the schema specification. You don't want the "description" and "title" fields in a separate object than the property object itself so why would you want that for enum properties? I would propose to make enum an array of objects (EnumSchema) . This way it becomes future-proof for additional features/fields(.e.g. internationalisation, additional UI meta etc). [0] |
Hello. Very interesting thread. We are using JSON-schema to generate complex UIs. So far we have been using custom {
"oneOf": [
{
"title": "One",
"description": "One is the magic number.",
"constant": 1
},
{ "title": "Two",
"description": "It takes two hands to clap.",
"constant": 2
},
{
"title": "Three",
"description": "Three is a company.",
"constant": 3
}
]
} It's basically the same as @themihai's solution, but within the spec. So I would vote against this proposal - simple cases are covered by * or |
This discussion should be moved to the new (active) JSON Schema specification repository: While I tried to capture the basics in the new issue, I am sure I left some significant points from this long discussion out. Feel free to copy them over. |
@nemesisdesign Could you please close this since we have an active discussion in the new repository? |
@handrews sorry, I was so slow to respond. I'll try to participate in the discussion again. |
Problem
the
enum
attribute for strings is really useful but has a severe limitation: it does not support providing a human readable label for raw values.I tried workaround solutions but I prefer to stick to
enum
for its simplicity and for the fact that UI implementations (like JSON Editor) can very easily generate an HTML select with the allowed values.Unfortunately though,
enum
cannot provide human readable values for decoration and documentation purposes (just liketitle
anddescription
properties included in the json-schema validation RFC), which makes it difficult to work with a long list of raw values that differ greatly from the corresponding human readable ones, for example, in a list of timezone settings for the entire globe, how can a programmer remember that "SAST-2" means "Africa/Mbabane" and "MST7MDT,M3.2.0,M11.1.0" means "America/Cambridge Bay".The human readable value can be used for different purposes:
Solution proposed
Many popular frameworks like Django (python), Ruby on Rails (ruby) and Symfony (php) support specifying a list of
choices
, which are usually either a list of tuples or a dictionary (associative array), in which one element is the raw value while the other is the corresponding human readable label.An example of a subset of choices for timezones:
Backward compatibility
Adding a "choices" (or an equivalent) attribute can be backward compatible with the current draft (v4):
if both
enum
andchoices
would be present,choices
would be used.if only
enum
is present implementations can still support it for backward compatibility.Full backward compatible example:
Advantages
Using a list of tuples (opposed as a list of objects) has the advantage of being a very simple, portable, and concise.
It's easier to maintain, faster to type and easier to read. Additional attributes can be allowed from the third element onwards if needed.
Another advantage is that this pattern is very well known in many MVC frameworks and it will result familiar to a lot of developers.
Downsides
The main downside is duplication of efforts when backward compatibility is needed: In an eventual transition from v4 to v5 both
enum
andchoices
should be supported in implementations. Popular schemas that aim to maximum compatibility also should use both.The text was updated successfully, but these errors were encountered: