You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
remove confusing type Enum or String (a set of values could not be closed and open) #215
But this definition is "conflicting" as the second is a superset of the first. (if oneOf is used instead of anyOf a jsonschema linter will warm about this issue). I mean for any language implementation string will be used (or an automatic generator/serializer+deserializer will be confused).
What is the purpose/value of the enum if any value can be used? (eg if a system is uppercase like "LOW")
Suggestion: replace the anyOf by single"type": "string"
@davidB we added the anyOf(string, enum) on purpose so that we could suggest some known values in the JSON schema (and not only in the docs), but still accept others. We are aware that validation can in fact only validate that values are a string but we use this as a way to document well known values.
The initial approach was to only have an enum with other as one of the values, which was limiting because it would not let users carry the value they needed in their event.
An alternative I considered was keeping other and adding another field, to be used only when other is used. With this option, a consumer does not know in advance which field contains the expected value, which is not ideal. Additionally this would be harder to validate.
You should not only consider those schemas as a way to "validate" (in this case there is no validation), but also as definitions used to:
create parser/generator/serializer/deserializer & types into other programming languages (and how to do automatic (or semi-auto) translation)
generate documentation
...
What it will look like in an API? A anyOf is often translated into an inheritance or an Union or a Enum. And parser will need a way to select the correct (sub)type. Eg for the value "low" in json is it a enum's member or a string? and same question for "LOW".
@davidB does this definition affect some tooling or something you are using? Im trying to understand what the exact problem is other than it can be more efficiently written.
And it isn't just documenting. It's specifically saying, this is a string, and here are the list of possible values. The latter point is very important. If we just say string, then that means anything, but we want to limit that.
If there is tooling that is being used that is having trouble with this definition, can we get some more information on that? That may help us figure out a way to approach the problem.
@xibz, Yes this definition affect tooling (currently I customize it to say String only). Try to answer the question:
How to you think this json schema should be translated into typed languages (try with your favorite)?
How a parser/deserializer should convert the json into your types? (how the parser select the right definition String or Enum (but the enum is a string))
What is the look of a generated documentation (from the type and from the json schema, eg when this json schema is imported into an OpenAPI documentation)?
is "low" (enum) same as "low" (string) in typed language and what about "LOW"?
With a tool like https://app.quicktype.io/, it becomes a simple String, but if I do word to word manual translation of this definition in rust it should be something like:
But on parsing, deserialization PriorityEnum will never be used, because it requires custom code to say if the string value in ... then use enum, else use string. (My issue is the need of this custom code for each type/field)
After playing around some with the JSON schema, I believe anyOf should be oneOf. I noticed you did mention that oneOf would complain about the issue, but would still be fine. Would that work for you, @davidB?
oneOf is better IMHO in term of types and how to deserialize data. oneOf is like XOR, a value can be A or B but not both. I guess that a json validator will failed with the current definition because the enum and the string are not mutually exclusive (eg with a "low"). oneOf use discriminator or "duck typing" to check that there is no overlap between possible type.
IMO, oneOf highlights the issue. Using the enum to show some possible value is not the right approach.
To be clear what will work for me (and for tool and reader) is something like:
"priority": {
"description": "An indicator of the importance of the ticket",
"examples": [
"low",
"medium",
"high"
],
"type": "string"
}
@davidB Because a string'd enum is different than an normal enum, e.g. C where it is an integer, and we also want to explicitly say it is a string, and not anything else. It's mostly for clarity.
@davidB No, they don't have to be strings. We are explicitly saying that they are strings. This is important because anyone wanting to make a change to the spec, will know that it is explicitly as a string.
"oneOf": [
{
"enum": [
1, <- this is valid"low",
"medium",
"high"
]
}
]
@davidB Oh, sorry, I misunderstood what you are asking. It's not an array in practice. That's just part of the spec.
Basically it is saying, we have some enum value, and it can be a, b, or c. The a, b, or c is just a list in the schema, but the list itself is not represented in code to be generated, etc.
@afrittoli I think that makes sense except for generating based on examples. Mostly that examples does not seem like the appropriate place to generate defaults from. However, if we want to change the semantics of examples then Im fine with it.
This isn't used for validation, but may help with explaining the effect and purpose of the schema to a reader
While we could generate from there, other enums from examples may cause issues if we do not plan to have them as generated. So it feels hacky.
Im personally fine with the current definition, as it is valid. However, if there is a more correct way to write it, I'd be for it. I don't know if examples is the correct way though. With that said, though, Im not too overly worried about it and I think if it unblocks @davidB, we can move forward with it in v0.5.
changed the title [-]remove confusing type `Enum or String`[/-][+]remove confusing type `Enum or String` (a set of values could not be closed and open)[/+]on Feb 12, 2025
Activity
afrittoli commentedon May 10, 2024
@davidB we added the
anyOf(string, enum)on purpose so that we could suggest some known values in the JSON schema (and not only in the docs), but still accept others. We are aware that validation can in fact only validate that values are a string but we use this as a way to document well known values.The initial approach was to only have an
enumwithotheras one of the values, which was limiting because it would not let users carry the value they needed in their event.An alternative I considered was keeping
otherand adding another field, to be used only whenotheris used. With this option, a consumer does not know in advance which field contains the expected value, which is not ideal. Additionally this would be harder to validate.davidB commentedon May 19, 2024
If the purpose is to document, why not use the field
descriptionorexamples(or a custom fieldx-...)?https://json-schema.org/understanding-json-schema/reference/annotations
You should not only consider those schemas as a way to "validate" (in this case there is no validation), but also as definitions used to:
What it will look like in an API? A
anyOfis often translated into an inheritance or an Union or a Enum. And parser will need a way to select the correct (sub)type. Eg for the value "low" in json is it a enum's member or a string? and same question for "LOW".xibz commentedon May 19, 2024
@davidB does this definition affect some tooling or something you are using? Im trying to understand what the exact problem is other than it can be more efficiently written.
And it isn't just documenting. It's specifically saying, this is a string, and here are the list of possible values. The latter point is very important. If we just say string, then that means anything, but we want to limit that.
If there is tooling that is being used that is having trouble with this definition, can we get some more information on that? That may help us figure out a way to approach the problem.
davidB commentedon May 20, 2024
@xibz, Yes this definition affect tooling (currently I customize it to say String only). Try to answer the question:
With a tool like https://app.quicktype.io/, it becomes a simple String, but if I do word to word manual translation of this definition in rust it should be something like:
But on parsing, deserialization PriorityEnum will never be used, because it requires custom code to say if the string value in ... then use enum, else use string. (My issue is the need of this custom code for each type/field)
And for user it also complexity the usage.
json enum are for exhausted list.
xibz commentedon May 20, 2024
After playing around some with the JSON schema, I believe
anyOfshould beoneOf. I noticed you did mention thatoneOfwould complain about the issue, but would still be fine. Would that work for you, @davidB?davidB commentedon May 20, 2024
oneOfis better IMHO in term of types and how to deserialize data.oneOfis like XOR, a value can be A or B but not both. I guess that a json validator will failed with the current definition because the enum and the string are not mutually exclusive (eg with a "low").oneOfuse discriminator or "duck typing" to check that there is no overlap between possible type.IMO,
oneOfhighlights the issue. Using the enum to show some possible value is not the right approach.To be clear what will work for me (and for tool and reader) is something like:
xibz commentedon May 20, 2024
I think
anyOfis still valid in this case, but I could see why it is confusing. I think the more confusing portion is hereI will go ahead and mark this as a bug, and we can fix it to look like,
Would that work for you? I think this will solve all the issues you are seeing at least with the tool
davidB commentedon May 20, 2024
in this case why using a
oneOf? (there is only one possible type the enum)xibz commentedon May 20, 2024
@davidB Because a string'd enum is different than an normal enum, e.g. C where it is an integer, and we also want to explicitly say it is a string, and not anything else. It's mostly for clarity.
davidB commentedon May 20, 2024
in json-schema
enumarestring.What I mean is why
and not
Sorry I don't understand the value of
oneOfxibz commentedon May 20, 2024
@davidB No, they don't have to be strings. We are explicitly saying that they are strings. This is important because anyone wanting to make a change to the spec, will know that it is explicitly as a string.
davidB commentedon May 20, 2024
My question is why using a "oneOf" with an array of 1 value (the enum)?
xibz commentedon May 20, 2024
@davidB Oh, sorry, I misunderstood what you are asking. It's not an array in practice. That's just part of the spec.
Basically it is saying, we have some
enumvalue, and it can bea,b, orc. Thea,b, orcis just a list in the schema, but the list itself is not represented in code to be generated, etc.I hope that makes sense?
davidB commentedon May 20, 2024
oneOfandenumare unrelated.enumare to restrict value (https://json-schema.org/understanding-json-schema/reference/enum)oneOfare to choose a type into a list (it's why I don't see the value of a list of 1 element)8 remaining items
xibz commentedon May 20, 2024
@davidB
Something like this would be sufficient
Then if a user wanted a custom value they could do something like
afrittoli commentedon May 20, 2024
@xibz We discussed this during the working group today and came to this proposal, let us know what you think:
stringas the type for fields that use theanyOfexamplesfield using some custom code in the generator, e.g.FYI @e-backmark-ericsson @rjalander
xibz commentedon May 20, 2024
@afrittoli I think that makes sense except for generating based on
examples. Mostly thatexamplesdoes not seem like the appropriate place to generate defaults from. However, if we want to change the semantics of examples then Im fine with it.While we could generate from there, other enums from examples may cause issues if we do not plan to have them as generated. So it feels hacky.
Im personally fine with the current definition, as it is valid. However, if there is a more correct way to write it, I'd be for it. I don't know if
examplesis the correct way though. With that said, though, Im not too overly worried about it and I think if it unblocks @davidB, we can move forward with it in v0.5.afrittoli commentedon Jul 22, 2024
Different modelling languages might allows us to express this better, so that's something we should look into.
xibz commentedon Dec 2, 2024
cdevents/implementation-wg#5
I believe a new modeling language(s) may help with this, but we may still need some consistency across definitions, specifically enums.
{ "enum": "value" }afrittoli commentedon Feb 10, 2025
A solution to this depends on the work being done on the modelling language, as long as the language supports modelling it.
@xibz example: https://stackoverflow.com/questions/3978654/best-way-to-create-enum-of-strings
We've not used
enumproperly so far, so what we need is:[-]remove confusing type `Enum or String`[/-][+]remove confusing type `Enum or String` (a set of values could not be closed and open)[/+]