Skip to content

Commit

Permalink
Merge pull request #50 from mikucat0309/refactor-schema-zod
Browse files Browse the repository at this point in the history
Refactor schema with zod
  • Loading branch information
elct9620 authored Nov 27, 2023
2 parents 5ad0a84 + 3b11af6 commit cd5c7df
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 57 deletions.
12 changes: 8 additions & 4 deletions api/schema/attendee.ts
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()
})
13 changes: 9 additions & 4 deletions api/schema/booth.ts
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()
})
8 changes: 7 additions & 1 deletion api/schema/localized.ts
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' })
48 changes: 24 additions & 24 deletions api/schema/puzzle.ts
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)
24 changes: 13 additions & 11 deletions api/schema/scenario.ts
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,
})
24 changes: 13 additions & 11 deletions api/schema/status.ts
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,
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"vite": "^4.4.9",
"vitest": "^0.x",
"vue-tsc": "^1.8.8",
"wrangler": "3.8.0"
"wrangler": "3.8.0",
"zod": "^3.22.4"
},
"dependencies": {
"@cloudflare/itty-router-openapi": "^1.0.5",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,7 @@ yup@^0.32.11:
property-expr "^2.0.4"
toposort "^2.0.2"

zod@^3.20.6, zod@^3.21.4:
zod@^3.20.6, zod@^3.21.4, zod@^3.22.4:
version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==

0 comments on commit cd5c7df

Please sign in to comment.