+++ lib/domain/content-form.ts
... | ... | @@ -0,0 +1,272 @@ |
| 1 | +/** | |
| 2 | + * 콘텐츠관리 등록·수정 폼의 입력 규칙 — 화면과 Server Action이 같은 규칙을 본다. | |
| 3 | + * 검증의 최종 판정은 Server Action이 하고, 화면의 required는 편의일 뿐이다. | |
| 4 | + */ | |
| 5 | + | |
| 6 | +import { | |
| 7 | + ANSWER_MESSAGE_ETC, | |
| 8 | + ANSWER_MESSAGE_OPTIONS, | |
| 9 | + MIN_COMIC_CUTS, | |
| 10 | + QUIZ_ANSWER_OPTIONS, | |
| 11 | + QUIZ_LEVEL_OPTIONS, | |
| 12 | + findSchoolLevel, | |
| 13 | + gradeOptionsOf, | |
| 14 | + type ContentKeyword, | |
| 15 | +} from '@/lib/domain/content'; | |
| 16 | + | |
| 17 | +export const TITLE_MAX_LENGTH = 200; | |
| 18 | +export const SUBJECT_MAX_LENGTH = 200; | |
| 19 | +export const KEYWORD_MAX_LENGTH = 50; | |
| 20 | +export const KEYWORD_DESCRIPTION_MAX_LENGTH = 200; | |
| 21 | +export const MESSAGE_MAX_LENGTH = 2000; | |
| 22 | +export const QUESTION_MAX_LENGTH = 500; | |
| 23 | +export const ANSWER_MESSAGE_MAX_LENGTH = 200; | |
| 24 | + | |
| 25 | +export type ContentFormErrors = Record<string, string>; | |
| 26 | + | |
| 27 | +export type ContentFormState = | |
| 28 | + | { status: 'idle' } | |
| 29 | + | { status: 'error'; message?: string; errors?: ContentFormErrors } | |
| 30 | + | { status: 'success'; id?: string }; | |
| 31 | + | |
| 32 | +export const INITIAL_CONTENT_FORM_STATE: ContentFormState = { status: 'idle' }; | |
| 33 | + | |
| 34 | +export type GradeSelection = { | |
| 35 | + schoolLevel: string; | |
| 36 | + grades: string[]; | |
| 37 | +}; | |
| 38 | + | |
| 39 | +export type ShortsFormValues = GradeSelection & { | |
| 40 | + title: string; | |
| 41 | + subject: string; | |
| 42 | + keywords: ContentKeyword[]; | |
| 43 | + message: string; | |
| 44 | + videoFileId: string; | |
| 45 | + thumbnailFileId: string; | |
| 46 | + isVisible: boolean; | |
| 47 | +}; | |
| 48 | + | |
| 49 | +export type ComicFormValues = GradeSelection & { | |
| 50 | + name: string; | |
| 51 | + subject: string; | |
| 52 | + keywords: ContentKeyword[]; | |
| 53 | + message: string; | |
| 54 | + thumbnailFileId: string; | |
| 55 | + cutFileIds: string[]; | |
| 56 | + isVisible: boolean; | |
| 57 | +}; | |
| 58 | + | |
| 59 | +export type QuizFormValues = GradeSelection & { | |
| 60 | + level: string; | |
| 61 | + keywords: ContentKeyword[]; | |
| 62 | + question: string; | |
| 63 | + answer: string; | |
| 64 | + answerMessageCode: string; | |
| 65 | + answerMessageText: string; | |
| 66 | + explanation: string; | |
| 67 | + isVisible: boolean; | |
| 68 | +}; | |
| 69 | + | |
| 70 | +export type ValidationResult<T> = | |
| 71 | + | { ok: true; values: T } | |
| 72 | + | { ok: false; errors: ContentFormErrors }; | |
| 73 | + | |
| 74 | +/** 빈 줄은 저장하지 않는다 — 폼이 늘 보여 주는 기본 2줄이 그대로 넘어오기 때문이다. */ | |
| 75 | +export function normalizeKeywords( | |
| 76 | + keywords: readonly ContentKeyword[] | |
| 77 | +): ContentKeyword[] { | |
| 78 | + return keywords | |
| 79 | + .map((item) => ({ | |
| 80 | + keyword: item.keyword.trim().slice(0, KEYWORD_MAX_LENGTH), | |
| 81 | + description: item.description | |
| 82 | + .trim() | |
| 83 | + .slice(0, KEYWORD_DESCRIPTION_MAX_LENGTH), | |
| 84 | + })) | |
| 85 | + .filter((item) => item.keyword !== '' || item.description !== ''); | |
| 86 | +} | |
| 87 | + | |
| 88 | +function validateGrades( | |
| 89 | + values: GradeSelection, | |
| 90 | + errors: ContentFormErrors | |
| 91 | +): void { | |
| 92 | + const level = findSchoolLevel(values.schoolLevel); | |
| 93 | + if (!level) { | |
| 94 | + errors.schoolLevel = '학교급을 선택해 주세요.'; | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + if (level.gradeCount === 0) { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + const allowed = gradeOptionsOf(values.schoolLevel); | |
| 101 | + if (values.grades.length === 0) { | |
| 102 | + errors.grades = '학년을 하나 이상 선택해 주세요.'; | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + if (values.grades.some((grade) => !allowed.includes(grade))) { | |
| 106 | + errors.grades = '선택할 수 없는 학년이 포함돼 있습니다.'; | |
| 107 | + } | |
| 108 | +} | |
| 109 | + | |
| 110 | +function validateKeywords( | |
| 111 | + keywords: readonly ContentKeyword[], | |
| 112 | + errors: ContentFormErrors | |
| 113 | +): void { | |
| 114 | + if (keywords.some((item) => item.keyword === '' && item.description !== '')) { | |
| 115 | + errors.keywords = '설명만 있는 줄이 있습니다. 키워드를 입력해 주세요.'; | |
| 116 | + } | |
| 117 | +} | |
| 118 | + | |
| 119 | +function requireText( | |
| 120 | + value: string, | |
| 121 | + maxLength: number, | |
| 122 | + field: string, | |
| 123 | + message: string, | |
| 124 | + errors: ContentFormErrors | |
| 125 | +): string { | |
| 126 | + const trimmed = value.trim(); | |
| 127 | + if (trimmed === '') { | |
| 128 | + errors[field] = message; | |
| 129 | + } | |
| 130 | + return trimmed.slice(0, maxLength); | |
| 131 | +} | |
| 132 | + | |
| 133 | +export function validateShortsForm( | |
| 134 | + values: ShortsFormValues | |
| 135 | +): ValidationResult<ShortsFormValues> { | |
| 136 | + const errors: ContentFormErrors = {}; | |
| 137 | + validateGrades(values, errors); | |
| 138 | + | |
| 139 | + const title = requireText( | |
| 140 | + values.title, | |
| 141 | + TITLE_MAX_LENGTH, | |
| 142 | + 'title', | |
| 143 | + '제목을 입력해 주세요.', | |
| 144 | + errors | |
| 145 | + ); | |
| 146 | + const message = requireText( | |
| 147 | + values.message, | |
| 148 | + MESSAGE_MAX_LENGTH, | |
| 149 | + 'message', | |
| 150 | + '전달 메시지(해설)를 입력해 주세요.', | |
| 151 | + errors | |
| 152 | + ); | |
| 153 | + const keywords = normalizeKeywords(values.keywords); | |
| 154 | + validateKeywords(keywords, errors); | |
| 155 | + | |
| 156 | + if (Object.keys(errors).length > 0) { | |
| 157 | + return { ok: false, errors }; | |
| 158 | + } | |
| 159 | + return { | |
| 160 | + ok: true, | |
| 161 | + values: { | |
| 162 | + ...values, | |
| 163 | + title, | |
| 164 | + subject: values.subject.trim().slice(0, SUBJECT_MAX_LENGTH), | |
| 165 | + message, | |
| 166 | + keywords, | |
| 167 | + }, | |
| 168 | + }; | |
| 169 | +} | |
| 170 | + | |
| 171 | +export function validateComicForm( | |
| 172 | + values: ComicFormValues | |
| 173 | +): ValidationResult<ComicFormValues> { | |
| 174 | + const errors: ContentFormErrors = {}; | |
| 175 | + validateGrades(values, errors); | |
| 176 | + | |
| 177 | + const name = requireText( | |
| 178 | + values.name, | |
| 179 | + TITLE_MAX_LENGTH, | |
| 180 | + 'name', | |
| 181 | + '콘텐츠명을 입력해 주세요.', | |
| 182 | + errors | |
| 183 | + ); | |
| 184 | + const message = requireText( | |
| 185 | + values.message, | |
| 186 | + MESSAGE_MAX_LENGTH, | |
| 187 | + 'message', | |
| 188 | + '전달 메시지(해설)를 입력해 주세요.', | |
| 189 | + errors | |
| 190 | + ); | |
| 191 | + const keywords = normalizeKeywords(values.keywords); | |
| 192 | + validateKeywords(keywords, errors); | |
| 193 | + | |
| 194 | + const cutFileIds = values.cutFileIds.filter(Boolean); | |
| 195 | + if (cutFileIds.length < MIN_COMIC_CUTS) { | |
| 196 | + errors.cutFileIds = `만화 이미지를 ${MIN_COMIC_CUTS}컷 이상 등록해 주세요.`; | |
| 197 | + } | |
| 198 | + | |
| 199 | + if (Object.keys(errors).length > 0) { | |
| 200 | + return { ok: false, errors }; | |
| 201 | + } | |
| 202 | + return { | |
| 203 | + ok: true, | |
| 204 | + values: { | |
| 205 | + ...values, | |
| 206 | + name, | |
| 207 | + subject: values.subject.trim().slice(0, SUBJECT_MAX_LENGTH), | |
| 208 | + message, | |
| 209 | + keywords, | |
| 210 | + cutFileIds, | |
| 211 | + }, | |
| 212 | + }; | |
| 213 | +} | |
| 214 | + | |
| 215 | +export function validateQuizForm( | |
| 216 | + values: QuizFormValues | |
| 217 | +): ValidationResult<QuizFormValues> { | |
| 218 | + const errors: ContentFormErrors = {}; | |
| 219 | + validateGrades(values, errors); | |
| 220 | + | |
| 221 | + if (!QUIZ_LEVEL_OPTIONS.some((option) => option.value === values.level)) { | |
| 222 | + errors.level = '난이도를 선택해 주세요.'; | |
| 223 | + } | |
| 224 | + if (!(QUIZ_ANSWER_OPTIONS as readonly string[]).includes(values.answer)) { | |
| 225 | + errors.answer = '정답을 선택해 주세요.'; | |
| 226 | + } | |
| 227 | + if ( | |
| 228 | + values.answerMessageCode !== '' && | |
| 229 | + !ANSWER_MESSAGE_OPTIONS.some( | |
| 230 | + (option) => option.value === values.answerMessageCode | |
| 231 | + ) | |
| 232 | + ) { | |
| 233 | + errors.answerMessageCode = '노출 문구를 다시 선택해 주세요.'; | |
| 234 | + } | |
| 235 | + | |
| 236 | + const question = requireText( | |
| 237 | + values.question, | |
| 238 | + QUESTION_MAX_LENGTH, | |
| 239 | + 'question', | |
| 240 | + '질문을 입력해 주세요.', | |
| 241 | + errors | |
| 242 | + ); | |
| 243 | + const explanation = requireText( | |
| 244 | + values.explanation, | |
| 245 | + MESSAGE_MAX_LENGTH, | |
| 246 | + 'explanation', | |
| 247 | + '해설을 입력해 주세요.', | |
| 248 | + errors | |
| 249 | + ); | |
| 250 | + | |
| 251 | + let answerMessageText = values.answerMessageText | |
| 252 | + .trim() | |
| 253 | + .slice(0, ANSWER_MESSAGE_MAX_LENGTH); | |
| 254 | + if (values.answerMessageCode === ANSWER_MESSAGE_ETC) { | |
| 255 | + if (answerMessageText === '') { | |
| 256 | + errors.answerMessageText = '노출할 문구를 입력해 주세요.'; | |
| 257 | + } | |
| 258 | + } else { | |
| 259 | + answerMessageText = ''; | |
| 260 | + } | |
| 261 | + | |
| 262 | + const keywords = normalizeKeywords(values.keywords); | |
| 263 | + validateKeywords(keywords, errors); | |
| 264 | + | |
| 265 | + if (Object.keys(errors).length > 0) { | |
| 266 | + return { ok: false, errors }; | |
| 267 | + } | |
| 268 | + return { | |
| 269 | + ok: true, | |
| 270 | + values: { ...values, question, explanation, answerMessageText, keywords }, | |
| 271 | + }; | |
| 272 | +} |
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?