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

Commit

Permalink
fix: json lacks undefined
Browse files Browse the repository at this point in the history
Booo!
  • Loading branch information
lishaduck committed Jan 14, 2024
1 parent 48bbe96 commit 93d47c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/data/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,45 @@ const roomStrictSchema = z.custom<keyof typeof rooms>(
*/
const roomSchema = z.union([roomStrictSchema, z.literal("")]).readonly();

/**
* Coerces a null value to undefined.
*
* @param val - Something possibly nullable.
* @returns Something, but the null value is coerced to undefined.
*/
function coerceNullToUndefined(val: unknown): unknown {
return val ?? undefined;
}

/**
* Represents the schema for profiles data.
*/
const profilesSchema = z.union([
z.tuple([z.undefined()]).rest(z.union([z.string(), z.string().array()])),
z
.tuple([z.preprocess(coerceNullToUndefined, z.undefined())])
.rest(z.union([z.string(), z.string().array()])),
z.tuple([]),
]);

/**
* Represents the schema for an individual profile.
*/
const profileSchema = z.array(z.tuple([z.string(), z.string()]));
const profileSchema = z.tuple([z.string(), z.string()]);

/**
* Represents the schema for a list of profiles.
*/
// TODO(ParkerH27): Make this a Map.
const profilesListSchema = z.union([
z.tuple([profilesSchema]).rest(profileSchema),
z.tuple([profilesSchema]).rest(profileSchema.array()),
z.tuple([]),
]);

const asyncProfilesListSchema = z.promise(profilesListSchema.nullish());

const numberIndexSchema = z.custom<NumberIndex>((val) =>
z
.string()
.refine((val2) => !isNaN(Number(val2)))
.safeParse(val),
z.string().regex(/^\d+$/).safeParse(val),
);

export {
asyncProfilesListSchema,
isKey,
numberIndexSchema,
profilesListSchema,
Expand Down
11 changes: 4 additions & 7 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import type { Coords2D, Level, Lvl, StairList } from "./data/data-types.ts";
import { level0, level1, level2 } from "./data/levels.ts";
import { rooms } from "./data/rooms.ts";
import {
asyncProfilesListSchema,
isKey,
numberIndexSchema,
profilesListSchema,
roomSchema,
numberIndexSchema,
type ProfilesList,
type Room,
isKey,
} from "./data/schemas.ts";
import { btmStairs, stairs } from "./data/stairs.ts";

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

async function applySavedProfiles(): Promise<void> {
const unparsedProfiles = await asyncProfilesListSchema.parse(
storage.getItem("profiles"),
);
const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles);
const unparsedProfiles = await storage.getItem("profiles");
const parsedProfiles = profilesListSchema.safeParse(unparsedProfiles ?? []);

if (parsedProfiles.success) {
profiles = parsedProfiles.data;
Expand Down

0 comments on commit 93d47c1

Please sign in to comment.