How to use the isEmpty function from validator

Find comprehensive JavaScript validator.isEmpty code examples handpicked from public code repositorys.

The validator.isEmpty function checks if a given value is an empty string, null or undefined.

64
65
66
67
68
69
70
71
72
73
var hasError = false;
_.each(checkRule, (rules, paramName) => {
	if (rules) {
		var value = params[paramName] != undefined ? params[paramName].toString() : '';
		_.each(rules.split(';'), (rule) => {
			if (rule === 'notEmpty' && validator.isEmpty(value)) {
				hasError = true;
				ctx.makeResObj(500, paramName + '#common.notEmpty#');
				return false;
			} else if (rule === 'number' && !validator.isFloat(value)) {
fork icon191
star icon728
watch icon0

410
411
412
413
414
415
416
417
418
419
}
if (!args[main][name]) {
  return (errors = `${capitalize(name)} is required`);
}
let value = args[main][name]
if (!args[main][name] || Validator.isEmpty(value.toString())) {
  return (errors = `${capitalize(name)} field is required`)
}

if (name === "email" && !Validator.isEmail(args[main][name])) {
fork icon0
star icon2
watch icon3

+ 13 other calls in file

How does validator.isEmpty work?

validator.isEmpty is a function that checks whether a given value is an empty string, an empty array, an empty object, or a falsy value, and returns true if it is, false otherwise. The function takes a single argument which is the value to be checked.

143
144
145
146
147
148
149
150
151
152

const password = req.body.password;
const cpassword = req.body.cnfpass;
const email = req.body.email;
const role = [req.body.role];
const empty = validator.isEmpty(password && cpassword);

if (password === cpassword && !empty) {
    if (role) {
        const eg = await Role.find({ name: { $in: req.body.role } });
fork icon0
star icon1
watch icon1

+ 7 other calls in file

73
74
75
76
77
78
79
80
81
82
  !validator.isLength(postInput.title, { min: 5 })
) {
  errors.push({ message: 'Title is invalid.' });
}
if (
  validator.isEmpty(postInput.content) ||
  !validator.isLength(postInput.content, { min: 5 })
) {
  errors.push({ message: 'Content is invalid.' });
}
fork icon0
star icon0
watch icon1

+ 99 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const validator = require("validator");

const str = "";
if (validator.isEmpty(str)) {
  console.log("The string is empty");
} else {
  console.log("The string is not empty");
}

In this example, validator.isEmpty is used to check whether the str variable is an empty string or not. If it is empty, the code will log "The string is empty", otherwise it will log "The string is not empty".

39
40
41
42
43
44
45
46
47
48
}

  // Validating the request for lastName
  if (
    typeof req.body.lastName !== "string" ||
    validator.isEmpty(req.body.lastName) ||
    validator.isWhitelisted(req.body.lastName, " ")
  ) {
    return  responseGenerator(res,400,false,"lastName is required as String and must not be Empty",[]);
  }
fork icon0
star icon0
watch icon1

+ 71 other calls in file

37
38
39
40
41
42
43
44
45
46
47
//   }


  // Validating the request for title
  if (
    typeof req.body.title !== "string" ||
    validator.isEmpty(req.body.title) ||
    validator.isWhitelisted(req.body.title, " ")
  ) {
    return  responseGenerator(res,400,false,"Title is required and must not be Empty",[]);
  }
fork icon0
star icon0
watch icon1

+ 71 other calls in file

84
85
86
87
88
89
90
91
92
93
94
95
  validator[key] = extend;
});


// map isNull to isEmpty
// https://github.com/chriso/validator.js/commit/e33d38a26ee2f9666b319adb67c7fc0d3dea7125
validator.isNull = validator.isEmpty;


// isDate removed in 7.0.0
// https://github.com/chriso/validator.js/commit/095509fc707a4dc0e99f85131df1176ad6389fc9
validator.isDate = function(dateString) {
fork icon0
star icon0
watch icon1

+ 11 other calls in file

10
11
12
13
14
15
16
17
18
19
const errors = [];
if (!validator.isEmail(email)) {
  errors.push({ message: "E-mail is invalid" });
}
if (
  validator.isEmpty(password) ||
  !validator.isLength(password, { min: 5 })
) {
  errors.push({ message: "Password too short" });
}
fork icon0
star icon0
watch icon1

+ 26 other calls in file

64
65
66
67
68
69
70
71
72
73

createEvent : [tokenValidator,  (req, res) => {

   if(validator.isEmpty(req.body.eventName) ||
   !validator.isBoolean(req.body.eventOrWorkshop) ||
   validator.isEmpty(req.body.description) ||
   !validator.isEmail(req.body.eventManagerEmail) ||
   validator.isEmpty(req.body.date) ||
   validator.isEmpty(req.body.eventTime) ||
   validator.isEmpty(req.body.venue) ||
fork icon0
star icon0
watch icon1

+ 11 other calls in file

32
33
34
35
36
37
38
39
40
41
const vOption = {
  ignore_whitespace: false
};
let errors = [];

// if (requestBody.UserId === undefined || V.isEmpty(requestBody.UserId + '', vOption)) {
//   errors.push({
//     attr: 'UserId',
//     msg: 'attribute is required.'
//   });
fork icon0
star icon0
watch icon1

+ 3 other calls in file

32
33
34
35
36
37
38
39
40
41
const vOption = {
  ignore_whitespace: false
};
let errors = [];

if (requestBody.userId === undefined || V.isEmpty(requestBody.userId + '', vOption)) {
  errors.push({
    attr: 'userId',
    msg: 'attribute is required.'
  });
fork icon0
star icon0
watch icon1

+ 5 other calls in file

170
171
172
173
174
175
176
177
178
179
 */
if (user_role === 'sales' && type === 'sales') {
    throw new Error(`Sales representative can not create 'sales' users`);
}

if (full_name === undefined || V.isEmpty(full_name + '', {ignore_whitespace: false})) {
    throw new Error('Full name is required.');
}

if (email) {
fork icon0
star icon0
watch icon1

39
40
41
42
43
44
45
46
47
48
    attr: 'Sender',
    msg: 'attribute is required.'
  });
}

if (requestBody.Source === undefined || V.isEmpty(requestBody.Source + '', vOption)) {
  errors.push({
    attr: 'Source',
    msg: 'attribute is required.'
  });
fork icon0
star icon0
watch icon1

+ 4 other calls in file

50
51
52
53
54
55
56
57
58
}
if (!validator.isAscii(name) || validator.isEmpty(name)) {
  req.flash('nameError', 'Names contains just letters!')
  isSomeError = true
}
if (!validator.isNumeric(points) || validator.isEmpty(points)) {
  req.flash('pointsError', 'Just numbers please!')
  isSomeError = true
}
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
22
        errors[fieldName] = "Please use valid email";
    }
};


exports.vEmpty = (fieldName, errors, value)=> {
    if(!value || validator.isEmpty(value)) {
        errors[fieldName] = "This field is required";
    }
};

fork icon0
star icon0
watch icon0

77
78
79
80
81
82
83
84
85
86

if (validator.isEmpty(nom) || nom === '{nom}' || nom === 'undefined' ||
    validator.isEmpty(email) || email === '{email}' || email === 'undefined' ||
    validator.isEmpty(telephone) || telephone === '{telephone}' || telephone === 'undefined' ||
    validator.isEmpty(site_web) || site_web === '{site_web}' || site_web === 'undefined' ||
    validator.isEmpty(passwd) || passwd === '{passwd}' || passwd === 'undefined' ||
    id_type === '{id_type}' || id_type === 'undefined') {
    return res.status(400).send({success:0,data: "Bad request, veuillez remplir tous les champs."});

} else if (validator.isAscii(nom) && validator.isEmail(email) && validator.isAscii(telephone)
fork icon0
star icon0
watch icon0

+ 17 other calls in file

125
126
127
128
129
130
131
132
133
134
id_age = typeof id_age === 'undefined' ? "" : id_age;
id_sexe = typeof id_sexe === 'undefined' ? "" : id_sexe;
id_pays = typeof id_pays === 'undefined' ? "" : id_pays;

if (validator.isEmpty(idPublic) || idPublic === '{idPublic}' || idPublic === 'undefined' ||
    validator.isEmpty(prenom) || prenom === '{prenom}' || prenom === 'undefined' ||
    validator.isEmpty(nom) || nom === '{nom}' || nom === 'undefined' ||
    validator.isEmpty(email) || email === '{email}' || email === 'undefined' ||
    validator.isEmpty(passwd) || passwd === '{passwd}' || passwd === 'undefined' ||
    id_langue === '{id_langue}' || id_langue === 'undefined' ||
fork icon0
star icon0
watch icon0

+ 13 other calls in file

27
28
29
30
31
32
33
34
35
36
const phone_number2 = req.body.phon_num2;

if (
  validator.isEmpty(username) ||
  validator.isEmpty(password1) ||
  validator.isEmpty(password2) ||
  validator.isEmpty(line1) ||
  validator.isEmpty(line2)||
  validator.isEmpty(Town) ||
  validator.isEmpty(district) ||
fork icon0
star icon0
watch icon0

+ 35 other calls in file

31
32
33
34
35
36
37
38
39
40
const errors = [];
if (!validator.isEmail(userSignUpInput.email)) {
  errors.push({ message: "E-mail is invalid." });
}
if (
  validator.isEmpty(userSignUpInput.password) ||
  !validator.isStrongPassword(userSignUpInput.password, {
    minLength: 8,
    minLowercase: 1,
    minUppercase: 1,
fork icon0
star icon0
watch icon0