0
0
Expressframework~20 mins

express-validator setup - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Validator 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-validator middleware?
Consider this Express route using express-validator. What will be the response if the request body is { "email": "invalidemail", "age": 17 }?
Express
import express from 'express';
import { body, validationResult } from 'express-validator';

const app = express();
app.use(express.json());

app.post('/register', [
  body('email').isEmail(),
  body('age').isInt({ min: 18 })
], (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }
  res.json({ message: 'User registered' });
});
AStatus 200 with message 'User registered'
BStatus 400 with errors array containing email and age validation errors
CStatus 400 with errors array containing only email validation error
DStatus 400 with errors array containing only age validation error
Attempts:
2 left
💡 Hint
Check which validations fail for the given input values.
📝 Syntax
intermediate
1:30remaining
Which option correctly imports and uses express-validator in an Express app?
Select the option that correctly sets up express-validator middleware for validating a username field as non-empty.
A
import { body } from 'express-validator';
app.post('/user', [body('username').notEmpty()], handler);
B
const { validate } = require('express-validator');
app.post('/user', [validate('username').notEmpty()], handler);
C
import validator from 'express-validator';
app.post('/user', [validator('username').notEmpty()], handler);
D
import { check } from 'express-validator';
app.post('/user', [check('username').notEmpty()], handler);
Attempts:
2 left
💡 Hint
Check the official express-validator import and method names.
🔧 Debug
advanced
2:00remaining
Why does this express-validator code not catch validation errors?
Given this code snippet, why does the server always respond with 'User created' even if the input is invalid? app.post('/create', [body('email').isEmail()], (req, res) => { res.send('User created'); });
ABecause body() middleware is not imported correctly
BBecause isEmail() does not validate email format
CBecause express.json() middleware is missing
DBecause validationResult is not called to check for errors before sending response
Attempts:
2 left
💡 Hint
Think about how express-validator reports validation errors.
state_output
advanced
2:00remaining
What is the value of errors after this validation?
Given this code snippet, what will be the value of errors.array() if the request body is { "password": "123" }? app.post('/signup', [ body('password').isLength({ min: 6 }) ], (req, res) => { const errors = validationResult(req); res.json({ errors: errors.array() }); });
A[{ msg: 'Password must be at least 6 characters', param: 'password', location: 'body' }]
B[]
C[{ msg: 'Invalid value', param: 'password', location: 'body' }]
Dnull
Attempts:
2 left
💡 Hint
Check the default error message for isLength validator.
🧠 Conceptual
expert
2:30remaining
Which statement about express-validator setup is true?
Choose the correct statement about how express-validator integrates with Express routes.
AvalidationResult(req) must be called inside the route handler to check for validation errors
Bexpress-validator middleware must be placed before express.json() to parse request body
Cexpress-validator automatically sends error responses if validation fails
DValidation chains can only be used globally, not per route
Attempts:
2 left
💡 Hint
Think about how validation errors are handled in express-validator.