0
0
Expressframework~20 mins

Custom validation rules in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express 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 of this Express middleware with custom validation?
Consider this Express middleware that validates a request body property 'age' to be a number greater than 18. What response will the server send if the request body is {"age": 16}?
Express
app.post('/check-age', (req, res, next) => {
  const age = req.body.age;
  if (typeof age !== 'number' || age <= 18) {
    return res.status(400).json({ error: 'Age must be a number greater than 18' });
  }
  next();
}, (req, res) => {
  res.json({ message: 'Age is valid' });
});
AStatus 400 with JSON { error: 'Invalid request body' }
BStatus 200 with JSON { message: 'Age is valid' }
CStatus 500 with JSON { error: 'Server error' }
DStatus 400 with JSON { error: 'Age must be a number greater than 18' }
Attempts:
2 left
💡 Hint
Think about the condition that checks the age value and what happens when it fails.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom validation function in Express?
You want to create a custom validation function to check if a string is uppercase. Which code snippet correctly defines and uses this function in Express middleware?
Afunction isUpperCase(str) { return str === str.toUpperCase(); } app.use((req, res, next) => { if (!isUpperCase(req.body.name)) return res.status(400).send('Name must be uppercase'); next(); });
Bconst isUpperCase = (str) => { str.toUpperCase(); }; app.use((req, res, next) => { if (!isUpperCase(req.body.name)) return res.status(400).send('Name must be uppercase'); next(); });
Cfunction isUpperCase(str) { return str.toLowerCase() === str; } app.use((req, res, next) => { if (!isUpperCase(req.body.name)) return res.status(400).send('Name must be uppercase'); next(); });
Dconst isUpperCase = str => { return str.toUpperCase === str; }; app.use((req, res, next) => { if (!isUpperCase(req.body.name)) return res.status(400).send('Name must be uppercase'); next(); });
Attempts:
2 left
💡 Hint
Check if the function returns a boolean and compares correctly.
🔧 Debug
advanced
2:00remaining
Why does this custom validation middleware always pass even with invalid input?
This middleware is supposed to reject requests where 'email' is missing or invalid. Why does it allow invalid emails?
Express
app.use((req, res, next) => {
  const email = req.body.email;
  if (!email && !email.includes('@')) {
    return res.status(400).send('Invalid email');
  }
  next();
});
ABecause next() is called before the condition, so validation is skipped.
BBecause email.includes('@') throws an error if email is undefined, so middleware crashes silently.
CBecause the condition uses && instead of ||, so it only fails if email is falsy AND includes '@', which never happens.
DBecause res.status(400).send() is asynchronous and does not stop middleware execution.
Attempts:
2 left
💡 Hint
Look carefully at the if condition combining !email and !email.includes('@').
state_output
advanced
2:00remaining
What is the value of req.customData after this validation middleware runs?
This middleware adds a customData property to req if validation passes. What is req.customData after a request with body { username: 'User123' }?
Express
app.use((req, res, next) => {
  const username = req.body.username;
  if (typeof username === 'string' && username.length >= 5) {
    req.customData = { valid: true, user: username.toLowerCase() };
  } else {
    req.customData = { valid: false };
  }
  next();
});
A{ valid: false }
B{ valid: true, user: 'user123' }
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Check the username length and type, then see what customData is set to.
🧠 Conceptual
expert
2:00remaining
Which option best explains why custom validation middleware should call next() only once?
In Express, why must custom validation middleware call next() exactly once and not multiple times or skip it?
ACalling next() multiple times causes the request to be processed by multiple handlers repeatedly, leading to errors or crashes.
BCalling next() multiple times improves performance by parallelizing middleware execution.
CSkipping next() causes the server to automatically retry the request, causing infinite loops.
DSkipping next() causes the response to be sent twice, which is allowed but not recommended.
Attempts:
2 left
💡 Hint
Think about how Express middleware chains work and what happens if next() is called more than once.