0
0
Expressframework~20 mins

Validating body fields in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Body Field Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending a POST request with missing required fields?

Consider this Express route that validates the presence of 'username' and 'password' in the request body. What response will the server send if the client sends a POST request with an empty JSON body?

Express
const express = require('express');
const app = express();
app.use(express.json());

app.post('/login', (req, res) => {
  const { username, password } = req.body;
  if (!username || !password) {
    return res.status(400).json({ error: 'Missing username or password' });
  }
  res.status(200).json({ message: 'Login successful' });
});
AStatus 404 with JSON { error: 'Not found' }
BStatus 400 with JSON { error: 'Missing username or password' }
CStatus 500 with JSON { error: 'Server error' }
DStatus 200 with JSON { message: 'Login successful' }
Attempts:
2 left
💡 Hint

Think about what happens when required fields are missing in the request body.

📝 Syntax
intermediate
2:00remaining
Which option correctly validates that 'email' is a non-empty string in the request body?

Which code snippet correctly checks that the 'email' field exists and is a non-empty string in an Express POST route?

Express
app.post('/subscribe', (req, res) => {
  // validation here
});
Aif (req.body.email !== undefined) { res.send('Valid'); } else { res.status(400).send('Invalid email'); }
Bif (req.body.email) { res.send('Valid'); } else { res.status(400).send('Invalid email'); }
Cif (req.body.email.length > 0) { res.send('Valid'); } else { res.status(400).send('Invalid email'); }
Dif (typeof req.body.email === 'string' && req.body.email.trim() !== '') { res.send('Valid'); } else { res.status(400).send('Invalid email'); }
Attempts:
2 left
💡 Hint

Check both type and content to ensure 'email' is a non-empty string.

🔧 Debug
advanced
2:00remaining
What is the response when sending a POST request with missing 'age' field?

Look at this Express route snippet. What response will the server send if the client sends a POST request without the 'age' field?

Express
app.post('/profile', (req, res) => {
  const { age } = req.body;
  if (age && typeof age === 'number' && age > 0) {
    res.send('Valid age');
  } else {
    res.status(400).send('Invalid age');
  }
});
ABecause 'age' is undefined, the condition 'age && ...' is skipped, so it returns 200 (Valid age).
BBecause 'age' is undefined, the condition 'age && ...' is false, so it returns 200 (Valid age).
CBecause 'age' is undefined, the condition 'age && ...' is false, so it returns 400 (Invalid age).
DBecause 'age' is undefined, destructuring throws an error, resulting in status 500.
Attempts:
2 left
💡 Hint

Think about how JavaScript treats undefined in logical AND expressions.

state_output
advanced
2:00remaining
What is the response when sending a POST with 'age' as a string '25'?

Given this Express route, what response will the server send if the client sends { "age": "25" } in the JSON body?

Express
app.post('/check-age', (req, res) => {
  const { age } = req.body;
  if (typeof age === 'number' && age >= 18) {
    res.status(200).json({ message: 'Access granted' });
  } else {
    res.status(403).json({ message: 'Access denied' });
  }
});
AStatus 403 with JSON { message: 'Access denied' }
BStatus 200 with JSON { message: 'Access granted' }
CStatus 400 with JSON { error: 'Invalid age type' }
DStatus 500 with JSON { error: 'Server error' }
Attempts:
2 left
💡 Hint

Check the type of 'age' in the condition.

🧠 Conceptual
expert
3:00remaining
Which middleware approach best ensures all required body fields are validated before route handlers?

You want to validate that the request body contains 'name', 'email', and 'password' fields before any route handler runs. Which approach below best achieves this in Express?

ACreate a middleware function that checks these fields and calls next() if valid or sends 400 error if not; use app.use() to apply it globally.
BAdd validation code inside each route handler separately to check the fields before processing.
CUse a try-catch block inside each route handler to catch missing fields errors.
DRely on client-side validation only and do not validate on the server.
Attempts:
2 left
💡 Hint

Think about reusability and centralizing validation logic.