title: Express.js + Sequelize + Form validation
body: <p>I'm working on my first Express + Sequelize application and would like to use Sequelize's validation mechanism to validate submitted forms without having to re-implement all validations on the frontend (or use additional validation libraries like express-validator, etc.). So, basically I'd like to do something like the following:</p>
  
  <pre><code>var signup = function(req, res) {
      res.render('signup', { title: 'Sign-up' });
  }
  
  router.get('/signup', signup);
  
  router.post('/signup', function(req, res) {
      User.create({
          email: req.body.email,
          ...
      }).then(function(user) {
          req.flash('info', 'User created successfully.');
          res.redirect('/login');
      }).catch(function(err) {
          // [1] Bring sequelize's error messages into a suitable form somehow
          if (req.xhr) {
              res.json(err);
          }
          else {
              // [2] Fallback for non-JS browsers
              req.flash('error', err);
              signup(req, res);
          }
      });
  });
  </code></pre>
  
  <ol>
  <li>What's a good way to solve [1] without manually massaging Sequelize error messages into a JSON object? </li>
  <li>How would you process the JSON on the frontend to have fields marked as containing errors without manually doing so for every field?</li>
  <li>What's the proposed way to repopulate the form with the posted data? I've searched the web high and low, but it's hard to come across any information about this at all and everybody seems to just manually set the value attribute in the template (which gets tedious when dealing with check-/radio-/combo boxes). I know that this is not a problem when submitting the form via AJAX, but forms should always also work without javascript IMHO. </li>
  </ol>
  
  <p>Thanks a lot!</p>