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

Commit

Permalink
fix: zodded typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jan 10, 2024
1 parent dea8c46 commit e8dd5a3
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/data/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ type Lvl = z.infer<typeof lvlSchema>;
type StairList = z.infer<typeof stairListSchema>;
type Level = z.infer<typeof levelSchema>;
type ProfilesList = z.infer<typeof profilesListSchema>;
type Room = z.infer<typeof roomSchema>;
type Room = z.infer<typeof roomStrictSchema>;

const levelSchema = z.array(z.array(z.number()));
/**
* Represents the schema for the level data.
*/
const levelSchema = z.number().array().array();

/**
* Normalizes a room string by removing special characters.
Expand All @@ -25,17 +28,22 @@ function normalizeRoomString(room: string): string {
}

/**
* Helper function for normalizing and validating room strings.
* Represents the custom schema for a room.
*
* @param val - The room string to be normalizing and validating.
* @returns A normalizing and validating room string.
* This schema is used to validate if a value is a valid room.
* A valid room, well, exists in the list of rooms.
*/
const roomSchemaHelper = z
.string()
.toUpperCase()
.trim()
.transform(normalizeRoomString)
.refine((val) => Object.hasOwn(rooms, val) || val === "");
const roomStrictSchema = z.custom<keyof typeof rooms>(
(val) =>
z
.string()
.toUpperCase()
.trim()
.transform(normalizeRoomString)
.refine((val2) => Object.hasOwn(rooms, val2))
.safeParse(val).success,
(val) => ({ message: `${val} is not a room` }),
);

/**
* Represents the custom schema for a room.
Expand All @@ -44,10 +52,7 @@ const roomSchemaHelper = z
* A valid room, well, exists in the list of rooms.
* In addition, this allows an empty string to pass, so that that validation can be caught better.
*/
const roomSchema = z.custom<keyof typeof rooms | "">(
(val) => roomSchemaHelper.safeParse(val).success,
(val) => ({ message: `${val} is not a room` }),
);
const roomSchema = z.union([roomStrictSchema, z.literal("")]).readonly();

/**
* Represents the schema for profiles data.
Expand Down

0 comments on commit e8dd5a3

Please sign in to comment.