diff --git a/frontend/src/api/my-study/typeChecker.ts b/frontend/src/api/my-study/typeChecker.ts index 6f97f8f17..7bd827acb 100644 --- a/frontend/src/api/my-study/typeChecker.ts +++ b/frontend/src/api/my-study/typeChecker.ts @@ -47,7 +47,7 @@ export const checkMyStudy = (data: unknown): MyStudy => { id: checkType(data.id, isNumber), title: checkType(data.title, isString), startDate: checkType(data.startDate, isDateYMD), - endDate: checkType(data.endDate, isDateYMD), + endDate: checkType(data.endDate, isDateYMD, true), studyStatus: checkType(data.studyStatus, isStudyStatus), tags: checkType(data.tags, isArray).map(tag => checkMyStudyTag(tag)), owner: checkMember(data.owner), diff --git a/frontend/src/api/studies/typeChecker.ts b/frontend/src/api/studies/typeChecker.ts index 771d4db01..e4fde0726 100644 --- a/frontend/src/api/studies/typeChecker.ts +++ b/frontend/src/api/studies/typeChecker.ts @@ -1,9 +1,57 @@ import { AxiosError } from 'axios'; -import { arrayOfAll, checkType, hasOwnProperties, isArray, isBoolean, isObject } from '@utils'; +import { + arrayOfAll, + checkType, + hasOwnProperties, + isArray, + isBoolean, + isNumber, + isObject, + isRecruitmentStatus, + isString, +} from '@utils'; + +import type { Study, Tag } from '@custom-types'; import { type ApiStudies } from '@api/studies'; -import { checkStudy } from '@api/study/typeChecker'; + +type MainStudyTag = Pick; +type MainStudyTagKeys = keyof MainStudyTag; + +const arrayOfAllMainStudyTagKeys = arrayOfAll(); + +const checkMainStudyTag = (data: unknown): MainStudyTag => { + if (!isObject(data)) throw new AxiosError(`Main-Tag does not have correct type: object`); + + const keys = arrayOfAllMainStudyTagKeys(['id', 'name']); + if (!hasOwnProperties(data, keys)) throw new AxiosError('Main-Tag does not have some properties'); + + return { + id: checkType(data.id, isNumber), + name: checkType(data.name, isString), + }; +}; + +type StudyKeys = keyof Study; + +const arrayOfAllStudyKeys = arrayOfAll(); + +const checkStudy = (data: unknown): Study => { + if (!isObject(data)) throw new AxiosError(`Main-Study does not have correct type: object`); + + const keys = arrayOfAllStudyKeys(['id', 'excerpt', 'recruitmentStatus', 'tags', 'thumbnail', 'title']); + if (!hasOwnProperties(data, keys)) throw new AxiosError('Main-Study does not have some properties'); + + return { + id: checkType(data.id, isNumber), + title: checkType(data.title, isString), + excerpt: checkType(data.title, isString), + thumbnail: checkType(data.title, isString), + tags: checkType(data.tags, isArray).map(tag => checkMainStudyTag(tag)), + recruitmentStatus: checkType(data.recruitmentStatus, isRecruitmentStatus), + }; +}; type StudiesKeys = keyof ApiStudies['get']['responseData'];