diff --git a/src/lib/mobx-form/form-has-error.test.ts b/src/lib/mobx-form/form-has-error.test.ts index 431a2978..61c58427 100644 --- a/src/lib/mobx-form/form-has-error.test.ts +++ b/src/lib/mobx-form/form-has-error.test.ts @@ -8,6 +8,8 @@ import { } from "./form-has-error.ts"; import { validators } from "./validator.ts"; +const isRequiredMessage = 'is required'; + test("isFormTouchedAndHasError", () => { const f1 = { a: new TextField("a"), @@ -24,8 +26,8 @@ test("isFormTouchedAndHasError", () => { ).toBeTruthy(); const f2 = { - a: new TextField("a", validators.required()), - b: [new TextField("b", validators.required()), new TextField("d")], + a: new TextField("a", validators.required(isRequiredMessage)), + b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")], }; f2.a.touch(); @@ -49,8 +51,8 @@ test("isFormTouchedAndHasError", () => { test("is form dirty", () => { const f = { - a: new TextField("a", validators.required()), - b: [new TextField("b", validators.required()), new TextField("d")], + a: new TextField("a", validators.required(isRequiredMessage)), + b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")], }; expect(isFormTouched(f)).toBeFalsy(); @@ -61,7 +63,7 @@ test("is form dirty", () => { test("is form invalid by default", () => { const f = { - a: new TextField("", validators.required()), + a: new TextField("", validators.required(isRequiredMessage)), }; expect(isFormValid(f)).toBeFalsy(); @@ -83,8 +85,8 @@ test("is boolean form dirty", () => { test("is form empty", () => { const f = { - a: new TextField("a", validators.required()), - b: [new TextField("b", validators.required()), new TextField("d")], + a: new TextField("a", validators.required(isRequiredMessage)), + b: [new TextField("b", validators.required(isRequiredMessage)), new TextField("d")], }; expect(isFormEmpty(f)).toBeFalsy(); @@ -99,10 +101,10 @@ test("is form empty", () => { test("very nested form - only fields", () => { const f = { - a: new TextField("a", validators.required()), + a: new TextField("a", validators.required(isRequiredMessage)), b: { c: { - d: new TextField("d", validators.required()), + d: new TextField("d", validators.required(isRequiredMessage)), }, }, }; @@ -118,11 +120,11 @@ test("very nested form - only fields", () => { test("very nested form - any fields", () => { const f = { - a: new TextField("a", validators.required()), + a: new TextField("a", validators.required(isRequiredMessage)), num: 12, b: { c: { - d: new TextField("d", validators.required()), + d: new TextField("d", validators.required(isRequiredMessage)), k: null, }, }, diff --git a/src/lib/mobx-form/validator.ts b/src/lib/mobx-form/validator.ts index 3eec800f..55ac1e50 100644 --- a/src/lib/mobx-form/validator.ts +++ b/src/lib/mobx-form/validator.ts @@ -1,13 +1,11 @@ // @ts-nocheck -import { t } from "../../translations/t.ts"; // https://codesandbox.io/s/github/final-form/react-final-form/tree/master/examples/field-level-validation?file=/index.js export const validators = { required: - (errorMessage = t("validation_required")) => + (errorMessage) => (value) => value ? undefined : errorMessage, - number: (value) => (isNaN(value) ? t("validation_number") : undefined), all: (...validators) => (value) => diff --git a/src/store/deck-form-store.test.ts b/src/store/deck-form-store.test.ts index 68ee2abe..2e3967aa 100644 --- a/src/store/deck-form-store.test.ts +++ b/src/store/deck-form-store.test.ts @@ -118,6 +118,13 @@ vi.mock("../lib/telegram/show-alert.ts", () => { }; }); +vi.mock("../translations/t.ts", () => { + return { + t: (val: string) => val, + }; +}); + + vi.mock("../api/api.ts", () => { return { upsertDeckRequest: mocks.upsertDeckRequest, diff --git a/src/store/deck-form-store.ts b/src/store/deck-form-store.ts index bb8f2dda..90611edb 100644 --- a/src/store/deck-form-store.ts +++ b/src/store/deck-form-store.ts @@ -41,7 +41,7 @@ export const createDeckTitleField = (value: string) => { }; export const createCardSideField = (value: string) => { - return new TextField(value, validators.required()); + return new TextField(value, validators.required(t('validation_required'))); }; const createUpdateForm = (