Skip to content

Commit 1db827e

Browse files
committed
Fixed an issue where options.errors were unsued
1 parent 09ed3db commit 1db827e

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@programmer_network/use-ajv-form",
3-
"version": "1.0.30",
3+
"version": "1.0.31",
44
"description": "Custom React Hook that integrates with Ajv JSON Schema Validator",
55
"main": "dist/use-ajv-form.es.js",
66
"author": "Aleksandar Grbic",

src/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ const useAJVForm = <T extends Record<string, any>>(
202202
[state],
203203
);
204204

205+
useEffect(() => {
206+
if (!options?.errors || !options.errors.length) {
207+
return;
208+
}
209+
210+
setState((prevState) => {
211+
const errors = getErrors(options.errors as ErrorObject[]);
212+
213+
return Object.keys(errors).reduce(
214+
(updatedState, key) => ({
215+
...updatedState,
216+
[key]: {
217+
...prevState[key],
218+
error: errors[key],
219+
},
220+
}),
221+
{ ...prevState } as IState<T>,
222+
);
223+
});
224+
}, [options?.errors]);
225+
205226
useEffect(() => {
206227
if (
207228
!debouncedField ||

src/useAjvForm.test.tsx

+62
Original file line numberDiff line numberDiff line change
@@ -911,3 +911,65 @@ describe('useAJVForm should properly set errors programmatically using setErrors
911911
expect(result.eventUrl.isRequired).toEqual(true);
912912
});
913913
});
914+
915+
describe('useAJVForm with error handling', () => {
916+
it('should set initial errors from options', () => {
917+
const initialData = { title: '' };
918+
const schema: JSONSchemaType<{ title: string }> = {
919+
type: 'object',
920+
required: ['title'],
921+
properties: {
922+
title: { type: 'string' },
923+
},
924+
};
925+
926+
const errors = [
927+
{
928+
instancePath: '/title',
929+
keyword: 'required',
930+
params: { missingProperty: 'title' },
931+
schemaPath: '#/required',
932+
},
933+
];
934+
935+
const { result } = renderHook(() => useAJVForm(initialData, schema, { errors }));
936+
937+
expect(result.current.state.title.error).toBe('title is required.');
938+
expect(result.current.isValid).toBe(false);
939+
});
940+
941+
it('should update errors when options.errors changes', () => {
942+
const initialData = { title: '' };
943+
const schema: JSONSchemaType<{ title: string }> = {
944+
type: 'object',
945+
required: ['title'],
946+
properties: {
947+
title: { type: 'string' },
948+
},
949+
};
950+
951+
const { result, rerender } = renderHook(
952+
(props) => useAJVForm(initialData, schema, props),
953+
{ initialProps: {} },
954+
);
955+
956+
expect(result.current.state.title.error).toBe('');
957+
958+
rerender({
959+
errors: [
960+
{
961+
instancePath: '/title',
962+
keyword: 'minLength',
963+
params: { limit: 3 },
964+
schemaPath: '#/properties/title/minLength',
965+
},
966+
],
967+
});
968+
969+
expect(result.current.state.title.error).toBe(
970+
'Should be at least 3 characters long.',
971+
);
972+
973+
expect(result.current.isValid).toBe(false);
974+
});
975+
});

0 commit comments

Comments
 (0)