Skip to content
Learn Netverks

Lesson

Step 26/36 72% through track

validation-basics

Input validation basics

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Input validation basics: the syntax, APIs, and habits you need before advancing in Node.js.

Teams ship Input validation basics on every Node.js codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Input validation basics in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

Every API boundary is untrusted—validate shape, types, and business rules before persistence. Libraries like Zod, Joi, or express-validator reduce boilerplate.

Manual checks

function parseCreateUser(body) {
  const email = String(body.email ?? '').trim();
  if (!email.includes('@')) throw new Error('Invalid email');
  const age = Number(body.age);
  if (!Number.isInteger(age) || age < 0) throw new Error('Invalid age');
  return { email, age };
}

Schema validation (Zod concept)

// npm install zod — locally
// const schema = z.object({ email: z.string().email(), age: z.number().int().min(0) });

Whitelist approach

Reject unknown fields in strict APIs—prevents mass-assignment vulnerabilities where clients set isAdmin: true.

Important interview questions and answers

  1. Q: Validate on client and server?
    A: Client for UX; server is authoritative—never skip server validation.
  2. Q: Sanitize vs validate?
    A: Validate ensures data meets rules; sanitize transforms (trim, escape)—both may apply.

Self-check

  1. Why reject unknown JSON fields in strict APIs?
  2. What should happen when validation fails?

Pitfall: Client-side validation is UX only—always validate and sanitize on the server before trusting data.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Schema lib why?
  • Trust client input?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump