Cleanup
This commit is contained in:
69
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
69
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
@@ -7,11 +7,10 @@
|
||||
const SchemaObjectIdOptions = require('../options/SchemaObjectIdOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const castObjectId = require('../cast/objectid');
|
||||
const getConstructorName = require('../helpers/getConstructorName');
|
||||
const oid = require('../types/objectid');
|
||||
const utils = require('../utils');
|
||||
|
||||
const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
let Document;
|
||||
|
||||
@@ -151,18 +150,24 @@ ObjectId.cast = function cast(caster) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (!(v instanceof oid)) {
|
||||
throw new Error(v + ' is not an instance of ObjectId');
|
||||
}
|
||||
return v;
|
||||
};
|
||||
caster = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
ObjectId._defaultCaster = v => {
|
||||
if (!(v instanceof oid)) {
|
||||
throw new Error(v + ' is not an instance of ObjectId');
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
@@ -219,50 +224,24 @@ ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
ObjectId.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (value instanceof oid) {
|
||||
return value;
|
||||
} else if ((value.constructor.name || '').toLowerCase() === 'objectid') {
|
||||
} else if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
|
||||
return new oid(value.toHexString());
|
||||
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
||||
throw new CastError('ObjectId', value, this.path, null, this);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
let ret = value;
|
||||
if (!doc.$__.populated ||
|
||||
!doc.$__.populated[path] ||
|
||||
!doc.$__.populated[path].options ||
|
||||
!doc.$__.populated[path].options.options ||
|
||||
!doc.$__.populated[path].options.options.lean) {
|
||||
ret = new pop.options[populateModelSymbol](value);
|
||||
ret.$__.wasPopulated = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
|
||||
let castObjectId;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castObjectId = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castObjectId = this.constructor.cast();
|
||||
} else {
|
||||
castObjectId = ObjectId.cast();
|
||||
}
|
||||
|
||||
const castObjectId = typeof this.constructor.cast === 'function' ?
|
||||
this.constructor.cast() :
|
||||
ObjectId.cast();
|
||||
try {
|
||||
return castObjectId(value);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user