Skip to content

Commit

Permalink
Switch everywhere from Union[Literal] to Enum
Browse files Browse the repository at this point in the history
The generated schema is cleaner this way and it's more obious what
is happening.
  • Loading branch information
p2004a committed Sep 15, 2024
1 parent 1b77a23 commit f388a19
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 56 deletions.
6 changes: 1 addition & 5 deletions docs/schema/matchmaking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 5 additions & 5 deletions docs/schema/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
Expand Down
27 changes: 10 additions & 17 deletions schema/compiled.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
]
}
},
Expand Down Expand Up @@ -1915,11 +1908,11 @@
},
"countryCode": { "type": "string" },
"status": {
"anyOf": [
{ "const": "offline" },
{ "const": "menu" },
{ "const": "playing" },
{ "const": "lobby" }
"enum": [
"offline",
"menu",
"playing",
"lobby"
]
}
}
Expand Down
9 changes: 1 addition & 8 deletions schema/definitions/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions schema/matchmaking/cancelled/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 5 additions & 5 deletions schema/user/updated/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
},
"countryCode": { "type": "string" },
"status": {
"anyOf": [
{ "const": "offline" },
{ "const": "menu" },
{ "const": "playing" },
{ "const": "lobby" }
"enum": [
"offline",
"menu",
"playing",
"lobby"
]
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/schema/definitions/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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" }
);
7 changes: 2 additions & 5 deletions src/schema/matchmaking/cancelled.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"]),
}),
},
});

0 comments on commit f388a19

Please sign in to comment.