From 6fb8e554333916d0a190334dc78140b3e047b8a7 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Mon, 27 May 2019 19:42:38 +0530 Subject: [PATCH] Feedbacks --- lib/mongodb.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 79e50d89a..729312c32 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -18,7 +18,7 @@ const Connector = require('loopback-connector').Connector; const debug = require('debug')('loopback:connector:mongodb'); const Decimal128 = mongodb.Decimal128; -const ObjectIdRegex = /^[0-9a-fA-F]{24}$/; +const ObjectIdValueRegex = /^[0-9a-fA-F]{24}$/; const ObjectIdTypeRegex = /objectid/i; exports.ObjectID = ObjectID; @@ -38,7 +38,7 @@ function ObjectID(id) { // MongoDB's ObjectID constructor accepts number, 12-byte string or 24-byte // hex string. For LoopBack, we only allow 24-byte hex string, but 12-byte // string such as 'line-by-line' should be kept as string - if (ObjectIdRegex.test(id)) { + if (ObjectIdValueRegex.test(id)) { return bson.ObjectID(id); } else { return id; @@ -1873,15 +1873,15 @@ MongoDB.prototype.ping = function(cb) { } }; -// Determine if a property must have an ObjectID value -function mustBeObjectID(propDef) { +// Determine if a property must be stored as ObjectID +function isStoredAsObjectID(propDef) { if (!propDef) return false; if (propDef.mongodb && ObjectIdTypeRegex.test(propDef.mongodb.dataType)) return true; return false; } // Determine if strictObjectIDCoercion should be enabled -function enableStrictObjectIDCoercion(modelCtor, options) { +function isStrictObjectIDCoercionEnabled(modelCtor, options) { const settings = modelCtor.settings; return (settings && settings.strictObjectIDCoercion) || (modelCtor.model && modelCtor.model.getConnector().settings.strictObjectIDCoercion) || @@ -1890,17 +1890,17 @@ function enableStrictObjectIDCoercion(modelCtor, options) { } function coerceToObjectId(modelCtor, propDef, propValue) { - if (mustBeObjectID(propDef)) { + if (isStoredAsObjectID(propDef)) { if (isObjectIDProperty(modelCtor, propDef, propValue)) { return ObjectID(propValue); } else { throw new Error(`${propValue} is not an ObjectID string`); } - } else if (enableStrictObjectIDCoercion(modelCtor)) { + } else if (isStrictObjectIDCoercionEnabled(modelCtor)) { if (isObjectIDProperty(modelCtor, propDef, propValue)) { return ObjectID(propValue); } - } else if (ObjectIdRegex.test(propValue)) { + } else if (ObjectIdValueRegex.test(propValue)) { return ObjectID(propValue); } return propValue; @@ -1919,21 +1919,19 @@ function isObjectIDProperty(modelCtor, propDef, value, options) { return true; } else if ('string' === typeof value) { // dataType is specified as ObjectID - if (propDef && propDef.mongodb && ObjectIdTypeRegex.test(propDef.mongodb.dataType)) { - // value looks like an ObjectID, and it should be treated as one - // because `prop.mongodb.dataType` is set to 'ObjectID' - // this condition overrides the `strictObjectIDCoercion` setting - if (value.match(ObjectIdRegex)) return true; + if (isStoredAsObjectID(propDef)) { + // dataType looks like an ObjectID + if (value.match(ObjectIdValueRegex)) return true; // if dataType is specified as ObjectID, value is expected to an ObjectID-like string return false; - } else if (!value.match(ObjectIdRegex)) { + } else if (!value.match(ObjectIdValueRegex)) { // obviously not an ObjectID return false; } // value looks like an ObjectID, determine its nature based on `strictObjectIDCoercion` // coerce only when strictObjectIDCoercion is disabled - return !enableStrictObjectIDCoercion(modelCtor, options); + return !isStrictObjectIDCoercionEnabled(modelCtor, options); } else { return false; }