Performance: Schema validation
MEDIUM IMPACT
Schema validation affects the server response time and user experience by adding processing before sending data back or accepting input.
import Joi from 'joi'; const schema = Joi.object({ name: Joi.string().required(), age: Joi.number().required() }); app.post('/user', (req, res) => { const { error } = schema.validate(req.body); if (error) return res.status(400).send(error.message); res.send('User created'); });
app.post('/user', (req, res) => { const data = req.body; if (!data.name || typeof data.name !== 'string') { return res.status(400).send('Invalid name'); } if (data.age === undefined || typeof data.age !== 'number') { return res.status(400).send('Invalid age'); } // more manual checks... res.send('User created'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual validation with many checks | 0 (server-side) | 0 | 0 | [X] Bad |
| Schema validation with Joi or similar | 0 (server-side) | 0 | 0 | [OK] Good |