title: How to generate Joi validations via code, based on an object and save it in a file using Nodejs
body: <p>I am trying to generate Joi validations via code based on an object. I would like to know if there is a way to generate them programatically and store them into a file. I tried the following way:</p>
  <p>Lets say I have an object as follows:</p>
  <pre><code>let obj = [{
    Key: 'key1',
    Type: 'string'  
  }]
  </code></pre>
  <p>A function to generate an object of Joi validations:</p>
  <pre><code>function getJoiObject(data) {
      const obj = {};
      Object.keys(data).forEach(key =&gt; {
          if (data[key].Type === &quot;string&quot;)
              obj[data[key].Key] = Joi.string();
          else if (data[key].Type === &quot;integer&quot;)
              obj[data[key].Key] = Joi.number();
          else if (data[key].Type === &quot;boolean&quot;)
              obj[data[key].Key] = Joi.boolean();
      });
      return obj;
  } 
  </code></pre>
  <p>I saved the object onto a file and used it in another file for validations.</p>
  <pre><code> fs.writeFileSync('validations.json', JSON.stringify({res:getJoiObject(obj)}));
  </code></pre>
  <p>The problem is that when I import the validations.json into another file and validate it with data, it wouldn't work.</p>
  <pre><code>const a = {key1:'abc'};
  Joi.object(validations.res).validate(a)
  </code></pre>
  <p>Could you please help.</p>
  <p>Thanks in advance.</p>