Manual validation helps check user data before saving or using it. It stops errors and keeps your app safe.
0
0
Manual validation patterns in Express
Introduction
When you want to check if a form input is filled correctly before saving.
When you need to make sure an email or password meets rules.
When you want to give clear error messages to users about their input.
When you want to control exactly how data is checked without extra tools.
When you want to stop bad data from reaching your database or logic.
Syntax
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'); });
Use
req.body to get data sent by the user.Return early with an error response if validation fails.
Examples
Checks if email and password are provided before continuing.
Express
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'); });
Ensures username is a string and has minimum length.
Express
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'); });
Converts age to number and checks if user is adult.
Express
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'); });
Sample Program
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.
Express
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'); });
OutputSuccess
Important Notes
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.
Summary
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.