How to use the toBoolean function from validator

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

validator.toBoolean is a function in Node.js that converts a string value to a boolean.

25
26
27
28
29
30
31
32
33
34
},
jwt: {
	enable: validator.toBoolean(process.env.STEEDOS_IDENTITY_JWT_ENABLED || 'false', true),
},
unpkg:{
	enable: validator.toBoolean(process.env.STEEDOS_UNPKG_ENABLE_LOCAL || 'false', true)
},
saas: {
	enable: validator.toBoolean(process.env.STEEDOS_TENANT_ENABLE_SAAS || 'false', true)
},
fork icon307
star icon999
watch icon0

+ 3 other calls in file

40
41
42
43
44
45
46
47
48
49
module.exports = {
	name: packageName,
	namespace: "steedos",
	settings: {
		STEEDOS_HELP_URL: process.env.STEEDOS_HELP_URL ? process.env.STEEDOS_HELP_URL : 'https://www.steedos.com',
		STEEDOS_TENANT_ENABLE_ACTIVATION: validator.toBoolean(process.env.STEEDOS_TENANT_ENABLE_ACTIVATION || 'false', true),
		// 通过云端中控初始化本地服务
		STEEDOS_CLOUD_URL: process.env.STEEDOS_CLOUD_URL ? process.env.STEEDOS_CLOUD_URL : 'https://console.steedos.cn',
		STEEDOS_CLOUD_SPACE_ID: process.env.STEEDOS_CLOUD_SPACE_ID,
		STEEDOS_CLOUD_API_KEY: process.env.STEEDOS_CLOUD_API_KEY,
fork icon307
star icon999
watch icon0

How does validator.toBoolean work?

validator.toBoolean() is a function in the validator library for Node.js that converts a string representation of a boolean value to a boolean data type in JavaScript. If the input is not a string representation of a boolean value, it returns null. The function considers the following string values as true: 'true', 't', 'yes', 'y', '1' and the following string values as false: 'false', 'f', 'no', 'n', '0'.

1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
 */
Chain.prototype.toBooleanStrict = function() {
  this._pushValidator({
    name: 'toBooleanStrict',
    func: function(value, baton, callback) {
      callback(null, check.toBoolean(value, true));
    },
    help: null
  });
  return this;
fork icon11
star icon96
watch icon0

43
44
45
46
47
48
49
50
51
52
base64: {
  method: 'isBase64'
},
boolean: {
  method: 'isBoolean',
  sanitizer: V.toBoolean
},
country2: {
  method: 'isISO31661Alpha2'
},
fork icon10
star icon11
watch icon2

+ 10 other calls in file

Ai Example

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

console.log(validator.toBoolean("true")); // true
console.log(validator.toBoolean("yes")); // true
console.log(validator.toBoolean("1")); // true

console.log(validator.toBoolean("false")); // false
console.log(validator.toBoolean("no")); // false
console.log(validator.toBoolean("0")); // false

1
2
3
4
5
6
7
8
9
10
11
12
const { _ } = require('@genx/july');


exports.toBoolean = (value) =>
    typeof value === 'boolean'
        ? value
        : validator.toBoolean(value.toString(), true);


exports.toText = (value, noTrim) => {
    if (value) {
        value = typeof value !== 'string' ? value.toString() : value;
fork icon2
star icon2
watch icon0

20
21
22
23
24
25
26
27
28
29
    result = validator.stripLow(xss(sanitize(value)));
    break;

case 'boolean':
    // noinspection JSUnresolvedFunction
    result = validator.toBoolean(value);
    break;

case 'string':
case 'reference':
fork icon1
star icon13
watch icon0

28
29
30
31
32
33
34
35
36
37
}
if (sanitization.unescape) {
  sanitizefield = Validator.unescape(sanitizefield);
}
if (sanitization.toBoolean) {
  sanitizefield = Validator.toBoolean(sanitizefield);
}
if (sanitization.toInt) {
  sanitizefield = Validator.toInt(sanitizefield);
}
fork icon0
star icon3
watch icon0

471
472
473
474
475
476
477
478
479
480

if (cType == 'boolean') {
    // Проверка
    if (!['1', 'true', '0', 'false'].includes(paramValStr)) throw new Error('E_IncorrectParams', null, refId);
    // Приведение
    retParam = validator.toBoolean(paramValStr, true);
    paramChecked = true;
}

if (cType == 'phone') {
fork icon0
star icon2
watch icon0

38
39
40
41
42
43
44
45
46
47
	},
	STEEDOS_CLOUD_URL: process.env.STEEDOS_CLOUD_URL ? process.env.STEEDOS_CLOUD_URL : 'https://console.steedos.cn',
	STEEDOS_CLOUD_SPACE_ID: process.env.STEEDOS_CLOUD_SPACE_ID,
	STEEDOS_CLOUD_API_KEY: process.env.STEEDOS_CLOUD_API_KEY,
	STEEDOS_REGISTRY_URL: process.env.STEEDOS_REGISTRY_URL ? process.env.STEEDOS_REGISTRY_URL : 'https://registry.steedos.cn/',
	STEEDOS_CLOUD_SYNC_PACKAGES: validator.toBoolean(process.env.STEEDOS_CLOUD_SYNC_PACKAGES || 'false', true),
	STEEDOS_INITIAL_PACKAGES: process.env.STEEDOS_INITIAL_PACKAGES
},

/**
fork icon0
star icon0
watch icon1

+ 3 other calls in file

37
38
39
40
41
42
43
44
45
		path: path.join(__dirname, 'main'),
		name: packageName,
		isPackage: false
	},
	initBuilderRouter: false,
	QUERY_RESULTS_CLEANUP_ENABLED: process.env.STEEDOS_QUERY_RESULTS_CLEANUP_ENABLED ? validator.toBoolean(process.env.STEEDOS_QUERY_RESULTS_CLEANUP_ENABLED || 'true', true) : true,
	QUERY_RESULTS_CLEANUP_COUNT: process.env.STEEDOS_QUERY_RESULTS_CLEANUP_COUNT || 100,
	QUERY_RESULTS_CLEANUP_MAX_AGE: process.env.STEEDOS_QUERY_RESULTS_CLEANUP_MAX_AGE || 7
},
fork icon0
star icon0
watch icon1

215
216
217
218
219
220
221
222
223
224
}
if (sanitization.rtrim) {
  req.body[field] = Validator.rtrim(req.body[field]);
}
if (sanitization.toBoolean) {
  req.body[field] = Validator.toBoolean(req.body[field]);
}
if (sanitization.toDate) {
  req.body[field] = Validator.toDate(req.body[field]);
}
fork icon0
star icon0
watch icon0

+ 11 other calls in file

139
140
141
142
143
144
145
146
147
148
149
 * @param {Boolean=} restrict - 如果是false,除了0,'0','false',false,或'',其餘都返回true。預設true。
 * @returns {Boolean} result
*/
vcheck.boolean = (str, restrict = true) => {
  if (str === undefined) return false;
  return validator.toBoolean(str.toString(), restrict);
};


/**
 * 轉換成json,轉換出來的物件會是原物件的deep copy。失敗的話會回傳null。
fork icon0
star icon0
watch icon0

35
36
37
38
39
40
41
42
43
44
const user = new User({
  name: req.body.name,
  email: req.body.email,
  phone: req.body.phone,
  address: req.body.address,
  isVerify: validator.toBoolean(req.body.isVerify),
  verificationToken: req.body.verificationToken,
  deviceToken: [req.body.deviceToken],
  password: hashedPassword,
});
fork icon0
star icon0
watch icon0

22
23
24
25
26
27
28
29
30
31
description: req.body.description,
quantity: req.body.quantity,
other: req.body.other,
pickupTimes: req.body.pickupTimes,
listDays: req.body.listDays,
isShared: validator.toBoolean(req.body.isShared),
location: {
  lan: req.body.location.lan,
  lon: req.body.location.lon,
},
fork icon0
star icon0
watch icon0

328
329
330
331
332
333
334
335
336
337
    this.parsedValue = value;
    return validator1.isFloat(String(value), ...this.options);
  }
case 'isBoolean':
  if (typeof value === 'string') {
    this.parsedValue = validator1.toBoolean(value);
    return validator1.isBoolean(value);
  } else {
    this.parsedValue = value;
    return validator1.isBoolean(String(value));
fork icon0
star icon0
watch icon0

+ 3 other calls in file

96
97
98
99
100
101
102
103
104
105
/**
 * 字符串转boolean
 * @param input 输入字符串
 */
toBoolean(input) {
  return validator1.toBoolean(input);
}

/**
 * 字符串转Date
fork icon0
star icon0
watch icon0

357
358
359
360
361
362
363
364
365
366

// -----------------------

converters: {
  'boolean': {
    converter: (val) => V.toBoolean('' + val)
  },
  'date': {
    converter: V.toDate
  },
fork icon0
star icon0
watch icon0