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
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'constA=Type.Object({type: Type.Literal('A'),// Discriminatorvalue: Type.Number()})constB=Type.Object({type: Type.Literal('B'),// Discriminatorvalue: Type.String()})constC=Type.Object({type: Type.Literal('C'),// Discriminatorvalue: Type.Boolean()})constU=Type.Union([A,B,C])functiontest(union: Static<typeofU>){if(union.type==='B'){// <- Control Flow Checkunion.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.
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.
Cheers
S
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: