Repository navigation
Generating a JSON schema with descriptions for an type.enumerated field of an object #1360
|
Hi! I'm trying to output a JSON schema with descriptions. I have a few string enums, that I managed to describe the variants of, using this syntax (instead of type.enumerated): const FooEnum = type(['"one"', '@', 'one']).or(type(['"two"', '@', 'two']))Using that enum in a object type works: const Foo = type({
bar: FooEnum
})The generated JSON schema looks like this: {
"type": "object",
"properties": {
"bar": {
"anyOf": [
{
"const": "one",
"description": "one"
},
{
"const": "two",
"description": "two"
}
]
}
},
"required": [
"bar"
]
}But then, I also want to describe the object's field itself: const Foo = type({
bar: FooEnum.describe("Bar's description")
})Unfortunately, the last .describe call overrides the metadata set for each branch of the union: {
"type": "object",
"properties": {
"bar": {
"anyOf": [
{
"const": "one",
"description": "Bar's description"
},
{
"const": "two",
"description": "Bar's description"
}
],
"description": "Bar's description"
}
},
"required": [
"bar"
]
}I tried to work around it using the tuple style syntax: const Foo = type({
bar: [FooEnum, "@", "Bar's description"]
})But it does the same thing. Is this possible? |
Replies: 1 comment
|
In ArkType 2.2.8, pass import { type } from "arktype"
const FooEnum = type(['"one"', "@", "one"])
.or(type(['"two"', "@", "two"]))
const Foo = type({
bar: FooEnum.describe("Bar's description", "self")
})
console.log(Foo.toJsonSchema())The {
"anyOf": [
{ "const": "one", "description": "one" },
{ "const": "two", "description": "two" }
],
"description": "Bar's description"
}Tested on 2.2.8, including reproducing the branch-description overwrite without |
In ArkType 2.2.8, pass
"self"as the second argument to.describe(). The default selector is"shallow", which also reaches the union's branches;"self"changes only the union node's metadata.The
barproperty then contains:{ "anyOf": [ { "const": "one", "description": "one" }, { "const": "two", "description": "two" } ], "description": "Bar's description" }Tested on 2.2.8, including reproducing the branch-description overwrite without
"self". This uses the c…