How to use the toFloat function from validator

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

1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
 */
Chain.prototype.toFloat = function() {
  this._pushValidator({
    name: 'toFloat',
    func: function(value, baton, callback) {
      callback(null, check.toFloat(value));
    },
    help: null
  });
  return this;
fork icon11
star icon96
watch icon0

3
4
5
6
7
8
9
10
11
12

const moment = require('moment-timezone');

const { ApiError } = require('./better-ctx-throw');

validator.extend('min', (str, min) => validator.toFloat(str) >= min);
validator.extend('max', (str, max) => validator.toFloat(str) <= max);

validator.extend('gt', (val, compare) => val > compare);
validator.extend('gte', (val, compare) => val >= compare);
fork icon5
star icon7
watch icon2

+ 11 other calls in file

10
11
12
13
14
15
16
17
18
19


switch (type) {
    case 'number':
        // noinspection JSUnresolvedFunction
        result = validator.toFloat(value);
        break;

    case 'date':
        // noinspection JSUnresolvedFunction,JSValidateTypes
fork icon1
star icon13
watch icon0

160
161
162
163
164
165
166
167
168
169
validator.isFloat = function (str) {
    return str !== '' && float.test(str);
};

validator.isDivisibleBy = function (str, num) {
    return validator.toFloat(str) % validator.toInt(num) === 0;
};

validator.isNull = function (str) {
    return str.length === 0;
fork icon2
star icon3
watch icon4

34
35
36
37
38
39
40
41
42
43
}
if (sanitization.toInt) {
  sanitizefield = Validator.toInt(sanitizefield);
}
if (sanitization.toFloat) {
  sanitizefield = Validator.toFloat(sanitizefield);
}
if (sanitization.toDate) {
  sanitizefield = Validator.toDate(sanitizefield);
}
fork icon0
star icon3
watch icon0

16
17
18
19
20
21
22
23
24
25
26
27
28


exports.toInt = (value, radix) =>
    _.isInteger(value) ? value : parseInt(value, radix);


exports.toFloat = (value) =>
    _.isFinite(value) ? value : validator.toFloat(value);


exports.jsonToBase64 = (obj) =>
    Buffer.from(JSON.stringify(obj)).toString('base64');

fork icon2
star icon2
watch icon0

221
222
223
224
225
226
227
228
229
230
}
if (sanitization.toDate) {
  req.body[field] = Validator.toDate(req.body[field]);
}
if (sanitization.toFloat) {
  req.body[field] = Validator.toFloat(req.body[field]);
}
if (sanitization.toInt) {
  req.body[field] = Validator.toInt(req.body[field]);
}
fork icon0
star icon0
watch icon0

+ 11 other calls in file

320
321
322
323
324
325
326
327
328
329
    this.parsedValue = value;
    return validator1.isInt(String(value), ...this.options);
  }
case 'isFloat':
  if (typeof value === 'string') {
    this.parsedValue = validator1.toFloat(value);
    return validator1.isFloat(value, ...this.options);
  } else {
    this.parsedValue = value;
    return validator1.isFloat(String(value), ...this.options);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

88
89
90
91
92
93
94
95
96
97
/**
 * 字符串转float
 * @param input 输入字符串
 */
toFloat(input) {
  return validator1.toFloat(input);
}

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

405
406
407
408
409
410
411
412
413
414
},
'int': {
  converter: (val) => _.isString(val) ? V.toInt(val) : parseInt(val)
},
'float': {
  converter: (val) => _.isString(val) ? V.toFloat(val) : parseFloat(val)
},
'trim': {
  converter: V.trim
},
fork icon0
star icon0
watch icon0