Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions joi/src/__tests__/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
12 changes: 9 additions & 3 deletions joi/src/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down