Skip to content

Commit

Permalink
fix(utils): omit computedDefault of empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rani committed Oct 8, 2022
1 parent 6666b18 commit 38044a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
8 changes: 7 additions & 1 deletion packages/utils/src/schema/getDefaultFormState.ts
Expand Up @@ -180,7 +180,13 @@ export function computeDefaults<T = any>(
get(formData, [key]),
includeUndefinedValues
);
if (includeUndefinedValues || computedDefault !== undefined) {
if (typeof computedDefault === "object") {
// Store computedDefault if it's a non-empty object (e.g. not {})
if (includeUndefinedValues && !isEmpty(computedDefault)) {
acc[key] = computedDefault;
}
} else if (computedDefault !== undefined) {
// Store computedDefault if it's a defined primitive (e.g. true)
acc[key] = computedDefault;
}
return acc;
Expand Down
22 changes: 11 additions & 11 deletions packages/validator-ajv6/src/validator.ts
Expand Up @@ -234,19 +234,9 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
customValidate?: CustomValidator<T>,
transformErrors?: ErrorTransformer
): ValidationData<T> {
// Include form data with undefined values, which is required for validation.
const rootSchema = schema;
const newFormData = getDefaultFormState<T>(
this,
schema,
formData,
rootSchema,
true
) as T;

let validationError: Error | null = null;
try {
this.ajv.validate(schema, newFormData);
this.ajv.validate(schema, formData);
} catch (err) {
validationError = err as Error;
}
Expand Down Expand Up @@ -286,6 +276,16 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
return { errors, errorSchema };
}

// Include form data with undefined values, which is required for custom validation.
const rootSchema = schema;
const newFormData = getDefaultFormState<T>(
this,
schema,
formData,
rootSchema,
true
) as T;

const errorHandler = customValidate(
newFormData,
this.createErrorHandler(newFormData)
Expand Down

0 comments on commit 38044a6

Please sign in to comment.