We are working on a backend with Fastify, declaring schemas with Typebox and generating OpenApi definitions with Fastify Swagger We are facing some problems with enums or similar during docs generation
Let's say we declare a typebox enumerator in one of the following ways
export const ProviderType = Type.Union([
Type.Literal("sms"),
Type.Literal("email"),
]);
//OR
enum Providers {
SMS = "sms",
EMAIL = "email",
}
export const ProviderType = Type.Enum(Providers);
When the open api docs are generated we get this
- schema:
anyOf:
- type: string
enum:
- sms
- type: string
enum:
- email
That is not what we expect, but something like
schema:
type: string
enum: [sms, email]
Otherwise, the places where the enum is used are not correctly rendered by the various packages.
Did you already face this problem?
The TypeBox project does not support enum
ATM as said by the lead maintainer.
It suggests a workaround:
const StringEnum = <T extends string[]>(items: [...T]) =>
Type.Unsafe<T[number]>({ type: "string", enum: items });
export const ProviderType = StringEnum(["email", "sms"]);
// BECOMES
- schema:
type: string
enum:
- email
- sms
So the fastify-swagger
would be using the enum
keyword.