This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -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) {