From 3563aa08b9f6c1bd86f0d2b49e4b3c090c331251 Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:48:00 +0200 Subject: [PATCH 1/2] Remove isDirty check from validation --- src/index.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index f1381b2..b72fca3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -136,7 +136,7 @@ const useAJVForm = >( return { isValid: false, data: null }; } - if (!isFormValid(state, initialStateRef.current)) { + if (!isFormValid(state)) { return { isValid: false, data: null }; } @@ -155,21 +155,16 @@ const useAJVForm = >( ); }; - const isFormValid = ( - currentState: IState, - initialState: IState, - ): boolean => { + const isFormValid = (currentState: IState): boolean => { const hasErrors = Object.keys(currentState).some( (key) => currentState[key].error !== '', ); - const formIsDirty = isFormDirty(currentState, initialState); - - return !hasErrors && formIsDirty; + return !hasErrors; }; const isValid = useMemo(() => { - return isFormValid(state, initialStateRef.current); + return isFormValid(state); }, [state]); const isDirty = useMemo( From d12a1608af110ec3737893414aa8ea61033772d8 Mon Sep 17 00:00:00 2001 From: Johan Book <13253042+johanbook@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:49:53 +0200 Subject: [PATCH 2/2] Fix failing test --- src/useAjvForm.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/useAjvForm.test.tsx b/src/useAjvForm.test.tsx index 06e1a0d..c6bdc74 100644 --- a/src/useAjvForm.test.tsx +++ b/src/useAjvForm.test.tsx @@ -126,7 +126,7 @@ describe('useAJVForm', () => { expect(validation.isValid).toBe(true); }); - it('isValid should be false when the initial state is set or when reset is called', () => { + it('isValid should be true when the initial state is set or when reset is called', () => { const initialData = { title: 'Foo' }; const schema: JSONSchemaType<{ title: string }> = { type: 'object', @@ -138,7 +138,7 @@ describe('useAJVForm', () => { const { result } = renderHook(() => useAJVForm(initialData, schema)); - expect(result.current.validate().isValid).toBe(false); + expect(result.current.validate().isValid).toBe(true); result.current.set({ title: 'Bar' }); @@ -146,7 +146,7 @@ describe('useAJVForm', () => { result.current.reset(); - expect(result.current.validate().isValid).toBe(false); + expect(result.current.validate().isValid).toBe(true); }); it('validates minLength and maxLength for title', () => {