-
Notifications
You must be signed in to change notification settings - Fork 0
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 #13 from toririm/feature/type-safe
モデルのスキーマに基づいた統一的なバリデーションを実現
- Loading branch information
Showing
8 changed files
with
148 additions
and
58 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { | ||
DocumentData, | ||
QueryDocumentSnapshot, | ||
SnapshotOptions, | ||
} from "firebase/firestore"; | ||
import type { ZodSchema } from "zod"; | ||
|
||
export const converter = <T>(schema: ZodSchema<T>) => { | ||
return { | ||
toFirestore: (data: T) => { | ||
return data as DocumentData; | ||
}, | ||
fromFirestore: ( | ||
snapshot: QueryDocumentSnapshot, | ||
options: SnapshotOptions, | ||
) => { | ||
const data = snapshot.data(options); | ||
return schema.parse(data); | ||
}, | ||
}; | ||
}; |
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,28 +1,24 @@ | ||
import { | ||
collection, | ||
type DocumentData, | ||
onSnapshot, | ||
query, | ||
} from "firebase/firestore"; | ||
import { type SWRSubscription } from "swr/subscription"; | ||
import { collection, onSnapshot, query } from "firebase/firestore"; | ||
import type { SWRSubscription } from "swr/subscription"; | ||
import { type ZodSchema } from "zod"; | ||
import { converter } from "./converter"; | ||
import { db } from "./firestore"; | ||
|
||
// データの型はあとでマシなものにする | ||
export const collectionSub: SWRSubscription<string, DocumentData[], Error> = ( | ||
key, | ||
{ next }, | ||
) => { | ||
const unsub = onSnapshot( | ||
query(collection(db, key)), | ||
(snapshot) => { | ||
next( | ||
null, | ||
snapshot.docs.map((doc) => doc.data()), | ||
); | ||
}, | ||
(err) => { | ||
next(err); | ||
}, | ||
); | ||
return unsub; | ||
export const collectionSub = <T>(schema: ZodSchema<T>) => { | ||
const sub: SWRSubscription<string, T[], Error> = (key, { next }) => { | ||
const unsub = onSnapshot( | ||
query(collection(db, key)).withConverter(converter(schema)), | ||
(snapshot) => { | ||
next( | ||
null, | ||
snapshot.docs.map((doc) => doc.data()), | ||
); | ||
}, | ||
(err) => { | ||
next(err); | ||
}, | ||
); | ||
return unsub; | ||
}; | ||
return sub; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { z } from "zod"; | ||
|
||
export const itemtypes = ["hot", "ice", "ore", "milk"] as const; | ||
|
||
export const itemSchema = z.object({ | ||
name: z.string({ required_error: "名前が未入力です" }), | ||
price: z.number({ required_error: "価格が未入力です" }), | ||
type: z.enum(itemtypes, { | ||
required_error: "種類が未選択です", | ||
invalid_type_error: "不正な種類です", | ||
}), | ||
}); | ||
|
||
export type Item = z.infer<typeof itemSchema>; | ||
|
||
export type ItemType = Pick<Item, "type">["type"]; |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from "zod"; | ||
import { itemSchema } from "./item"; | ||
|
||
export const orderSchema = z.object({ | ||
orderId: z.number(), | ||
createdAt: z.date(), | ||
servedAt: z.date().nullable(), | ||
items: z.array(itemSchema), | ||
assignee: z.string().nullable(), | ||
total: z.number(), | ||
orderReady: z.boolean(), | ||
}); | ||
|
||
export type Order = z.infer<typeof orderSchema>; |
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
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