--- name: loom-data-validation description: Data validation patterns covering schema validation, input sanitization, output encoding, and type coercion. triggers: - validate - validation - schema - form validation - API validation - JSON Schema - Zod - Pydantic - Joi - Yup - Ajv - class-validator - sanitize - sanitization - XSS prevention - injection prevention - escape - encode - output encoding - whitelist - allowlist - blacklist - denylist - coercion - ReDoS - mass assignment - constraint checking - invariant validation - data pipeline validation - ML feature validation - custom validators - Great Expectations - data quality - data drift --- # Data Validation ## Overview Validate untrusted data at trust boundaries before it flows into your system. This skill covers schema libraries (Zod/Pydantic/Joi/JSON Schema), coercion pitfalls, context-dependent output encoding, injection/XSS/DoS defenses, and pipeline/ML feature validation. ## Core principles (read first) - **Parse, don't validate.** A validator that returns `bool` throws away work — the caller re-parses or trusts blindly. Return a *typed value* (`Result`, `User | errors`) so downstream code cannot receive unvalidated data. Schema libraries (Zod `.parse`, Pydantic `.model_validate`) do this by construction. - **Validate at the boundary, once, then trust the typed value inward.** Boundaries: HTTP handlers, queue consumers, file/CLI parsers, pipeline ingestion, cross-service calls. - **Server-side is authoritative; client-side validation is UX only.** Never rely on it for security — attackers bypass the client entirely. - **Allowlist > denylist.** Enumerate what's permitted (`enum`, char classes, known hosts). Denylists (blocking `` in a JS string ends the tag | | URL component | `encodeURIComponent` | but `javascript:` scheme still executes — allowlist scheme | | CSS value | `\HH` hex (space-terminated) | `expression()`/`url()` injection | | SQL | **parameterized queries** | escaping is not a substitute | | Shell | avoid; pass argv arrays, never a string | quoting is unwinnable | ⚠ Prefer framework auto-escaping (React JSX, Jinja2 autoescape, template engines) over manual encoders. Manual encoding is for the gaps (`dangerouslySetInnerHTML`, `|safe`, building HTML strings). React escapes text but **not** `href="javascript:…"`, `dangerouslySetInnerHTML`, or `