diff --git a/joi/src/__tests__/joi.ts b/joi/src/__tests__/joi.ts index 00bc7570..c5f808a3 100644 --- a/joi/src/__tests__/joi.ts +++ b/joi/src/__tests__/joi.ts @@ -139,4 +139,29 @@ describe('joiResolver', () => { expect(result).toMatchSnapshot(); }); + + it('should return values from joiResolver when validation pass and pass down the Joi context', async () => { + const data: Data = { + username: 'Doe', + password: 'Password123', + repeatPassword: 'Password123', + birthYear: 2000, + email: 'john@doe.com', + tags: ['tag1', 'tag2'], + enabled: true, + }; + const context = { value: 'context' }; + const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); + const validateSpy = jest.spyOn(schema, 'validate'); + + const result = await joiResolver(schema)(data, context, { fields: {} }); + + expect(validateSpy).not.toHaveBeenCalled(); + expect(validateAsyncSpy).toHaveBeenCalledTimes(1); + expect(validateAsyncSpy).toHaveBeenCalledWith(data, { + abortEarly: false, + context, + }); + expect(result).toEqual({ errors: {}, values: data }); + }); }); diff --git a/joi/src/joi.ts b/joi/src/joi.ts index 7629b1d5..67484730 100644 --- a/joi/src/joi.ts +++ b/joi/src/joi.ts @@ -50,13 +50,19 @@ export const joiResolver: Resolver = ( abortEarly: false, }, { mode } = { mode: 'async' }, -) => async (values, _, { criteriaMode }) => { +) => async (values, context, { criteriaMode }) => { try { let result; if (mode === 'async') { - result = await schema.validateAsync(values, schemaOptions); + result = await schema.validateAsync(values, { + ...schemaOptions, + context, + }); } else { - const { value, error } = schema.validate(values, schemaOptions); + const { value, error } = schema.validate(values, { + ...schemaOptions, + context, + }); if (error) { throw error;