diff --git a/src/presentation/forms/form-contracts.ts b/src/presentation/forms/form-contracts.ts index c432ef1b..d424b6e3 100644 --- a/src/presentation/forms/form-contracts.ts +++ b/src/presentation/forms/form-contracts.ts @@ -37,6 +37,30 @@ const VALIDATION_COPY = Object.freeze({ invalid_type: "form.validation.invalidType", invalid_format: "form.validation.invalidFormat", invalid_value: "form.validation.invalidValue", + // 1101/14.4. The paired backend's field codes, every one it can send + // (clean-architecture-backend-template 5786126c, + // ClientSafeValidationDetails.java RULES and INVALID). A code it adds later + // shows form.validation.unknown until it gets a row here. + NOT_NULL: "form.validation.required", + NOT_BLANK: "form.validation.required", + NOT_EMPTY: "form.validation.required", + SIZE: "form.validation.size", + MIN: "form.validation.min", + MAX: "form.validation.max", + POSITIVE: "form.validation.positive", + POSITIVE_OR_ZERO: "form.validation.positiveOrZero", + NEGATIVE: "form.validation.negative", + NEGATIVE_OR_ZERO: "form.validation.negativeOrZero", + PATTERN: "form.validation.invalidFormat", + EMAIL: "form.validation.email", + PAST: "form.validation.past", + PAST_OR_PRESENT: "form.validation.pastOrPresent", + FUTURE: "form.validation.future", + FUTURE_OR_PRESENT: "form.validation.futureOrPresent", + ASSERT_TRUE: "form.validation.invalidValue", + ASSERT_FALSE: "form.validation.invalidValue", + TYPE_MISMATCH: "form.validation.invalidType", + INVALID: "form.validation.invalid", }); type MessageResolver = (key: ParameterlessMessageKey) => string; diff --git a/src/presentation/i18n/catalog.ts b/src/presentation/i18n/catalog.ts index 03a0f202..7f31d496 100644 --- a/src/presentation/i18n/catalog.ts +++ b/src/presentation/i18n/catalog.ts @@ -140,6 +140,19 @@ const PLATFORM_KO_MESSAGES = { "form.validation.invalidType": "입력 형식을 확인해 주세요.", "form.validation.invalidFormat": "입력 형식을 확인해 주세요.", "form.validation.invalidValue": "허용된 값을 선택해 주세요.", + "form.validation.size": "길이나 개수가 허용 범위를 벗어났습니다.", + "form.validation.min": "허용된 최솟값보다 작습니다.", + "form.validation.max": "허용된 최댓값보다 큽니다.", + "form.validation.positive": "0보다 큰 값을 입력해 주세요.", + "form.validation.positiveOrZero": "0 이상의 값을 입력해 주세요.", + "form.validation.negative": "0보다 작은 값을 입력해 주세요.", + "form.validation.negativeOrZero": "0 이하의 값을 입력해 주세요.", + "form.validation.email": "이메일 주소 형식을 확인해 주세요.", + "form.validation.past": "지난 시각을 입력해 주세요.", + "form.validation.pastOrPresent": "앞으로의 시각은 입력할 수 없습니다.", + "form.validation.future": "앞으로의 시각을 입력해 주세요.", + "form.validation.futureOrPresent": "지난 시각은 입력할 수 없습니다.", + "form.validation.invalid": "올바른 값을 입력해 주세요.", "form.validation.unknown": "입력값을 확인해 주세요.", "form.validation.retry": "입력값을 다시 확인해 주세요.", "form.validation.unknownField": @@ -339,6 +352,19 @@ const PLATFORM_EN_MESSAGES = { "form.validation.invalidType": "Check the input type.", "form.validation.invalidFormat": "Check the input format.", "form.validation.invalidValue": "Choose an allowed value.", + "form.validation.size": "The length or count is outside the allowed range.", + "form.validation.min": "This value is below the allowed minimum.", + "form.validation.max": "This value is above the allowed maximum.", + "form.validation.positive": "Enter a value greater than 0.", + "form.validation.positiveOrZero": "Enter 0 or a greater value.", + "form.validation.negative": "Enter a value less than 0.", + "form.validation.negativeOrZero": "Enter 0 or a smaller value.", + "form.validation.email": "Check the email address format.", + "form.validation.past": "Enter a time in the past.", + "form.validation.pastOrPresent": "A future time is not allowed.", + "form.validation.future": "Enter a time in the future.", + "form.validation.futureOrPresent": "A past time is not allowed.", + "form.validation.invalid": "Enter a valid value.", "form.validation.unknown": "Check the entered value.", "form.validation.retry": "Review the entered values.", "form.validation.unknownField": diff --git a/tests/component/form-foundation.test.tsx b/tests/component/form-foundation.test.tsx index 26c0bca2..df0233e1 100644 --- a/tests/component/form-foundation.test.tsx +++ b/tests/component/form-foundation.test.tsx @@ -22,7 +22,12 @@ import { FormField, useAppForm, useDirtyNavigationGuard, + validationMessage, } from "../../src/presentation/forms/index.ts"; +import { + formatMessage, + type ParameterlessMessageKey, +} from "../../src/presentation/i18n/index.ts"; type Values = Readonly>; @@ -206,6 +211,8 @@ describe("local form facade", () => { expect(screen.getByRole("textbox", { name: /Note/ })).not.toHaveAttribute( "aria-invalid", ); + // NOT_BLANK has its own copy, not the "check the value" default. + expect(screen.getByRole("alert")).toHaveTextContent("필수 입력값입니다."); // A class-level error names no form field. expect(screen.getByRole("alert")).toHaveTextContent( "서버가 확인하지 못한 입력 항목", @@ -448,6 +455,57 @@ describe("local form facade", () => { }); }); +// 1101/14.4. Every code the paired backend can put in a field error's +// details[].code: clean-architecture-backend-template 5786126c, +// ClientSafeValidationDetails.java RULES (18-42) and its INVALID fallback. +// http-contract.md §4 names the shape, not the list. A code missing from the +// form copy table shows the "check the value" default instead. +const BACKEND_FIELD_CODES = [ + "NOT_NULL", + "NOT_BLANK", + "NOT_EMPTY", + "SIZE", + "MIN", + "MAX", + "POSITIVE", + "POSITIVE_OR_ZERO", + "NEGATIVE", + "NEGATIVE_OR_ZERO", + "PATTERN", + "EMAIL", + "PAST", + "PAST_OR_PRESENT", + "FUTURE", + "FUTURE_OR_PRESENT", + "ASSERT_TRUE", + "ASSERT_FALSE", + "TYPE_MISMATCH", + "INVALID", +] as const; + +describe("paired backend field codes", () => { + it.each(["ko-KR", "en-US"] as const)( + "gives every backend field code its own %s copy", + (locale) => { + const message = (key: ParameterlessMessageKey) => + formatMessage(locale, key); + const fallback = message("form.validation.unknown"); + + expect( + BACKEND_FIELD_CODES.filter( + (code) => validationMessage(code, message) === fallback, + ), + ).toEqual([]); + }, + ); + + it("keeps the default copy for a code it does not know", () => { + expect(validationMessage("SOME_FUTURE_CODE")).toBe( + formatMessage("ko-KR", "form.validation.unknown"), + ); + }); +}); + describe("dirty navigation guard", () => { it("blocks navigation, restores focus on stay and proceeds explicitly", async () => { const user = userEvent.setup();