3 ms·
App layer validation is for the user’s sanity. DB layer validation is for the developer’s sanity.
by pennomi 2y ago
App layer validation is for the user’s sanity. DB layer validation is for the developer’s sanity.
- papichulo2023 2y agoEhhh, most dbs are managed by migrations in the application, so essentially is duplicated logic
- eropple 2y agoMost databases eventually have a human typing an INSERT statement or the moral equivalent. It's not duplication to have your correctness functions protect that, too.
- goosejuice 2y agoI like this as a general rule, but I almost always have more refined validation at the app layer -- particularly when using jsonb. Tool of choice is ecto which has excellent support for maintaining jsonb structure.
- evanelias 2y agoI'd assume this might be solvable in the DB layer using CHECK constraints, possibly combined with JSON Schema validation? From quick glance, JSON Schema validation isn't built-in to Postgres, but there are third-party extensions, such as https://github.com/supabase/pg_jsonschema https://github.com/supabase/pg_jsonschema or https://github.com/gavinwahl/postgres-json-schema https://github.com/gavinwahl/postgres-json-schema. I'm more familiar with MySQL and MariaDB, which offer it built-in: https://dev.mysql.com/doc/refman/8.0/en/json-validation-functions.html https://dev.mysql.com/doc/refman/8.0/en/json-validation-func... and https://mariadb.com/kb/en/json_schema_valid/ https://mariadb.com/kb/en/json_schema_valid/