Skip to content

Commit 132a90e

Browse files
authored
fix: Joi pass context from useForm (#122)
1 parent ecee1e9 commit 132a90e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/joi.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@ describe('joiResolver', () => {
3030
it('should return errors', async () => {
3131
expect(await joiResolver(schema)({})).toMatchSnapshot();
3232
});
33+
34+
it('should validate with context', async () => {
35+
const schemaSpy = jest.spyOn(schema, 'validateAsync');
36+
const context = { value: 'context' };
37+
38+
const data = { username: 'abc', birthYear: 1994 };
39+
expect(await joiResolver(schema)(data, context)).toEqual({
40+
values: data,
41+
errors: {},
42+
});
43+
44+
expect(schemaSpy).toHaveBeenCalledWith(data, {
45+
abortEarly: false,
46+
context,
47+
});
48+
});
3349
});

src/joi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ export const joiResolver = <TFieldValues extends FieldValues>(
5454
},
5555
): Resolver<TFieldValues> => async (
5656
values,
57-
_,
57+
context,
5858
validateAllFieldCriteria = false,
5959
) => {
6060
try {
6161
return {
6262
values: await schema.validateAsync(values, {
6363
...options,
64+
context,
6465
}),
6566
errors: {},
6667
};

0 commit comments

Comments
 (0)