Skip to content

Commit

Permalink
fix(yup): lazy schema (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisre authored Aug 18, 2023
1 parent 4882173 commit c1d5433
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions yup/src/__tests__/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,29 @@ describe('yupResolver', () => {
expect(schemaSyncSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: validData });
});

it('shoud validate a lazy schema with success', async () => {
const lazySchema = yup.lazy(() => yup.object().shape({ firstName: yup.string().optional() }));

const schemaSpy = vi.spyOn(lazySchema, 'validate');
const schemaSyncSpy = vi.spyOn(lazySchema, 'validateSync');

const result = await yupResolver(lazySchema, undefined,)(
{ firstName: "resolver" },
undefined,
{
fields: {
firstName: {
ref: { name: 'firstName' },
name: 'firstName',
}
},
shouldUseNativeValidation,
},
);

expect(schemaSpy).toHaveBeenCalledTimes(1);
expect(schemaSyncSpy).not.toHaveBeenCalled();
expect(result).toEqual({ errors: {}, values: { firstName: "resolver" } });
});
});
4 changes: 2 additions & 2 deletions yup/src/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const parseErrorSchema = (
};

export function yupResolver<TFieldValues extends FieldValues>(
schema: Yup.ObjectSchema<TFieldValues>,
schema: Yup.ObjectSchema<TFieldValues> | ReturnType<typeof Yup.lazy>,
schemaOptions: Parameters<(typeof schema)['validate']>[1] = {},
resolverOptions: {
/**
Expand Down Expand Up @@ -90,7 +90,7 @@ export function yupResolver<TFieldValues extends FieldValues>(
parseErrorSchema(
e,
!options.shouldUseNativeValidation &&
options.criteriaMode === 'all',
options.criteriaMode === 'all',
),
options,
),
Expand Down

0 comments on commit c1d5433

Please sign in to comment.