Question: Is it possible to validate a field only when it exists?

I'm going to create an application. A field(email) may / may not display on step 2, it depends on the step 1's selected option. The problem is that I can't put `email: Yup.string().Required()`, it causes that the validation run all the time even the field is not displayed.

Answer: 
this will help you

```
const schema = yup.object({
phone: yup.string().when('$exist', {
    is: exist => exist,
    then: yup.string().required(),
    otherwise: yup.string()
})

```

})
You need to handle with manually, not with required. Just you need to keep a state of the each field. And check it if its value is present then you need to show it.
Some like this

```js
const [email, setEmail]= useState(null);

const onChange=(e)=>{
setEmail(e.target.value)
}

return (

<input type="text" onChange={onChnage}/>
)
```

And pass this email to your parent where you checking for validation. If there is any thin in **email** then show it do other validation. Or if it is empty or Null then don't show it.

Thanks