Skip to content

Remove isDirty check from validation #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const useAJVForm = <T extends Record<string, any>>(
return { isValid: false, data: null };
}

if (!isFormValid(state, initialStateRef.current)) {
if (!isFormValid(state)) {
return { isValid: false, data: null };
}

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

const isFormValid = (
currentState: IState<T>,
initialState: IState<T>,
): boolean => {
const isFormValid = (currentState: IState<T>): 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(
Expand Down
6 changes: 3 additions & 3 deletions src/useAjvForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -138,15 +138,15 @@ 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' });

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

result.current.reset();

expect(result.current.validate().isValid).toBe(false);
expect(result.current.validate().isValid).toBe(true);
});

it('validates minLength and maxLength for title', () => {
Expand Down
Loading