-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (36 loc) · 1.26 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const Joi = require('joi');
const schema = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().email().required(),
message: Joi.string().required()
});
app.post('/submit-form', (req, res) => {
const { firstName, lastName, email, message } = req.body;
const { error } = schema.validate({ firstName, lastName, email, message });
if (error) {
return res.status(400).send(error.details[0].message);
}
res.send('Form submitted successfully!');
});
const validator = require('validator');
app.post('/submit-form', (req, res) => {
const { firstName, lastName, email, message } = req.body;
const errors = [];
if (!validator.isLength(firstName, { min: 2, max: 50 })) {
errors.push('First name must be between 2 and 50 characters');
}
if (!validator.isLength(lastName, { min: 2, max: 50 })) {
errors.push('Last name must be between 2 and 50 characters');
}
if (!validator.isEmail(email)) {
errors.push('Email is not valid');
}
if (!validator.isLength(message, { min: 10, max: 1000 })) {
errors.push('Message must be between 10 and 1000 characters');
}
if (errors.length > 0) {
return res.status(400).json({ errors });
}
res.send('Form submitted successfully!');
});