discriminator support? #828
Replies: 2 comments
-
@maxgurewitz Hiya,
TypeBox only officially supports keywords defined in the Json Schema specification, so the The following implements the example schema on the Ajv documentation website. import { Type, Static, TUnion, TSchema, SchemaOptions } from '@sinclair/typebox'
export function Enum<T extends string[]>(values: [...T], options: SchemaOptions = {}) {
return Type.Unsafe<T[number]>({ ...options, enum: values })
}
export function OneOf<T extends TSchema[]>(oneOf: [...T], options: SchemaOptions = {}) {
return Type.Unsafe<Static<TUnion<T>>>({ ...options, oneOf })
}
export const T = OneOf([
Type.Object({
foo: Type.Literal('x'),
a: Type.String()
}),
Type.Object({
foo: Enum(['y', 'z']),
b: Type.String()
})
], {
type: 'object',
discriminator: {propertyName: "foo"},
required: ['foo']
})
// https://ajv.js.org/json-schema.html#discriminator
// {
// type: "object",
// discriminator: {propertyName: "foo"},
// required: ["foo"],
// oneOf: [
// {
// properties: {
// foo: {const: "x"},
// a: {type: "string"},
// },
// required: ["a"],
// },
// {
// properties: {
// foo: {enum: ["y", "z"]},
// b: {type: "string"},
// },
// required: ["b"],
// },
// ],
// } There may be other ways you can implement the above function (factoring the Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
-
Hello! First want to say a huge thanks for this project! Probably my favorite typescript library.
Wanted to ask, does typebox support currently, or plan to support the new
discriminator
keyword?https://ajv.js.org/json-schema.html#discriminator
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions