Skip to content

Commit aa1b80d

Browse files
committed
Refactor form validation logic and rename utility functions for clarity
1 parent c4fc270 commit aa1b80d

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/index.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const useAJVForm = <T extends Record<string, any>>(
3636
[options?.debug],
3737
);
3838

39-
// Precompute field dependencies
4039
const fieldDependencies = useMemo(() => {
4140
const dependencies: Record<string, string[]> = {};
4241
if (schema.allOf) {
@@ -92,6 +91,7 @@ const useAJVForm = <T extends Record<string, any>>(
9291
: getErrors(errors, options?.userDefinedMessages, logger, schema);
9392

9493
const error = fieldErrors[fieldName as string] || '';
94+
9595
setState((prevState) => ({
9696
...prevState,
9797
[fieldName]: {
@@ -121,8 +121,8 @@ const useAJVForm = <T extends Record<string, any>>(
121121
}, {} as T);
122122

123123
logger.log('Validating entire form:', { data });
124-
const isValid = AJVValidate(data);
125124

125+
const isValid = AJVValidate(data);
126126
if (!isValid && AJVValidate.errors) {
127127
logger.error('Form validation failed with errors:', AJVValidate.errors);
128128

@@ -131,6 +131,7 @@ const useAJVForm = <T extends Record<string, any>>(
131131
options?.userDefinedMessages,
132132
logger,
133133
);
134+
134135
setState((prevState) =>
135136
Object.keys(prevState).reduce((updatedState, fieldName) => {
136137
return {
@@ -146,7 +147,6 @@ const useAJVForm = <T extends Record<string, any>>(
146147
return { isValid: false, data: null };
147148
}
148149

149-
// Clear errors if valid
150150
setState((prevState) =>
151151
Object.keys(prevState).reduce((updatedState, fieldName) => {
152152
return {
@@ -163,24 +163,14 @@ const useAJVForm = <T extends Record<string, any>>(
163163
return { isValid: true, data };
164164
};
165165

166-
const isDirty = useMemo(() => {
167-
return Object.keys(state).some(
168-
(key) => state[key].value !== initialStateRef.current[key].value,
169-
);
170-
}, [state]);
171-
172-
const isValid = useMemo(
173-
() => Object.keys(state).every((key) => !state[key].error),
174-
[state],
175-
);
176-
177166
const setErrors = (errors: ErrorObject[]) => {
178167
const newErrors = getErrors(
179168
errors,
180169
options?.userDefinedMessages,
181170
logger,
182171
schema,
183172
);
173+
184174
setState((prevState) =>
185175
Object.keys(newErrors).reduce((updatedState, fieldName) => {
186176
return {
@@ -194,6 +184,17 @@ const useAJVForm = <T extends Record<string, any>>(
194184
);
195185
};
196186

187+
const isDirty = useMemo(() => {
188+
return Object.keys(state).some(
189+
(key) => state[key].value !== initialStateRef.current[key].value,
190+
);
191+
}, [state]);
192+
193+
const isValid = useMemo(
194+
() => Object.keys(state).every((key) => !state[key].error),
195+
[state],
196+
);
197+
197198
useEffect(() => {
198199
if (
199200
!debouncedField ||

src/utils/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export const addUserDefinedKeywords = (
187187
* @param {FormField<T>[keyof T]} value - The new value for the field.
188188
* @returns {IState<T>} - The updated form state.
189189
*/
190-
const updateFieldValue = <T extends Record<string, any>>(
190+
const setFieldValue = <T extends Record<string, any>>(
191191
state: IState<T>,
192192
fieldName: keyof T,
193193
value: any,
@@ -211,7 +211,7 @@ const updateFieldValue = <T extends Record<string, any>>(
211211
* @param {SchemaObject | JSONSchemaType<T>} schema - The schema defining the dependencies and validation rules.
212212
* @returns {boolean} - Whether the dependent field should be required.
213213
*/
214-
const evaluateFieldDependency = <T>(
214+
const getIsRequired = <T>(
215215
fieldName: keyof T,
216216
parentFieldName: keyof T,
217217
parentValue: any,
@@ -238,7 +238,7 @@ const evaluateFieldDependency = <T>(
238238
* @param {SchemaObject | JSONSchemaType<T>} schema - The schema defining the dependencies and validation rules.
239239
* @returns {IState<T>} - The updated form state.
240240
*/
241-
const updateDynamicRequiredFields = <T>(
241+
const getIsRequiredDynamically = <T>(
242242
state: IState<T>,
243243
parentFieldName: keyof T,
244244
parentValue: any,
@@ -251,7 +251,7 @@ const updateDynamicRequiredFields = <T>(
251251
(updatedState, childFieldName) => {
252252
const fieldName = childFieldName as keyof T;
253253

254-
const isRequired = evaluateFieldDependency(
254+
const isRequired = getIsRequired(
255255
fieldName,
256256
parentFieldName,
257257
parentValue,
@@ -290,13 +290,9 @@ export const getFormState = <T extends Record<string, any>>(
290290
const fieldName = key as keyof T;
291291
const value = getValue(form[fieldName]);
292292

293-
const stateWithUpdatedFieldValue = updateFieldValue(
294-
updatedState,
295-
fieldName,
296-
value,
297-
);
293+
const stateWithUpdatedFieldValue = setFieldValue(updatedState, fieldName, value);
298294

299-
return updateDynamicRequiredFields(
295+
return getIsRequiredDynamically(
300296
stateWithUpdatedFieldValue,
301297
fieldName,
302298
value,

0 commit comments

Comments
 (0)