Skip to content

Commit 0a68ce1

Browse files
authored
Merge pull request #7 from Programmer-Network/remove-dirty-check-from-validation
Remove isDirty check from validation
2 parents d55caf9 + d12a160 commit 0a68ce1

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const useAJVForm = <T extends Record<string, any>>(
136136
return { isValid: false, data: null };
137137
}
138138

139-
if (!isFormValid(state, initialStateRef.current)) {
139+
if (!isFormValid(state)) {
140140
return { isValid: false, data: null };
141141
}
142142

@@ -155,21 +155,16 @@ const useAJVForm = <T extends Record<string, any>>(
155155
);
156156
};
157157

158-
const isFormValid = (
159-
currentState: IState<T>,
160-
initialState: IState<T>,
161-
): boolean => {
158+
const isFormValid = (currentState: IState<T>): boolean => {
162159
const hasErrors = Object.keys(currentState).some(
163160
(key) => currentState[key].error !== '',
164161
);
165162

166-
const formIsDirty = isFormDirty(currentState, initialState);
167-
168-
return !hasErrors && formIsDirty;
163+
return !hasErrors;
169164
};
170165

171166
const isValid = useMemo(() => {
172-
return isFormValid(state, initialStateRef.current);
167+
return isFormValid(state);
173168
}, [state]);
174169

175170
const isDirty = useMemo(

src/useAjvForm.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('useAJVForm', () => {
126126
expect(validation.isValid).toBe(true);
127127
});
128128

129-
it('isValid should be false when the initial state is set or when reset is called', () => {
129+
it('isValid should be true when the initial state is set or when reset is called', () => {
130130
const initialData = { title: 'Foo' };
131131
const schema: JSONSchemaType<{ title: string }> = {
132132
type: 'object',
@@ -138,15 +138,15 @@ describe('useAJVForm', () => {
138138

139139
const { result } = renderHook(() => useAJVForm(initialData, schema));
140140

141-
expect(result.current.validate().isValid).toBe(false);
141+
expect(result.current.validate().isValid).toBe(true);
142142

143143
result.current.set({ title: 'Bar' });
144144

145145
expect(result.current.validate().isValid).toBe(true);
146146

147147
result.current.reset();
148148

149-
expect(result.current.validate().isValid).toBe(false);
149+
expect(result.current.validate().isValid).toBe(true);
150150
});
151151

152152
it('validates minLength and maxLength for title', () => {

0 commit comments

Comments
 (0)