Skip to content

Commit

Permalink
fix: Joi pass context from useForm (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisre authored Feb 2, 2021
1 parent ecee1e9 commit 132a90e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/joi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ describe('joiResolver', () => {
it('should return errors', async () => {
expect(await joiResolver(schema)({})).toMatchSnapshot();
});

it('should validate with context', async () => {
const schemaSpy = jest.spyOn(schema, 'validateAsync');
const context = { value: 'context' };

const data = { username: 'abc', birthYear: 1994 };
expect(await joiResolver(schema)(data, context)).toEqual({
values: data,
errors: {},
});

expect(schemaSpy).toHaveBeenCalledWith(data, {
abortEarly: false,
context,
});
});
});
3 changes: 2 additions & 1 deletion src/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ export const joiResolver = <TFieldValues extends FieldValues>(
},
): Resolver<TFieldValues> => async (
values,
_,
context,
validateAllFieldCriteria = false,
) => {
try {
return {
values: await schema.validateAsync(values, {
...options,
context,
}),
errors: {},
};
Expand Down

0 comments on commit 132a90e

Please sign in to comment.