Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 93d47c1

Browse files
committed
fix: json lacks undefined
Booo!
1 parent 48bbe96 commit 93d47c1

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/data/schemas.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,45 @@ const roomStrictSchema = z.custom<keyof typeof rooms>(
5252
*/
5353
const roomSchema = z.union([roomStrictSchema, z.literal("")]).readonly();
5454

55+
/**
56+
* Coerces a null value to undefined.
57+
*
58+
* @param val - Something possibly nullable.
59+
* @returns Something, but the null value is coerced to undefined.
60+
*/
61+
function coerceNullToUndefined(val: unknown): unknown {
62+
return val ?? undefined;
63+
}
64+
5565
/**
5666
* Represents the schema for profiles data.
5767
*/
5868
const profilesSchema = z.union([
59-
z.tuple([z.undefined()]).rest(z.union([z.string(), z.string().array()])),
69+
z
70+
.tuple([z.preprocess(coerceNullToUndefined, z.undefined())])
71+
.rest(z.union([z.string(), z.string().array()])),
6072
z.tuple([]),
6173
]);
6274

6375
/**
6476
* Represents the schema for an individual profile.
6577
*/
66-
const profileSchema = z.array(z.tuple([z.string(), z.string()]));
78+
const profileSchema = z.tuple([z.string(), z.string()]);
6779

6880
/**
6981
* Represents the schema for a list of profiles.
7082
*/
7183
// TODO(ParkerH27): Make this a Map.
7284
const profilesListSchema = z.union([
73-
z.tuple([profilesSchema]).rest(profileSchema),
85+
z.tuple([profilesSchema]).rest(profileSchema.array()),
7486
z.tuple([]),
7587
]);
7688

77-
const asyncProfilesListSchema = z.promise(profilesListSchema.nullish());
78-
7989
const numberIndexSchema = z.custom<NumberIndex>((val) =>
80-
z
81-
.string()
82-
.refine((val2) => !isNaN(Number(val2)))
83-
.safeParse(val),
90+
z.string().regex(/^\d+$/).safeParse(val),
8491
);
8592

8693
export {
87-
asyncProfilesListSchema,
8894
isKey,
8995
numberIndexSchema,
9096
profilesListSchema,

src/script.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ import type { Coords2D, Level, Lvl, StairList } from "./data/data-types.ts";
2323
import { level0, level1, level2 } from "./data/levels.ts";
2424
import { rooms } from "./data/rooms.ts";
2525
import {
26-
asyncProfilesListSchema,
26+
isKey,
27+
numberIndexSchema,
2728
profilesListSchema,
2829
roomSchema,
29-
numberIndexSchema,
3030
type ProfilesList,
3131
type Room,
32-
isKey,
3332
} from "./data/schemas.ts";
3433
import { btmStairs, stairs } from "./data/stairs.ts";
3534

@@ -202,10 +201,8 @@ function createCourse(num: number, profNum: number): void {
202201
const zodErrorElement = document.getElementById("zod-error");
203202

204203
async function applySavedProfiles(): Promise<void> {
205-
const unparsedProfiles = await asyncProfilesListSchema.parse(
206-
storage.getItem("profiles"),
207-
);
208-
const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles);
204+
const unparsedProfiles = await storage.getItem("profiles");
205+
const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles ?? []);
209206

210207
if (parsedProfiles.success) {
211208
profiles = parsedProfiles.data;

0 commit comments

Comments
 (0)