title: Sharing backend and frontend input validation or module
body: <p>I want to share input validation because:</p>
  
  <ul>
  <li>User experience in frontend, instantly tells the user if the input is good/wrong</li>
  <li>Security concerns in backend, even if the user bypass the frontend javascript, a user cannot mess with the RESTful API</li>
  <li>Also javascript in frontend is always conditional to the browser interpreting it and cannot be trusted</li>
  </ul>
  
  <p>What is the best way to share input validation in a full javascript web app (frontend: react, backend: nodejs)?</p>
  
  <p>The solution I am thinking of is creating a validator module with all my javascript validators at the root of my codebase:</p>
  
  <ul>
  <li>If on localhost, both environments use this module to validate input.</li>
  <li>If in production/preprod, my deployment script copy the module in both frontend and backend before deploying. </li>
  <li>Import code would look like this each time I try to validate an input: <code>const validator = process.env.ENV === 'local' ? require('../../validator') : require('/validator')</code></li>
  </ul>
  
  <p>Is there any other widely accepted method to do this (I guess it is a very common problem but I didn't find similar issues)? If not, is my method correct? </p>