How to use the isUUID function from validator

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

validator.isUUID is a function that checks whether a given string is a valid UUID (universally unique identifier).

14
15
16
17
18
19
20
21
22
23

console.log('4716368472599727\n', 'isCreditCard', validator.isCreditCard('4716368472599727'));
console.log('4321\n', 'isCreditCard', validator.isCreditCard('4321'));


console.log('6b3a2b30-f2ee-11e4-b939-0800200c9a66\n', 'isUUID', validator.isUUID('6b3a2b30-f2ee-11e4-b939-0800200c9a66'));
console.log('6b3a2b30-f2ee-11e4-b939\n', 'isUUID', validator.isUUID('6b3a2b30-f2ee-11e4-b939'));


console.log('YXNkYXNkc2R2YWFzbG5rc2FkbnZsc2EnZHY=\n', 'isBase64', validator.isBase64('YXNkYXNkc2R2YWFzbG5rc2FkbnZsc2EnZHY='));
fork icon15
star icon3
watch icon4

+ 3 other calls in file

87
88
89
90
91
92
93
94
95
96
97
98
99
100


const validHexadecimal = url => validator.isHexadecimal(url);


const validHexaColor = url => validator.isHexColor(url);


const validUUID = url => validator.isUUID(url);


// unsafe validators
const wrongValidation = url => validator.isByteLength(url, {min:4,max:8});

fork icon5
star icon63
watch icon0

How does validator.isUUID work?

validator.isUUID is a method provided by the validator package in Node.js, which is used to check if the given string value is a valid UUID (Universally Unique Identifier) or not. The method checks the input value against a regular expression pattern for UUIDs, and returns true if the value matches the pattern, and false otherwise. The UUID pattern can be either a version 4 UUID or a version 1 UUID.

218
219
220
221
222
223
224
225
226
227
    return false;
  }
};

/**
 * Validate UUID value. validator.isUUID(str [, version]) - check if the string is a UUID (version 3, 4 or 5).
 * @param uuid
 * @returns {*}
 */
var isUuid = module.exports.isUuid = function isUuid(uuid) {
fork icon2
star icon6
watch icon21

+ 3 other calls in file

102
103
104
105
106
107
108
109
110
111
},
ipv6: function(x) {
  check(x, "an IPv6 address", validator.isIP(x, 6));
},
uuid: function(x) {
  check(x, "a UUID", validator.isUUID(x));
},
uuid3: function(x) {
  check(x, "a version 3 UUID", validator.isUUID(x, 3));
},
fork icon2
star icon4
watch icon0

+ 3 other calls in file

Ai Example

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

const uuid = "c5d5db40-97b7-11eb-a8b3-0242ac130003";

if (validator.isUUID(uuid)) {
  console.log(`${uuid} is a valid UUID`);
} else {
  console.log(`${uuid} is not a valid UUID`);
}

In this example, we first import the validator module, which provides the isUUID function. We then create a uuid string and pass it to the isUUID function. If the string is a valid UUID, the function returns true, so we output a message indicating that the UUID is valid. If the string is not a valid UUID, the function returns false, so we output a message indicating that the UUID is not valid.

122
123
124
125
126
127
128
129
130
131
  }
  return true;
},
isUUID: function(rule, value) {
  if (rule) {
    return validator.isUUID(value);
  }
  return true;
},
isDate: function(rule, value) {
fork icon0
star icon2
watch icon2

+ 5 other calls in file

120
121
122
123
124
125
126
127
128
129
isURL: function (str, options) {
  return validator.isURL(str, typeof options === 'object' ? options : undefined);
},
// isUUID(str [, version]) - check if the string is a UUID (version 3, 4 or 5).
isUUID: function (str, version) {
  return validator.isUUID(str, typeof version !== 'boolean' ? version : undefined);
},
// isUppercase(str) - check if the string is uppercase.
isUppercase: validator.isUppercase,
// isVariableWidth(str) - check if the string contains a mixture of full and
fork icon0
star icon1
watch icon3

+ 17 other calls in file

58
59
60
61
62
63
64
65
66
67
'ip'                    : validator.isIP,
'ipv4'          : validator.isIPv4,
'ipv6'          : validator.isIPv6,
'creditcard': validator.isCreditCard,
'uuid'          : validator.isUUID,
'uuidv3'        : function (x){ return validator.isUUID(x, 3);},
'uuidv4'        : function (x){ return validator.isUUID(x, 4);},

'int'                   : validator.isInt,
'integer'       : validator.isInt,
fork icon59
star icon0
watch icon7

+ 3 other calls in file

49
50
51
52
53
54
55
56
57
58
59
60


const isUUID = function (key) {
    if (typeof key != "string") {
        throw new Error("key is not a string")
    }
    return validator.isUUID(key)
}


const toTitleCase = function (key) {
    if (typeof key != "string") {
fork icon2
star icon0
watch icon2

31
32
33
34
35
36
37
38
39
40
41
42
        code.match(/^(\+\d{1,3}[- ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/) ||
        code.match(/^(\+\d{1,3}[- ]?)?\(?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/)
}


String.prototype.isUUID = function () {
    return validator.isUUID(this)
}


String.prototype.toTitleCase = function () {
    const str = this
fork icon2
star icon0
watch icon2

13
14
15
16
17
18
19
20
21
22
23
24
25
const cache = require('../cache');


const Thumbs = module.exports;


Thumbs.exists = async function (id, path) {
    const isDraft = validator.isUUID(String(id));
    const set = `${isDraft ? 'draft' : 'topic'}:${id}:thumbs`;


    return db.isSortedSetMember(set, path);
};
fork icon0
star icon0
watch icon1

+ 11 other calls in file

59
60
61
62
63
64
65
66
67
68
for (let j = 0; j < day.exercises.length; j++) {
  const exercise = day.exercises[j];

  await validateValue(exercise.exerciseId, [
    {
      validator: (value) => isUUID(value),
      message: 'Поле exerciseId упражнения должно быть UUID',
    },
  ]);
}
fork icon0
star icon0
watch icon1

344
345
346
347
348
349
350
351
352
353
},
'uuid': {
  method: 'isUUID',
  message: 'UUID string (version 3, 4 or 5)'
},
'uuidv3': { method: (val) => V.isUUID(val, 3), message: 'UUID version 3 string' },
'uuidv4': { method: (val) => V.isUUID(val, 4), message: 'UUID version 4 string' },
'uuidv5': { method: (val) => V.isUUID(val, 5), message: 'UUID version 5 string' },
'uppercase': {
  method: 'isUppercase',
fork icon0
star icon0
watch icon0

+ 2 other calls in file

61
62
63
64
65
66
67
68
69
70
'ip'                  : validator.isIP,
'ipv4'                : validator.isIPv4,
'ipv6'                : validator.isIPv6,
'creditcard': validator.isCreditCard,
'uuid'                : validator.isUUID,
'uuidv3'      : function (x){ return validator.isUUID(x, 3);},
'uuidv4'      : function (x){ return validator.isUUID(x, 4);},

'int'                 : validator.isInt,
'integer'     : validator.isInt,
fork icon0
star icon0
watch icon12

+ 7 other calls in file