From 20c3109eeb11643a919f0df479d85d38c82a7a0c Mon Sep 17 00:00:00 2001 From: Andriamanamihaga Zo Toavina Date: Tue, 6 Aug 2024 18:32:36 +0300 Subject: [PATCH] feat: add setErrorMap to formApi and fieldApi --- packages/form-core/src/FieldApi.ts | 13 +++++ packages/form-core/src/FormApi.ts | 12 +++++ packages/form-core/tests/FieldApi.spec.ts | 59 +++++++++++++++++++++++ packages/form-core/tests/FormApi.spec.ts | 39 +++++++++++++++ 4 files changed, 123 insertions(+) diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts index 915b997a0..337b38d8a 100644 --- a/packages/form-core/src/FieldApi.ts +++ b/packages/form-core/src/FieldApi.ts @@ -963,6 +963,19 @@ export class FieldApi< } this.validate('blur') } + + /** + * Updates the field's errorMap + */ + setErrorMap(errorMap: ValidationErrorMap) { + this.setMeta((prev) => ({ + ...prev, + errorMap: { + ...prev.errorMap, + ...errorMap, + }, + })) + } } function normalizeError(rawError?: ValidationError) { diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index dd1f608c2..017735b07 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -1095,6 +1095,18 @@ export class FormApi< this.validateField(`${field}[${index1}]` as DeepKeys, 'change') this.validateField(`${field}[${index2}]` as DeepKeys, 'change') } + /** + * Updates the form's errorMap + */ + setErrorMap(errorMap: ValidationErrorMap) { + this.store.setState((prev) => ({ + ...prev, + errorMap: { + ...prev.errorMap, + ...errorMap, + }, + })) + } } function normalizeError(rawError?: ValidationError) { diff --git a/packages/form-core/tests/FieldApi.spec.ts b/packages/form-core/tests/FieldApi.spec.ts index 1f86cd09c..56a415b19 100644 --- a/packages/form-core/tests/FieldApi.spec.ts +++ b/packages/form-core/tests/FieldApi.spec.ts @@ -1270,4 +1270,63 @@ describe('field api', () => { 'Passwords do not match', ]) }) + + it('should add a new value to the fieldApi errorMap', () => { + interface Form { + name: string + } + const form = new FormApi
() + const nameField = new FieldApi({ + form, + name: 'name', + }) + nameField.mount() + nameField.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh") + }) + it('should preserve other values in the fieldApi errorMap when adding other values', () => { + interface Form { + name: string + } + const form = new FormApi() + const nameField = new FieldApi({ + form, + name: 'name', + }) + nameField.mount() + nameField.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh") + nameField.setErrorMap({ + onBlur: 'name must begin with uppercase', + }) + expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh") + expect(nameField.getMeta().errorMap.onBlur).toEqual( + 'name must begin with uppercase', + ) + }) + it('should replace errorMap value if it exists in the fieldApi object', () => { + interface Form { + name: string + } + const form = new FormApi() + const nameField = new FieldApi({ + form, + name: 'name', + }) + nameField.mount() + nameField.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(nameField.getMeta().errorMap.onChange).toEqual("name can't be Josh") + nameField.setErrorMap({ + onChange: 'other validation error', + }) + expect(nameField.getMeta().errorMap.onChange).toEqual( + 'other validation error', + ) + }) }) diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 7e6a783fb..21b85690f 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -1527,4 +1527,43 @@ describe('form api', () => { { nameInfo: { first: 'firstName' } }, ]) }) + it('should add a new value to the formApi errorMap', () => { + interface Form { + name: string + } + const form = new FormApi() + form.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(form.state.errorMap.onChange).toEqual("name can't be Josh") + }) + it('should preserve other values in the formApi errorMap when adding other values', () => { + interface Form { + name: string + } + const form = new FormApi() + form.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(form.state.errorMap.onChange).toEqual("name can't be Josh") + form.setErrorMap({ + onBlur: 'name must begin with uppercase', + }) + expect(form.state.errorMap.onChange).toEqual("name can't be Josh") + expect(form.state.errorMap.onBlur).toEqual('name must begin with uppercase') + }) + it('should replace errorMap value if it exists in the FormApi object', () => { + interface Form { + name: string + } + const form = new FormApi() + form.setErrorMap({ + onChange: "name can't be Josh", + }) + expect(form.state.errorMap.onChange).toEqual("name can't be Josh") + form.setErrorMap({ + onChange: 'other validation error', + }) + expect(form.state.errorMap.onChange).toEqual('other validation error') + }) })