Skip to content

Commit

Permalink
Feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hage Yaapa committed May 30, 2019
1 parent e6d504b commit 6fb8e55
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/mongodb.js
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) ||
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 6fb8e55

Please sign in to comment.