From f388a191957688298ab8684b8463de13535f30c4 Mon Sep 17 00:00:00 2001 From: Marek Rusinowski Date: Sun, 15 Sep 2024 16:04:37 +0200 Subject: [PATCH] Switch everywhere from Union[Literal] to Enum The generated schema is cleaner this way and it's more obious what is happening. --- docs/schema/matchmaking.md | 6 +----- docs/schema/user.md | 10 ++++----- schema/compiled.json | 27 +++++++++---------------- schema/definitions/user.json | 9 +-------- schema/matchmaking/cancelled/event.json | 6 +----- schema/user/updated/event.json | 10 ++++----- src/schema/definitions/user.ts | 8 ++------ src/schema/matchmaking/cancelled.ts | 7 ++----- 8 files changed, 27 insertions(+), 56 deletions(-) diff --git a/docs/schema/matchmaking.md b/docs/schema/matchmaking.md index 15b3772..690fe6c 100644 --- a/docs/schema/matchmaking.md +++ b/docs/schema/matchmaking.md @@ -193,11 +193,7 @@ Server may send this event at any point when the user is queuing to indicate tha "type": "object", "properties": { "reason": { - "anyOf": [ - { "const": "intentional" }, - { "const": "server_error" }, - { "const": "party_user_left" } - ] + "enum": ["intentional", "server_error", "party_user_left"] } }, "required": ["reason"] diff --git a/docs/schema/user.md b/docs/schema/user.md index 1337f2c..fa9e6de 100644 --- a/docs/schema/user.md +++ b/docs/schema/user.md @@ -71,11 +71,11 @@ Sent by the server to inform the client when subscribed users get updated in som }, "countryCode": { "type": "string" }, "status": { - "anyOf": [ - { "const": "offline" }, - { "const": "menu" }, - { "const": "playing" }, - { "const": "lobby" } + "enum": [ + "offline", + "menu", + "playing", + "lobby" ] } } diff --git a/schema/compiled.json b/schema/compiled.json index 5d8f23a..c700565 100644 --- a/schema/compiled.json +++ b/schema/compiled.json @@ -168,14 +168,7 @@ }, "scopes": { "type": "array", "items": { "type": "string" } }, "countryCode": { "type": "string" }, - "status": { - "anyOf": [ - { "const": "offline" }, - { "const": "menu" }, - { "const": "playing" }, - { "const": "lobby" } - ] - } + "status": { "enum": ["offline", "menu", "playing", "lobby"] } }, "required": [ "userId", @@ -1348,10 +1341,10 @@ "type": "object", "properties": { "reason": { - "anyOf": [ - { "const": "intentional" }, - { "const": "server_error" }, - { "const": "party_user_left" } + "enum": [ + "intentional", + "server_error", + "party_user_left" ] } }, @@ -1915,11 +1908,11 @@ }, "countryCode": { "type": "string" }, "status": { - "anyOf": [ - { "const": "offline" }, - { "const": "menu" }, - { "const": "playing" }, - { "const": "lobby" } + "enum": [ + "offline", + "menu", + "playing", + "lobby" ] } } diff --git a/schema/definitions/user.json b/schema/definitions/user.json index dfd06a3..5648468 100644 --- a/schema/definitions/user.json +++ b/schema/definitions/user.json @@ -10,14 +10,7 @@ "partyId": { "anyOf": [{ "type": "string" }, { "type": "null" }] }, "scopes": { "type": "array", "items": { "type": "string" } }, "countryCode": { "type": "string" }, - "status": { - "anyOf": [ - { "const": "offline" }, - { "const": "menu" }, - { "const": "playing" }, - { "const": "lobby" } - ] - } + "status": { "enum": ["offline", "menu", "playing", "lobby"] } }, "required": [ "userId", diff --git a/schema/matchmaking/cancelled/event.json b/schema/matchmaking/cancelled/event.json index f267e44..ea60668 100644 --- a/schema/matchmaking/cancelled/event.json +++ b/schema/matchmaking/cancelled/event.json @@ -17,11 +17,7 @@ "type": "object", "properties": { "reason": { - "anyOf": [ - { "const": "intentional" }, - { "const": "server_error" }, - { "const": "party_user_left" } - ] + "enum": ["intentional", "server_error", "party_user_left"] } }, "required": ["reason"] diff --git a/schema/user/updated/event.json b/schema/user/updated/event.json index 9282913..0d723b1 100644 --- a/schema/user/updated/event.json +++ b/schema/user/updated/event.json @@ -51,11 +51,11 @@ }, "countryCode": { "type": "string" }, "status": { - "anyOf": [ - { "const": "offline" }, - { "const": "menu" }, - { "const": "playing" }, - { "const": "lobby" } + "enum": [ + "offline", + "menu", + "playing", + "lobby" ] } } diff --git a/src/schema/definitions/user.ts b/src/schema/definitions/user.ts index d9d3745..c0156d9 100644 --- a/src/schema/definitions/user.ts +++ b/src/schema/definitions/user.ts @@ -2,6 +2,7 @@ import { Type } from "@sinclair/typebox"; import { userId } from "@/schema/definitions/userId"; import { Nullable } from "@/typebox-utils"; +import { UnionEnum } from "@/union-enum"; export const user = Type.Object( { @@ -12,12 +13,7 @@ export const user = Type.Object( partyId: Nullable(Type.String()), scopes: Type.Array(Type.String()), countryCode: Type.Optional(Type.String()), - status: Type.Union([ - Type.Literal("offline"), - Type.Literal("menu"), - Type.Literal("playing"), - Type.Literal("lobby"), - ]), + status: UnionEnum(["offline", "menu", "playing", "lobby"]), }, { $id: "user" } ); diff --git a/src/schema/matchmaking/cancelled.ts b/src/schema/matchmaking/cancelled.ts index 7c6dfed..763f601 100644 --- a/src/schema/matchmaking/cancelled.ts +++ b/src/schema/matchmaking/cancelled.ts @@ -1,6 +1,7 @@ import { Type } from "@sinclair/typebox"; import { defineEndpoint } from "@/generator-helpers.js"; +import { UnionEnum } from "@/union-enum"; export default defineEndpoint({ source: "server", @@ -9,11 +10,7 @@ export default defineEndpoint({ "Server may send this event at any point when the user is queuing to indicate that the user has been booted out the matchmaking system.", event: { data: Type.Object({ - reason: Type.Union([ - Type.Literal("intentional"), - Type.Literal("server_error"), - Type.Literal("party_user_left"), - ]), + reason: UnionEnum(["intentional", "server_error", "party_user_left"]), }), }, });