Closed huypham50 closed 11 months ago
Problem with enums is that AFAIK there's no room for meta data such as title, description, etc..., we could use oneOf + const:
"gender": {
"oneOf": [
{
"const": "male",
"description": "..."
},
{
"const": "female"
}
],
}
Maybe something like this?
const enumJsonSchema = S.literal('male', 'female')
const anyOfJsonSchema = S.union(S.literal('male'), S.literal('female'))
Yeah, however S.literal('male', 'female')
is just a shorthand for S.union(S.literal('male'), S.literal('female'))
, they yield the same schema.
So I guess we must identify the members of a union that are literals without any meta data, group them toghether and generate for them a JSON Schema in the form of { enum: [...] }
.
Like:
const schema = S.union(
S.literal(1, 2),
S.literal(true).pipe(S.description("description")),
S.string
)
const jsonSchema = JSONSchema.to(schema)
expect(jsonSchema).toEqual({
"$schema": "http://json-schema.org/draft-07/schema#",
"anyOf": [
{ "const": true, "description": "description" },
{
"type": "string",
"description": "a string",
"title": "string"
},
{ "enum": [1, 2] }
]
})
And then S.literal(...)
is just a special case of the above
const schema = S.literal(1, 2)
const jsonSchema = JSONSchema.to(schema)
expect(jsonSchema).toEqual({
"$schema": "http://json-schema.org/draft-07/schema#",
"enum": [1, 2]
})
What is the problem this feature would solve?
Coming from pydantic,
literals
are converted toenums
in json schema:However, in effect, literals will be converted to anyOf
Would slightly prefer enums in this case because it's stricter than anyOf (literals have strict nature imo).
What is the feature you are proposing to solve the problem?
Literals should be converted to enums instead of anyof
What alternatives have you considered?
Current workaround is using enums (not recommended) or records (too verbose)