Yup resolver silently ignores error that are not ValidationError
See original GitHub issueDescribe the bug
For example, when a TypeError (TypeError: You cannot `concat()` schema's of different types: string and number
) is thrown by the schema validator (validator of the schema itself), the yup resolver silently ignores the error because the error.field
described in this line of code is missing, falling back to a blank array.
To Reproduce Steps to reproduce the behavior:
- Create a simple object schema like below and use it in the input element:
import React from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import "./styles.css";
const SignupSchema = yup.object().shape({
name: yup.string().required(),
value: yup.string().when("name", {
is: "test",
then: yup.number().required()
// TypeError: You cannot `concat()` schema's of different types: string and number
// is thrown in this case but there isn't any warning about that.
// As the error is silently ignored, when the user clicks the submit button
// nothing happens instead of alerting the stringified json data as described in line 27 of this sandbox.
})
});
function App() {
const { register, handleSubmit, errors } = useForm({
resolver: yupResolver(SignupSchema)
});
const onSubmit = (data) => {
alert(JSON.stringify(data));
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<div>
<label>Name</label>
<input type="text" name="name" ref={register} />
{errors.name && <p>{errors.name.message}</p>}
</div>
<div>
<label>Value</label>
<input type="text" name="value" ref={register} />
{errors.value && <p>{errors.value.message}</p>}
</div>
<input type="submit" />
</form>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
- Write ‘test’ in the first input element
- Write any text in the second input element
- Submit the form by clicking the button, but nothing happens.
Codesandbox link (Required) Include a codesandbox will help us to investigate the issue quicker.
My fork of yup resolver template
- Yup resolver Template
- Zod resolver Template
- Joi resolver Template
- Superstruct resolver Template
- Vest resolver Template
- Custom resolver Template
Expected behavior
Error TypeError: You cannot `concat()` schemes of different types: string and number
thrown.
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
Smartphone (please complete the following information):
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Yep, sounds good to me. I’ll open a PR. Thanks for the issue.
Fixed in the last version