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()
})

```

})
I modify the schema on the fly with a transform

```
  const updateUserValidator = yup.object().shape({
        email: yup.string(),
        updateBy: yup.string()
    }).transform(function (current, original) {
        this.fields.email = original?.email?.length === undefined ? yup().string().email() : yup().string().email().required()
        return current;
        })

```