{"success":true,"name":"shape-api-responses-with-dtos-never-return-raw-database-mode","description":"An endpoint returns whatever the database query gives back","category":"ai-brimind","path":"ai-brimind/shape-api-responses-with-dtos-never-return-raw-database-mode/SKILL.md","file":"SKILL.md","content":"---\nname: shape-api-responses-with-dtos-never-return-raw-database-mode\ndescription: \"An endpoint returns whatever the database query gives back\"\n---\n\n# Shape API Responses With DTOs — Never Return Raw Database Models\n\n## When to use\nYou are building or reviewing an endpoint that serializes database records straight into the response without a defined output shape.\n\n## Example request\nUser record returned as-is → password hash and internal flags ship to the browser; the response needed a deliberate shape, not a dump. — Database column renamed → every API consumer breaks instantly; the DTO layer is the shock absorber between schema and contract. — Response includes 40 fields, clients use 5 → payloads bloat and every schema detail becomes public contract; explicit field selection is the fix. — Two endpoints serialize the same model differently by accident → clients see inconsistent shapes; a shared serializer is the single owner.\n\n## Instructions\nAct as a boundary-keeper who never lets the database schema leak into the API contract. For each response: 1) Define the output shape explicitly — a DTO/serializer listing exactly which fields go out, their names, and their types; the response is a designed contract, not a query result. 2) Whitelist, never blacklist: pick the fields to include rather than stripping known-bad ones — new sensitive columns must be private by default. 3) Check for the project's existing serializer/transformer mechanism and reuse it so the same entity always serializes the same way everywhere. 4) Keep names and structure stable even when the schema changes — the DTO is where you absorb renames and refactors without breaking consumers. 5) If unsure whether a field is safe or needed, leave it out and ask — adding a field later is easy, removing a leaked one is a breaking change (or an incident). Only ship once the response shape is confirmed deliberate field-by-field, and explain how the DTO protects both secrets and the contract rather than just reformatting data.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #904\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}