Skip to content

Commit

Permalink
Add Zod schema for Ambassador
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Dec 10, 2024
1 parent 2d2b60a commit 7c86424
Showing 1 changed file with 59 additions and 34 deletions.
93 changes: 59 additions & 34 deletions src/ambassadors/core.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
import type { IUCNStatus } from "../iucn";
import type { EnclosureKey } from "../enclosures";
import type { PartialDateString, Nullable } from "../types";
import { Class } from "./classification";
import lifespans, { type Lifespan } from "./lifespans";
import { z } from "zod";

export type Ambassadors = typeof ambassadors;
import { isIUCNStatus } from "../iucn";
import { isEnclosureKey } from "../enclosures";
import { isPartialDateString } from "../types";
import { Class } from "./classification";
import lifespans, { lifespanSchema } from "./lifespans";

export type AmbassadorKey = keyof Ambassadors;
export const ambassadorSchema = z.object({
name: z.string(),
alternate: z.array(z.string()).readonly(),
commands: z.array(z.string()).readonly(),
class: z.nativeEnum(Class),
species: z.string(),
scientific: z.string(),
sex: z.enum(["Male", "Female"]).nullable(),
birth: z.string().refine(isPartialDateString).nullable(),
arrival: z.string().refine(isPartialDateString).nullable(),
retired: z.string().refine(isPartialDateString).nullable(),
iucn: z.object({
id: z.number().nullable(),
status: z.string().refine(isIUCNStatus),
}),
enclosure: z.string().refine(isEnclosureKey),
story: z.string(),
mission: z.string(),
native: z.object({
text: z.string(),
source: z.string(),
}),
lifespan: lifespanSchema,
clips: z
.array(
z.object({
id: z.string(),
caption: z.string(),
}),
)
.readonly(),
homepage: z
.object({
title: z.string(),
description: z.string(),
})
.nullable(),
plush: z
.union([
z.object({
link: z.string(),
}),
z.object({
soon: z.string(),
}),
])
.nullable(),
});

export type Ambassador = {
name: string;
alternate: Readonly<string[]>;
commands: Readonly<string[]>;
class: Class;
species: string;
scientific: string;
sex: Nullable<"Male" | "Female">;
birth: Nullable<PartialDateString>;
arrival: Nullable<PartialDateString>;
retired: Nullable<PartialDateString>;
iucn: {
id: Nullable<number>;
status: IUCNStatus;
};
enclosure: EnclosureKey;
story: string;
mission: string;
native: {
text: string;
source: string;
};
lifespan: Lifespan;
clips: Readonly<{ id: string; caption: string }[]>;
homepage: Nullable<{ title: string; description: string }>;
plush: Nullable<{ link: string } | { soon: string }>;
};
export type Ambassador = z.infer<typeof ambassadorSchema>;

const ambassadors = {
// Active ambassadors
Expand Down Expand Up @@ -1271,6 +1292,10 @@ const ambassadors = {
},
} as const satisfies Record<string, Ambassador>;

export type Ambassadors = typeof ambassadors;

export type AmbassadorKey = keyof Ambassadors;

const ambassadorKeys = Object.keys(ambassadors) as AmbassadorKey[];

export const isAmbassadorKey = (str: string): str is AmbassadorKey =>
Expand Down

0 comments on commit 7c86424

Please sign in to comment.