Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: typescript
validationapizodschema
Validate request input with a schema library instead of hand-rolled validators
Use an established schema-validation library (e.g. Zod or Valibot) rather than building and maintaining a custom type-checking utility.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codetypescript
| 1 | if (!validateType(body.title, 'string').success) { |
| 2 | return res.status(400).json({ message: 'Invalid title' }); |
| 3 | } |
| 4 | if (!validateType(body.content, 'string').success) { |
| 5 | return res.status(400).json({ message: 'Invalid content' }); |
| 6 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | import { z } from 'zod'; |
| 2 |
|
| 3 | const schema = z.object({ title: z.string(), content: z.string() }); |
| 4 | const parsed = schema.safeParse(body); |
| 5 | if (!parsed.success) { |
| 6 | return res.status(400).json({ message: 'Invalid request body' }); |
| 7 | } |
Explanation (EN)
Objašnjenje (HR)