-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for discriminator #760
Comments
@Baptiste-Garcin Hi, TypeBox won't support a DiscrimatedUnion type because it's not part of the Json Schema specification. Ajv does provide support for it, but it needs to be enabled to use (and it's still outside the specification). Note also that TypeScript doesn't have a representation of DiscriminatedUnion either. As it has a structural type system, it has features to deduce a union variant through control flow checks. import { Type, Static } from '@sinclair/typebox'
const A = Type.Object({
type: Type.Literal('A'), // Discriminator
value: Type.Number()
})
const B = Type.Object({
type: Type.Literal('B'), // Discriminator
value: Type.String()
})
const C = Type.Object({
type: Type.Literal('C'), // Discriminator
value: Type.Boolean()
})
const U = Type.Union([A, B, C])
function test(union: Static<typeof U>) {
if(union.type === 'B') { // <- Control Flow Check
union.value // <- Value is String
}
} If you need to represent a property in the schematic that indicates "which" property of the union variants should be used for discrimination, you can optionally add this metadata in with. const U = Type.Union([A, B, C], { discriminator: 'type' }) However, this is not required for validation (it's only usually helpful for tools that might transform a schema into code for nominal type systems). As far as Json Schema goes, the discriminator is implicit by having a unique discriminator field on each union variant. Will close off this issue for now as this functionality is largely out of scope. |
Hi there !
I haven't found any information about a possible support for discriminator in Union types although it is now supported by AJV.
Did I miss something ?
I understand that it's OpenAPI feature and not a JSON schema one but do you think, it's a possible evolution for TypeBox ?
Cheers
The text was updated successfully, but these errors were encountered: