-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateForm.js
33 lines (28 loc) · 920 Bytes
/
validateForm.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
const Validator = require('validator')
const isEmpty = require('./is-empty')
module.exports = validateData = (data) => {
let errors = {};
data.name = !isEmpty(data.name) ? data.name : '';
data.email = !isEmpty(data.email) ? data.email : '';
data.subject = !isEmpty(data.subject) ? data.subject : '';
data.message = !isEmpty(data.message) ? data.message : '';
if (Validator.isEmpty(data.name)) {
errors.name = 'Name is required'
}
if (Validator.isEmpty(data.email)) {
errors.email = 'Email is required'
}
if (!Validator.isEmail(data.email)) {
errors.email = 'Email is invalid'
}
if (Validator.isEmpty(data.subject)) {
errors.subject = 'Subject is required'
}
if (Validator.isEmpty(data.message)) {
errors.message = 'Message is required'
}
return {
errors: errors,
isValid: isEmpty(errors)
}
}