title: Yup / Formik validation using dynamic keys
body: <p>I am trying to validate a form with a dynamic amount of fields - i.e., data is returned from an API that determines how many rows are shown, and for each row there is a required field that needs the user to select an input for them to advance.</p>
  <p>The package to be used is Yup alongside Formik. When looking on Yup tutorials, the object is usually built as follows:</p>
  <pre><code>let userSchema = object({
    name: string().required(),
  });
  </code></pre>
  <p>Where keys like name are defined. However, my keys will need to be dynamic i.e. field1, field2 etc. as I do not know ahead of time how many of them there will be.</p>
  <p>I want to loop through my object and pass a dynamic set of keys to the schema - basically however long the object is, will be how many keys I have.</p>
  <pre><code>let userSchema = object({
    [field1]: string().required(),
    [field2]: string().required(),
  });
  </code></pre>
  <p>However, I am unsure of how to achieve this result. I can loop through my object and try to build a set of keys, rough e.g.</p>
  <pre><code>let myObject = {}
  myKeyObject.forEach((key) =&gt; myObject[key] = string().required());
  </code></pre>
  <p>And then pass <code>myKeyObject</code> to object.shape, but this usually produces TS errors. Does anyone know of any utility within Yup for a dynamic form? Unless there is something I have missed, I don't see anything on the documentation that would make working with dynamic forms easier</p>