-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from mikucat0309/refactor-schema-zod
Refactor schema with zod
- Loading branch information
Showing
8 changed files
with
77 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
export type AttendeeMetadata = Record<string, any> | ||
import { z } from 'zod' | ||
|
||
export type BasicAttendeeInfo = { | ||
nickname: string | ||
} | ||
export type AttendeeMetadata = z.infer<typeof attendeeMetadataSchema> | ||
export const attendeeMetadataSchema = z.record(z.any()) | ||
|
||
export type BasicAttendeeInfo = z.infer<typeof basicAttendeeInfoSchema> | ||
export const basicAttendeeInfoSchema = z.object({ | ||
nickname: z.string() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
export type BoothList = string[] | ||
export type BoothStaff = { | ||
slug: string | ||
} | ||
import { z } from "zod" | ||
|
||
export type BoothList = z.infer<typeof boothListSchema> | ||
export const boothListSchema = z.array(z.string()) | ||
|
||
export type BoothStaff = z.infer<typeof boothStaffSchema> | ||
export const boothStaffSchema = z.object({ | ||
slug: z.string() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { z } from 'zod' | ||
|
||
export enum Languages { | ||
zhTW = 'zh-TW', | ||
enUS = 'en-US', | ||
} | ||
|
||
export type LocalizedText = Partial<Record<Languages, string>> | ||
export type LocalizedText = z.infer<typeof localizedTextSchema> | ||
export const localizedTextSchema = z | ||
.record(z.nativeEnum(Languages), z.string()) | ||
.describe('RFC5646 Language Tag => Text') | ||
.default({ 'en-US': 'Check-in' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
export type PuzzleStatus = { | ||
user_id: string | ||
puzzles: string[] | ||
deliverers: string[] | ||
valid: number | null | ||
coupon: number | null | ||
} | ||
import { z } from 'zod' | ||
|
||
export type PuzzleStatus = z.infer<typeof puzzleStatusSchema> | ||
|
||
export const puzzleStatusSchema = z.object({ | ||
user_id: z.string().default('user1234'), | ||
puzzles: z.array(z.string().default('a')), | ||
deliverers: z.array(z.string().default('OCF')), | ||
valid: z.number().nullable(), | ||
coupon: z.number().nullable(), | ||
}) | ||
|
||
export type DeliverPuzzlePayload = { | ||
receiver: string | ||
} | ||
|
||
export type PuzzleDeliveredResponse = { | ||
status: 'OK' | ||
/** | ||
* Receiver's display name. | ||
* | ||
* @example | ||
* | ||
* "Aotoki" | ||
*/ | ||
user_id: string | ||
} | ||
export type PuzzleDeliveredResponse = z.infer<typeof puzzleDeliveredResponseSchema> | ||
export const puzzleDeliveredResponseSchema = z.object({ | ||
status: z.string().default('OK'), | ||
user_id: z.string().default('user1234'), | ||
}) | ||
|
||
export type PuzzleItemStat = { | ||
puzzle: string | ||
quantity: number | ||
currency: number | ||
} | ||
export type PuzzleItemStat = z.infer<typeof puzzleItemStatSchema> | ||
export const puzzleItemStatSchema = z.object({ | ||
puzzle: z.string().default('a'), | ||
quantity: z.number().default(1234), | ||
currency: z.number().default(5678), | ||
}) | ||
|
||
export type PuzzleStats = PuzzleItemStat[] | ||
export type PuzzleStats = z.infer<typeof puzzleStatsSchema> | ||
export const puzzleStatsSchema = z.array(puzzleItemStatSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { LocalizedText } from './localized' | ||
import { AttendeeMetadata } from './attendee' | ||
import { localizedTextSchema } from './localized' | ||
import { attendeeMetadataSchema } from './attendee' | ||
import { Path, Str } from '@cloudflare/itty-router-openapi' | ||
import { z } from 'zod' | ||
|
||
export const ScenarioIdPath = Path(Str, { description: 'scenarion name to apply' }) | ||
|
||
export type Scenario = { | ||
order: number | ||
available_time: number | null | ||
expire_time: number | null | ||
display_text: LocalizedText | ||
used: number | null | ||
disabled: string | null | ||
attr: AttendeeMetadata | ||
} | ||
export type Scenario = z.infer<typeof scenarioSchema> | ||
export const scenarioSchema = z.object({ | ||
order: z.number().describe('display order').default(1), | ||
available_time: z.number().nullable().describe('timestamp').default(1600000000), | ||
expire_time: z.number().nullable().describe('timestamp').default(1700000000), | ||
display_text: localizedTextSchema, | ||
used: z.number().nullable().describe('timestamp').default(1650000000), | ||
disabled: z.string().nullable(), | ||
attr: attendeeMetadataSchema, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { Scenario } from './scenario' | ||
import { AttendeeMetadata } from './attendee' | ||
import { scenarioSchema } from './scenario' | ||
import { attendeeMetadataSchema } from './attendee' | ||
import { z } from 'zod' | ||
|
||
export type Status = { | ||
event_id: string | ||
public_token: string | ||
user_id: string | ||
first_use: number | null | ||
role: string | ||
scenario: Record<string, Scenario> | ||
attr: AttendeeMetadata | ||
} | ||
export type Status = z.infer<typeof statusSchema> | ||
export const statusSchema = z.object({ | ||
event_id: z.string().default('COSCUP_2023'), | ||
public_token: z.string().default('8b1619a7-347c-477a-9045-e48a7828a235'), | ||
user_id: z.string().default('user1234'), | ||
first_use: z.number().nullable().describe('timestamp').default(1650000000), | ||
role: z.string().default('audience'), | ||
scenario: z.record(scenarioSchema).describe('Scenario ID => Scenario'), | ||
attr: attendeeMetadataSchema, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters