How to use the isMongoId function from validator

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

338
339
340
341
342
343
344
345
346
347
    //  check if value is a jwt
    else if (type === 'jwt')
        return validator.isJWT(value ?? "")
    // check if value is a valid mongodb id
    else if (type === 'mongoid') {
        return validator.isMongoId(String(value ?? ""))
    }
    else
        return false
}
fork icon1
star icon4
watch icon1

+ 6 other calls in file

164
165
166
167
168
169
170
171
172
173

const limit = data.limit && !isNaN(parseInt(data.limit)) && parseInt(data.limit) > 0 && parseInt(data.limit) < MAX_DOCUMENT_COUNT_PER_QUERY ? parseInt(data.limit) : DEFAULT_DOCUMENT_COUNT_PER_QUERY;
const page = data.page && !isNaN(parseInt(data.page)) && parseInt(data.page) > 0 ? parseInt(data.page) : 0;
const skip = page * limit;

if (data.book_id && validator.isMongoId(data.book_id.toString()))
  filters.book_id == mongoose.Types.ObjectId(data.book_id.toString());

if ('is_root' in data)
  filters.parent_id = data.is_root ? null : { $ne: null };
fork icon0
star icon2
watch icon0

209
210
211
212
213
214
215
216
217
218
  }
  return true;
},
isMongoId: function(rule, value) {
  if (rule) {
    return validator.isMongoId(value);
  }
  return true;
},
isLength: function(rule, value) {
fork icon0
star icon2
watch icon2

+ 5 other calls in file

7
8
9
10
11
12
13
14
15
16
17
18
    return new ObjectId(this.toString())
}


// eslint-disable-next-line no-extend-native
String.prototype.isObjectId = function () {
    return validator.isMongoId(this)
}


String.prototype.isEmail = function () {
    return validator.isEmail(this)
fork icon2
star icon0
watch icon2

13
14
15
16
17
18
19
20
21
22
23
// eslint-disable-next-line no-extend-native
const isObjectId = function (key) {
    if (typeof key != "string") {
        throw new Error("key is not a string")
    }
    return validator.isMongoId(key)
}


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