Manual validation helps check user data before saving or using it. It stops errors and keeps your app safe.
Manual validation patterns in Express
app.post('/route', (req, res) => { const { field } = req.body; if (!field || typeof field !== 'string') { return res.status(400).send('Invalid input'); } // continue processing res.send('Success'); });
req.body to get data sent by the user.app.post('/login', (req, res) => { const { email, password } = req.body; if (!email || !password) { return res.status(400).send('Email and password are required'); } res.send('Login data is valid'); });
app.post('/signup', (req, res) => { const { username } = req.body; if (typeof username !== 'string' || username.length < 3) { return res.status(400).send('Username must be at least 3 characters'); } res.send('Username is valid'); });
app.post('/age-check', (req, res) => { const age = Number(req.body.age); if (isNaN(age) || age < 18) { return res.status(400).send('You must be 18 or older'); } res.send('Age is valid'); });
This Express app listens for POST requests to /register. It manually checks if username, email, and password meet simple rules. If any check fails, it sends a clear error message. If all pass, it confirms the data is valid.
import express from 'express'; const app = express(); app.use(express.json()); app.post('/register', (req, res) => { const { username, email, password } = req.body; if (!username || typeof username !== 'string' || username.length < 3) { return res.status(400).send('Username must be at least 3 characters long'); } if (!email || !email.includes('@')) { return res.status(400).send('Email must be valid'); } if (!password || password.length < 6) { return res.status(400).send('Password must be at least 6 characters'); } res.send('Registration data is valid'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Always return a response after validation to stop further processing.
Manual validation is simple but can get complex for many fields; consider libraries for bigger apps.
Check data types and required fields carefully to avoid crashes.
Manual validation checks user input step-by-step in your route handlers.
It helps keep your app safe and user-friendly by catching bad data early.
Use clear error messages to guide users to fix their input.
Practice
Solution
Step 1: Understand manual validation role
Manual validation means checking user input carefully in your code before using it.Step 2: Identify the main goal
The goal is to catch bad or incorrect data early to keep the app safe and user-friendly.Final Answer:
To check user input step-by-step and catch bad data early -> Option AQuick Check:
Manual validation = catch bad data early [OK]
- Thinking validation auto-generates database code
- Believing validation speeds up server by skipping checks
- Confusing validation with UI styling
Solution
Step 1: Check for missing or empty username
Using!req.body.usernamechecks if username is missing or empty string.Step 2: Respond with error status and message
Sending status 400 with message 'Username required' correctly informs client of bad input.Final Answer:
if (!req.body.username) { res.status(400).send('Username required'); } -> Option BQuick Check:
Check missing username and send 400 error [OK]
- Using next() instead of sending error response
- Sending 200 OK on invalid input
- Redirecting instead of responding with error
<pre>app.post('/submit', (req, res) => { if (typeof req.body.age !== 'number' || req.body.age < 18) { return res.status(400).send('Age must be 18 or older'); } res.send('Welcome!'); });
What will be the response if the client sends {"age": 16} in JSON body?
Solution
Step 1: Check age type and value
The code checks if age is not a number or less than 18. Here age is 16, a number but less than 18.Step 2: Return 400 error with message
Since age < 18, the code returns status 400 with message 'Age must be 18 or older'.Final Answer:
Status 400 with message 'Age must be 18 or older' -> Option CQuick Check:
Age 16 triggers 400 error [OK]
- Assuming 16 passes validation
- Expecting 200 OK instead of error
- Thinking server crashes on invalid input
app.post('/login', (req, res) => {
if (req.body.password.length < 8) {
res.status(400).send('Password too short');
}
res.send('Login successful');
});Solution
Step 1: Analyze error response flow
The code sends error response if password is too short but does not stop execution.Step 2: Identify missing return causes double response
Withoutreturn, the code continues and sends 'Login successful' response, causing error.Final Answer:
Missing return after sending error response causes double response -> Option AQuick Check:
Return after error response to stop execution [OK]
- Thinking length check direction is wrong
- Confusing req.body with req.query
- Believing res.send must be res.json
Solution
Step 1: Validate email presence and type
if (!req.body.email || typeof req.body.email !== 'string')checks for missing, empty, or non-string email and returns 400 error if invalid.Step 2: Validate password length correctly
if (!req.body.password || req.body.password.length < 8)checks for missing or short password (<8 chars) and returns 400 error.Step 3: Validate optional age correctly
if (req.body.age !== undefined && (typeof req.body.age !== 'number' || req.body.age < 13))checks if age provided, then ensures it's a number >=13, returns 400 if invalid.Final Answer:
if (!req.body.email || typeof req.body.email !== 'string') { return res.status(400).send('Email required'); } if (!req.body.password || req.body.password.length < 8) { return res.status(400).send('Password too short'); } if (req.body.age !== undefined && (typeof req.body.age !== 'number' || req.body.age < 13)) { return res.status(400).send('Age must be 13 or older'); } next(); -> Option DQuick Check:
All fields validated with correct conditions and error codes [OK]
- Not returning after sending error response
- Using wrong status codes like 200 or 500 for validation errors
- Checking wrong types or missing optional field checks
Ready to prove it?
Pass the test and this pattern moves unknown
