parse-dont-validate
Skill9 starsChanged 4 months agoMITView on GitHub
Type-driven design principle: transform unstructured data into structured types at system boundaries, making illegal states unrepresentable. Use when writing or reviewing code that validates input, designs data types, defines function signatures, handles errors, or models domain logic. Use when you see validation functions that return void/undefined, redundant null checks, stringly-typed data, boolean flags controlling behavior, or functions that can receive data they shouldn't. Triggers on: "parse don't validate", "type-driven design", "make illegal states unrepresentable", "input validation", "data modeling", "refactor types", "strengthen types", "smart constructor", "newtype", "branded type".
2 other projects use a version of this.
Adopted by
Most recently taken up by
- auralshin/coding-skillstheir own version, first seen Aug 27, 2026
- selamy-labs/agent-skillstheir own version, first seen Jul 12, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- auralshin/coding-skills★ 0same name
- selamy-labs/agent-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository25 other files it gives its agents.
- adversarial-reviewSkill
- agent-first-repoSkill
- agents-mdSkill
- architecture-mdSkill
- behavioral-testingSkill
- cleanup-swiftSkill
- cleanup-webSkill
- clear-communicationSkill
- code-commentsSkill
- design-patterns-gofSkill
- design-systemSkill
- distill-to-skillSkill
- git-safe-pr-workflowSkill
- git-worktreeSkill
- karpathy-guidelinesSkill
- lean-ts-patternsSkill
- macos-swift-desktopSkill
- modern-cssSkill
- no-use-effectSkill
- pr-review-triageSkill
- simplifySkill
- technical-writingSkill
- typescript-refactoringSkill
- typescriptSkill
- update-readmeSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 5 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 43
When a function is partial (not defined for all inputs), there are exactly two ways to make it total:
- Distinctive: line 75
Use the most precise data structure you reasonably can.
- Distinctive: line 133
When making an illegal state truly unrepresentable is impractical (e.g., "integer in range 1-100"), use branded types with smart …
- Very specific: line 153
Don't stick a boolean in a record because your current function needs it.
- Distinctive: line 171
If denormalization is necessary for performance, hide it behind an abstraction boundary where a small, trusted module keeps representations …
- Distinctive: line 200
When reviewing code, watch for these smells:
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Parse, Don't Validate
Based on Alexis King's article: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
A parser is a function that consumes less-structured input and produces more-structured output. Validation checks a property and throws it away. Parsing checks a property and preserves it in the type system. Always prefer parsing.
The Core Idea
// VALIDATION: checks a property, returns nothing useful
function validateNonEmpty(list: string[]): void {
if (list.length === 0) throw new Error("list cannot be empty");
}
// PARSING: checks the same property, returns proof in the type
function parseNonEmpty<T>(list: T[]): [T, ...T[]] {
if (list.length === 0) throw new Error("list cannot be empty");
return list as [T, ...T[]];
}Both check the same thing. But parseNonEmpty gives the caller access to what it learned.
validateNonEmpty throws the knowledge away, forcing every downstream function to either
re-check or hope for the best.
The Two Strategies
When a function is partial (not defined for all inputs), there are exactly two ways to make it total:
1. Weaken the output (add Maybe/null)
function head<T>(list: T[]): T | undefined {
return list[0];
}Easy to implement, annoying to use. Every caller must handle undefined even if they already
know the list is non-empty. Leads to redundant checks and // should never happen comments.
2. Strengthen the input (narrow the type) -- PREFER THIS
function head<T>(list: [T, ...T[]]): T {
return list[0];
}The check happens once, at the boundary, when the data enters the system. After that, the type carries the proof. No redundant checks. No impossible branches. If the validation logic changes, the compiler catches every affected call site.
Always try strategy 2 first. Fall back to strategy 1 only when 2 is impractical.
Practical Rules
1. Make illegal states unrepresentable
Use the most precise data structure you reasonably can. Don't model things you shouldn't allow.
// BAD: allows duplicate keys, order might matter or might not
type Config = Array<[string, string]>;
// GOOD: duplicates impossible by construction
type Config = Map<string, string>;
// or even better if keys are known:
type Config = { host: string; port: number; debug: boolean };2. Push parsing to the boundary
Parse data into precise types as soon as it enters your system. The boundary between your program and the outside world is where parsing belongs.
// BAD: raw data flows deep into the system, validated ad-hoc
function processUser(data: unknown) {
// 50 lines later...
if (typeof data.email !== "string") throw new Error("invalid email");
}
// GOOD: parse at the boundary, use precise types everywhere else
interface User { name: string; email: string; age: number; }
function parseUser(data: unknown): User {
// validate and parse here, once
}
function processUser(user: User) {
// no validation needed -- the type guarantees it
}3. Treat void-returning validators with deep suspicion
A function whose primary purpose is checking a property but returns void is almost always
a missed opportunity. It should return a more precise type instead.
// SUSPICIOUS: checks something, returns nothing
function validateAge(age: number): void {
if (age < 0 || age > 150) throw new Error("invalid age");
}
// BETTER: returns proof of validity as a branded type
type ValidAge = number & { readonly __brand: "ValidAge" };
function parseAge(age: number): ValidAge {
if (age < 0 || age > 150) throw new Error("invalid age");
return age as ValidAge;
}4. Use branded/opaque types as "fake parsers"
When making an illegal state truly unrepresentable is impractical (e.g., "integer in range 1-100"), use branded types with smart constructors to fake it:
type EmailAddress = string & { readonly __brand: "EmailAddress" };
function parseEmail(input: string): EmailAddress {
if (!input.includes("@")) throw new Error("invalid email");
return input as EmailAddress;
}
// Now functions can demand EmailAddress instead of string
function sendEmail(to: EmailAddress, body: string): void { /* ... */ }The type system won't let you pass a raw string where EmailAddress is expected.
You must go through parseEmail first.
5. Let types inform code, not vice versa
Don't stick a boolean in a record because your current function needs it. Design the
types first, then write functions that transform between them.
// BAD: boolean flag controlling behavior
interface Request { url: string; isAuthenticated: boolean; token?: string; }
// GOOD: discriminated union makes invalid state impossible
type Request =
| { kind: "anonymous"; url: string }
| { kind: "authenticated"; url: string; token: string };6. Avoid denormalized data
Duplicating the same information in multiple places creates a trivially representable illegal state: the copies getting out of sync. Strive for a single source of truth.
If denormalization is necessary for performance, hide it behind an abstraction boundary where a small, trusted module keeps representations in sync.
7. Parse in multiple passes if needed
Avoiding shotgun parsing means don't act on data before it's fully parsed. It doesn't mean you can't use some input data to decide how to parse other input data.
// Fine: first parse the header to determine the format, then parse the body
const header = parseHeader(raw);
const body = parseBody(raw, header.format);Shotgun Parsing -- The Anti-Pattern
Shotgun parsing is when validation code is mixed with and spread across processing code. Checks are scattered everywhere, hoping to catch all bad cases without systematic justification.
The danger: if a late-discovered error means some invalid input was already partially processed, you may need to roll back state changes. This is fragile and error-prone.
Parsing avoids this by stratifying the program into two phases:
- Parsing phase -- failure due to invalid input can only happen here
- Execution phase -- input is known-good, failure modes are minimal
Code Review Checklist
When reviewing code, watch for these smells:
- Function accepts
stringwhere a more specific type exists (URL, email, ID) - Validation function returns
voidinstead of a refined type - Same property checked in multiple places (redundant validation)
// should never happenor// impossiblecomments- Raw
unknown/any/objectflowing past the system boundary into business logic - Boolean fields that could be discriminated unions
- Optional fields that are actually always present after a certain point
- Arrays where non-empty arrays are required
nullchecks deep in business logic for data validated at entry
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

