DEV Community

Discussion on: Sequalize model validation or express-validator?

Collapse
 
dmfay profile image
Dian Fay

It's good to validate quickly so you can return easy-to-catch errors to the user with a minimum of waiting, and express-validator or any of several other packages will do that perfectly well. But there's one important thing to remember: only the database can truly enforce validation rules. A validation rule in application code is akin to saying "it'd be nice if that field had something in it". If you want to make sure the field has something in it no matter what, set NOT NULL and add a CHECK constraint (unless you're using MySQL <8, which ignores the latter).

For your situation, Sequelize model validation sounds like the worst of both worlds: slower than early checks, not actually enforced by the database.

Collapse
 
adrian110288 profile image
Adrian

Thank you :) I will then move my validation rule checking into a route handler itself and have actual SQL table constraints as a safety net to be absolutely sure my tables don't take undesirable values.