diff --git a/src/features/tech-log/adapters/mock/validate-working-copy.ts b/src/features/tech-log/adapters/mock/validate-working-copy.ts index a3d52dd..af9875b 100644 --- a/src/features/tech-log/adapters/mock/validate-working-copy.ts +++ b/src/features/tech-log/adapters/mock/validate-working-copy.ts @@ -35,6 +35,8 @@ export type ValidationDependencies = { const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; const length = (value: string) => [...value].length; +type RecordKind = components["schemas"]["RecordKind"]; + const object = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value); const escape = (value: string) => value.replaceAll("~", "~0").replaceAll("/", "~1"); @@ -43,20 +45,30 @@ function runtimeShapeErrors(value: unknown): FieldError[] { const errors: FieldError[] = []; const kind = value.kind; const base = ["kind", "title", "slug", "summary", "topicId", "projectId", "relations"]; - const branch = kind === "CASE" - ? ["problem", "conclusion", "environment", "reproduction", "lastVerifiedOn", "bodyMarkdown"] - : kind === "REFERENCE" - ? ["purpose", "rules", "applyWhen", "exceptions", "examples", "verifiedOn"] - : kind === "QUESTION" - ? ["questionStatus", "facts", "assumptions", "unknowns", "constraints", "options", "nextValidation", "resolution"] - : kind === "PROJECT_DECISION" - ? ["decisionStatus", "decidedOn", "statement", "rationale", "consequences"] - : []; - if (!(["CASE", "REFERENCE", "QUESTION", "PROJECT_DECISION"] as unknown[]).includes(kind)) errors.push({ path: "/kind", message: "Invalid record kind." }); - const allowed = new Set([...base, ...branch]); + /* + 보내도 되지만 없어도 되는 칸. 계약에서 `required` 가 아니므로 여기서 요구하면 이 칸을 모르는 + 호출자가 전부 거절당한다. + */ + const optional = ["variantIds"]; + /* + 유형별 칸. 삼항 사슬이던 동안 개념이 어디에도 없어서, 개념 작업본은 「알 수 없는 종류」로 + 거절되고 그 문서의 모든 칸이 「허용되지 않은 속성」이 됐다 — 손으로 나열한 목록에 새 종류를 + 빠뜨리는 일이 이 저장소에서 반복됐다. `Record<종류, …>` 로 두면 컴파일러가 빈 자리를 잡는다. + */ + const BRANCH_FIELDS: Record = { + CASE: ["problem", "conclusion", "environment", "reproduction", "lastVerifiedOn", "bodyMarkdown"], + CONCEPT: ["bodyMarkdown", "basisVersion"], + REFERENCE: ["purpose", "rules", "applyWhen", "exceptions", "examples", "verifiedOn"], + QUESTION: ["questionStatus", "facts", "assumptions", "unknowns", "constraints", "options", "nextValidation", "resolution"], + PROJECT_DECISION: ["decisionStatus", "decidedOn", "statement", "rationale", "consequences"], + }; + const known = Object.hasOwn(BRANCH_FIELDS, kind as string); + const branch = known ? BRANCH_FIELDS[kind as RecordKind] : []; + if (!known) errors.push({ path: "/kind", message: "Invalid record kind." }); + const allowed = new Set([...base, ...optional, ...branch]); for (const key of Object.keys(value)) if (!allowed.has(key)) errors.push({ path: `/${escape(key)}`, message: "Additional properties are not allowed." }); for (const key of [...base, ...branch]) if (!Object.hasOwn(value, key)) errors.push({ path: `/${key}`, message: "Field is required." }); - const stringFields = ["title", "slug", "summary", ...(kind === "CASE" ? ["problem", "conclusion", "environment", "reproduction", "bodyMarkdown"] : []), ...(kind === "REFERENCE" ? ["purpose"] : []), ...(kind === "QUESTION" ? ["nextValidation"] : []), ...(kind === "PROJECT_DECISION" ? ["statement", "rationale"] : [])]; + const stringFields = ["title", "slug", "summary", ...(kind === "CASE" ? ["problem", "conclusion", "environment", "reproduction", "bodyMarkdown"] : []), ...(kind === "CONCEPT" ? ["bodyMarkdown", "basisVersion"] : []), ...(kind === "REFERENCE" ? ["purpose"] : []), ...(kind === "QUESTION" ? ["nextValidation"] : []), ...(kind === "PROJECT_DECISION" ? ["statement", "rationale"] : [])]; for (const key of stringFields) if (Object.hasOwn(value, key) && typeof value[key] !== "string") errors.push({ path: `/${key}`, message: "Must be a string." }); for (const key of ["topicId", "projectId"]) if (Object.hasOwn(value, key) && value[key] !== null && typeof value[key] !== "string") errors.push({ path: `/${key}`, message: "Must be a UUID or null." }); for (const key of ["lastVerifiedOn", "verifiedOn", "decidedOn"]) if (Object.hasOwn(value, key) && value[key] !== null && typeof value[key] !== "string") errors.push({ path: `/${key}`, message: "Must be a date or null." });