-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: zod 타입 stringAsInt와 stringAsDate 활용
- Loading branch information
Showing
17 changed files
with
127 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {z} from 'zod'; | ||
|
||
/** | ||
* Express는 query와 params를 모두 string으로 줍니다. | ||
* | ||
* 따라서 만약 숫자로 된 인자를 받고 싶다면 , | ||
* refine()을 통해 숫자 스트링만 걸러낸 뒤에 transform()으로 숫자로 변환해야 합니다. | ||
*/ | ||
|
||
export const stringAsInt = z.string().refine(isInt).transform(toInt); | ||
export const stringAsBoolean = z.string().refine(isBoolean).transform(toBoolean); | ||
export const stringAsDate = z.string().refine(isISOString).transform(toDate); | ||
|
||
import moment from 'moment'; | ||
|
||
export function isInt(value: string): boolean { | ||
return /^-?\d+$/.test(value); | ||
} | ||
|
||
export function toInt(value: string): number { | ||
return parseInt(value, 10); | ||
} | ||
|
||
export function isBoolean(value: string): boolean { | ||
return ['true', 'false'].includes(value); | ||
} | ||
|
||
export function toBoolean(value: string): boolean { | ||
return value === 'true'; | ||
} | ||
|
||
export function isISOString(value: string): boolean { | ||
return moment(value, moment.ISO_8601, true).isValid(); | ||
} | ||
|
||
export function toDate(value: string): Date { | ||
return new Date(value); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import {defineSchema} from '../../libs/schema'; | ||
import {z} from 'zod'; | ||
import {defineRoute} from '../../libs/route'; | ||
import {getCustomRepository} from "typeorm"; | ||
import CommentRepository from "../../libs/application/comments/comment-repository"; | ||
|
||
import {stringAsInt} from '../../libs/zodTypes'; | ||
import CommentService from '../../../service/CommentService'; | ||
|
||
const schema = defineSchema({ | ||
params: { | ||
commentId: z.string(), | ||
}, | ||
params: { | ||
commentId: stringAsInt, | ||
}, | ||
}); | ||
|
||
export default defineRoute('get', '/comment/:commentId?', schema, async (req, res) => { | ||
export default defineRoute('get', '/comment/:commentId', schema, async (req, res) => { | ||
const {commentId} = req.params; | ||
|
||
const {commentId} = req.params; | ||
const Comment_Information = await getCustomRepository(CommentRepository).getComment(commentId); | ||
console.log(Comment_Information) | ||
// res.send(`comment ${commentId}의 id : ${Comment_Information.id} , content : ${Comment_Information.content} , createdAt : ${Comment_Information.createdAt} | ||
// comment를 단 사용자 id :${Comment_Information.user.id} , email : ${Comment_Information.user.email} , nickname :${Comment_Information.user.nickname} , oauthProvider : ${Comment_Information.user.oauthProvider} , createAt :${Comment_Information.user.createdAt} | ||
// comment가 달린 event id: ${Comment_Information.event.id} , host : ${Comment_Information.event.host} , category : ${Comment_Information.event.category} , ${Comment_Information.event.title} , body :${Comment_Information.event.body} , imageUuid: ${Comment_Information.event.imageUuid} , startAt : ${Comment_Information.event.startAt} , endAt : ${Comment_Information.event.endAt} , createAt : ${Comment_Information.event.createdAt}`); | ||
const commentInformation = await CommentService.getComment(commentId); | ||
|
||
return res.json(Comment_Information) | ||
}); | ||
return res.json(commentInformation) | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
import {defineSchema} from '../../libs/schema'; | ||
import {string, z} from 'zod'; | ||
import MakeEvent from '../../../service/event/MakeEvent'; | ||
import {z} from 'zod'; | ||
import {defineRoute} from '../../libs/route'; | ||
import {getGoogleOAuthInfo} from '../../../oauth'; | ||
import { getCustomRepository } from 'typeorm'; | ||
import ReturnUserInformation from '../../../service/ReturnUserInformation'; | ||
import EventService from '../../../service/EventService'; | ||
import UserService from '../../../service/UserService'; | ||
import {stringAsDate} from '../../libs/zodTypes'; | ||
|
||
const schema = defineSchema({ | ||
body: { | ||
//일단 accessToken으로 받아옴 | ||
user: z.string(), | ||
host:z.string(), | ||
category: z.string(), | ||
title: z.string(), | ||
body: z.string(), | ||
imageUuid: z.string().optional(), | ||
startAt:z.date().optional(), | ||
endAt:z.date().optional(), | ||
} | ||
body: { | ||
host: z.string(), | ||
category: z.string(), | ||
title: z.string(), | ||
body: z.string(), | ||
imageUuid: z.string().optional(), | ||
startAt: stringAsDate.optional(), | ||
endAt: stringAsDate.optional(), | ||
} | ||
}); | ||
|
||
export default defineRoute('post', '/event', schema, async (req, res) => { | ||
console.log('make Event!'); | ||
const {user,host,category,title, body, imageUuid, startAt, endAt } = req.body; | ||
const Userstatus = await ReturnUserInformation.returnUser(user); | ||
await MakeEvent.makeEvent(Userstatus,host,category,title, body, imageUuid, startAt, endAt); | ||
res.sendStatus(201); //success | ||
console.log('make Event!'); | ||
|
||
const {userId} = req; | ||
const {host, category, title, body, imageUuid, startAt, endAt} = req.body; | ||
|
||
const Userstatus = await UserService.getUser(userId) | ||
await EventService.makeEvent(Userstatus, host, category, title, body, imageUuid, startAt, endAt); | ||
|
||
res.sendStatus(201); //success | ||
}); | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import {defineRoute} from '../libs/route'; | ||
import {defineSchema} from '../libs/schema'; | ||
import {stringAsDate} from '../libs/zodTypes'; | ||
|
||
const schema = defineSchema({ | ||
body: { | ||
aa: stringAsDate | ||
} | ||
}); | ||
|
||
export default defineRoute('get', '/hello', schema, async (req, res) => { | ||
export default defineRoute('post', '/hello', schema, async (req, res) => { | ||
console.log('요청옴!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); | ||
|
||
console.log(req.body.aa); | ||
|
||
return res.send('ㅎㅇㅎㅇ'); | ||
}); |
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
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