Skip to content
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

fix(utils): omit computedDefault of empty objects #2849

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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