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"
Oh! I completely mistook what you were saying! Gotcha! hahah, now I see the confusion. Okay, yea, this is definitely not modeled correctly. Okay, I will have this fixed with
if a, b, c should not be represented in code, then enum is not the right json-schema medium. It's why I suggest examples.
Also note that using examples will also help to be in sync in the md files (and maybe to generate part of them) and tool like schemathesis,...
Okay, I think I understand what was trying to be modeled. So basically we want any string, but want to provide some defaults, like low, medium, high in the SDKs. I think this is primarily used to generate some defaults in the SDK.
Just providing string is fine, and having it documented that the string is low, medium, or high, but that isn't as useful as providing that in an SDK.
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"
}
Interesting - is examples here a custom field? Does jsonschema allow for that?
I would like the SDK to include several constants which correspond to the values in examples, so that SDK users can benefit from those or add their own value - would it be possible to achieve that with a custom schema field?
@davidB Examples are nice, but I think it's really important to have the values modeled in the SDKs. I believe examples, will not be used in code generation? So it may not be as useful
@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
xibz commentedon May 20, 2024
Oh! I completely mistook what you were saying! Gotcha! hahah, now I see the confusion. Okay, yea, this is definitely not modeled correctly. Okay, I will have this fixed with
davidB commentedon May 20, 2024
if
a,b,cshould not be represented in code, thenenumis not the right json-schema medium. It's why I suggestexamples.Also note that using
exampleswill also help to be in sync in the md files (and maybe to generate part of them) and tool like schemathesis,...xibz commentedon May 20, 2024
Okay, I think I understand what was trying to be modeled. So basically we want any string, but want to provide some defaults, like
low,medium,highin the SDKs. I think this is primarily used to generate some defaults in the SDK.Just providing
stringis fine, and having it documented that the string islow,medium, orhigh, but that isn't as useful as providing that in an SDK.afrittoli commentedon May 20, 2024
Interesting - is
exampleshere a custom field? Does jsonschema allow for that?I would like the SDK to include several constants which correspond to the values in
examples, so that SDK users can benefit from those or add their own value - would it be possible to achieve that with a custom schema field?davidB commentedon May 20, 2024
examplesis part of json-schema draft 6, see https://json-schema.org/understanding-json-schema/reference/annotationsand used by some OpenAPI and contract-testing tool like schemathesis to validate API or generate samples or validate API
xibz commentedon May 20, 2024
@davidB Examples are nice, but I think it's really important to have the values modeled in the SDKs. I believe
examples, will not be used in code generation? So it may not be as usefuldavidB commentedon May 20, 2024
@xibz Can you provide an example (in a programming language) about how you think those values could be modeled in the SDKs?
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)[/+]