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( 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', () => {