freewil / express-form

Form validation and data filtering for Express
MIT License
89 stars 21 forks source link

Move data validation logic to a separate module #18

Open maxmalov opened 10 years ago

maxmalov commented 10 years ago

Hi. I've found express-form very useful, but only for express applications. For plain objects I can use https://www.npmjs.org/package/node-validator, but it didn't use validator package, which isn't nice. I think express-form can be easily refactored into 2 packages. First one is about data validation (with chaining and all cool stuff), and the second one is about express middleware specific. Here are some thoughts:

Object validation

// create validator bundle
var v = validator(
  validator.property('name').trim().toString().requred(), // alias for form.field
  // other  routines
);

// perform data validation
v.run(objectToValidate, function (err, data) {
  // err - validation errors (same as in the middleware) or null
  // data - sanitized objectToValidate
});

Express middleware

form factory for middleware will just delegate all validation specific logic to the validation module.

Any thoughts?

freewil commented 10 years ago

I think this makes a lot of sense. If you or someone else wants to work on that, I would accept a pull request to do that. :+1:

If you want some feedback, I would also be happy to do that.