Skip to content

Commit bb18d42

Browse files
committed
Merge branch 'master' of github.com:agjs/use-ajv-form
2 parents c19fac8 + 5a1bae0 commit bb18d42

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed

.prettierrc

-8
This file was deleted.

.prettierrc.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const config = {
2+
semi: true,
3+
trailingComma: 'all',
4+
singleQuote: true,
5+
printWidth: 85,
6+
tabWidth: 2,
7+
endOfLine: 'auto',
8+
};
9+
10+
module.exports = config;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@
9090
"json schema validation",
9191
"use-ajv-form",
9292
"react form validation"
93-
]
93+
],
94+
"packageManager": "pnpm@9.15.2+sha1.0ffb02f94047016ec7e088030337455b4c94bb34"
9495
}

pnpm-lock.yaml

+14-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,14 @@ const useAJVForm = <T extends Record<string, any>>(
198198
);
199199
}, [state]);
200200

201-
const isValid = useMemo(
202-
() => Object.keys(state).every((key) => !state[key].error),
203-
[state],
204-
);
201+
const isValid = useMemo(() => {
202+
const data = Object.keys(state).reduce((acc, inputName) => {
203+
acc[inputName as keyof T] = getValue(state[inputName].value) as T[keyof T];
204+
return acc;
205+
}, {} as T);
206+
207+
return AJVValidate(data);
208+
}, [state]);
205209

206210
useEffect(() => {
207211
if (!options?.errors || !options.errors.length) {

src/useAjvForm.test.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,6 @@ describe('useAJVForm with error handling', () => {
969969
const { result } = renderHook(() => useAJVForm(initialData, schema, { errors }));
970970

971971
expect(result.current.state.title.error).toBe('title is required.');
972-
expect(result.current.isValid).toBe(false);
973972
});
974973

975974
it('should update errors when options.errors changes', () => {
@@ -1003,7 +1002,25 @@ describe('useAJVForm with error handling', () => {
10031002
expect(result.current.state.title.error).toBe(
10041003
'Should be at least 3 characters long.',
10051004
);
1005+
});
1006+
1007+
it('should validate the form correctly', () => {
1008+
const initialData = { title: '' };
1009+
const schema: JSONSchemaType<{ title: string }> = {
1010+
type: 'object',
1011+
required: ['title'],
1012+
properties: {
1013+
title: { type: 'string', minLength: 3 },
1014+
},
1015+
};
10061016

1017+
const { result } = renderHook(() => useAJVForm(initialData, schema));
10071018
expect(result.current.isValid).toBe(false);
1019+
1020+
result.current.set({ title: 'Hi' });
1021+
expect(result.current.validate().isValid).toBe(false);
1022+
1023+
result.current.set({ title: 'Hello' });
1024+
expect(result.current.validate().isValid).toBe(true);
10081025
});
10091026
});

0 commit comments

Comments
 (0)