Manual Schema Parser Without Libraries
Validate untrusted input with a small hand-rolled parser that returns a typed result — no schema library needed for flat shapes.
Code
type Result<T> = { ok: true; value: T } | { ok: false; errors: string[] };
interface Signup {
email: string;
age: number;
}
function parseSignup(input: unknown): Result<Signup> {
const errors: string[] = [];
if (typeof input !== 'object' || input === null) {
return { ok: false, errors: ['payload must be an object'] };
}
const raw = input as Record<string, unknown>;
const email = raw.email;
if (typeof email !== 'string' || !email.includes('@')) {
errors.push('email must be a string containing @');
}
const age = raw.age;
if (typeof age !== 'number' || !Number.isInteger(age) || age < 13) {
errors.push('age must be an integer >= 13');
}
if (errors.length > 0) return { ok: false, errors };
return { ok: true, value: { email: email as string, age: age as number } };
}
console.log(parseSignup({ email: '[email protected]', age: 20 }));
console.log(parseSignup({ email: 'nope', age: 12 }));
console.log(parseSignup('not an object'));Output
Captured from a sandboxed run of this snippet in the build pipeline.
{ ok: true, value: { email: '[email protected]', age: 20 } }
{
ok: false,
errors: [
'email must be a string containing @',
'age must be an integer >= 13'
]
}
{ ok: false, errors: [ 'payload must be an object' ] }What each part contributes
Manual Schema Parser Without Libraries comes down to the function parseSignup.
The result comes back as an object carrying false.
Points of care
- The guard
typeof input !== 'object' || input === nullshort-circuits the flow with an earlyreturnwhen its condition holds.
Putting it to use
The main signature looks like function parseSignup(input: unknown)
The call returns false.
Core lines
return { ok: true, value: { email: email as string, age: age as number } };
return { ok: false, errors: ['payload must be an object'] };
if (errors.length > 0) return { ok: false, errors };
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.
FAQ
- Does `parseSignup` modify its input?
- No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
- What does the package manager see?
- An empty dependency file. The snippet imports nothing that needs fetching. The file defines `parseSignup` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.