Validate untrusted data at a TypeScript boundary | DevPrompt Lab
Build a runtime parser that narrows unknown input and returns useful errors.
prompt · TypeScript · intermediate
English Tiếng ViệtAvoid unsafe casts by validating data where it enters the application and deriving precise types from the validation result.
Reusable content
Implement validation for the supplied external input. Treat incoming data as unknown. Check required fields, types, length and range limits, allowed enum values, nested object shapes, and cross-field rules. Return a typed result or structured validation errors that fit the project's conventions. Do not use `as` to bypass checks. Reuse the project's validation library if present; otherwise keep the parser small and testable. Include valid, missing, malformed, boundary, and extra-field cases.
Input: {{INPUT}}
Contract: {{CONTRACT}}
Existing libraries: {{LIBRARIES}}
Error conventions: {{ERROR_STYLE}}
How to use it
Show the actual runtime payload shape and whether unknown fields should be rejected or ignored.
Example
const result = parseProjectContext(body.context); if (!result.ok) return validationError(result.issues); saveProject(result.value);