Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yupResolver): Yup fails silently #328

Merged
merged 1 commit into from
Jan 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
restoreMocks: true,
testMatch: ['**/__tests__/**/*.+(js|jsx|ts|tsx)'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
testPathIgnorePatterns: ['/__fixtures__/', '/\\.'],
moduleNameMapper: {
'^@hookform/resolvers$': '<rootDir>/src',
},
Expand Down
18 changes: 18 additions & 0 deletions yup/src/__tests__/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,22 @@ describe('validateWithSchema', () => {
errors: { name: { message: 'Email or name are required', type: 'name' } },
});
});

it('should throw an error without inner property', (done) => {
const schemaWithWhen = yup.object({
name: yup.string().required(),
value: yup.string().when('name', {
is: 'test',
then: yup.number().required(),
}),
});

// @ts-expect-error
yupResolver(schemaWithWhen)({ name: 'test', value: '' }).catch((e) => {
expect(e).toMatchInlineSnapshot(
`[TypeError: You cannot \`concat()\` schema's of different types: string and number]`,
);
done();
});
});
});
6 changes: 5 additions & 1 deletion yup/src/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export const yupResolver = <TFieldValues extends FieldValues>(
}),
errors: {},
};
} catch (e) {
} catch (e: any) {
if (!e.inner) {
throw e;
}

const parsedErrors = parseErrorSchema(e, validateAllFieldCriteria);
return {
values: {},
Expand Down