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

@@ -18,6 +18,7 @@ const util = require('util');
const utils = require('../utils');
const getConstructor = require('../helpers/discriminator/getConstructor');
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
const documentArrayParent = require('../helpers/symbols').documentArrayParent;
@@ -251,6 +252,11 @@ DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
}
if (options != null && options.validateModifiedOnly && !doc.isModified()) {
--count || fn(error);
continue;
}
doc.$__validate(callback);
}
}
@@ -267,7 +273,7 @@ DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
* @api private
*/
DocumentArrayPath.prototype.doValidateSync = function(array, scope) {
DocumentArrayPath.prototype.doValidateSync = function(array, scope, options) {
const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
if (schemaTypeError != null) {
schemaTypeError.$isArrayValidatorError = true;
@@ -299,6 +305,10 @@ DocumentArrayPath.prototype.doValidateSync = function(array, scope) {
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
}
if (options != null && options.validateModifiedOnly && !doc.isModified()) {
continue;
}
const subdocValidateError = doc.validateSync();
if (subdocValidateError && resultError == null) {
@@ -361,6 +371,11 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
// lazy load
MongooseDocumentArray || (MongooseDocumentArray = require('../types/documentarray'));
// Skip casting if `value` is the same as the previous value, no need to cast. See gh-9266
if (value != null && value[arrayPathSymbol] != null && value === prev) {
return value;
}
let selected;
let subdoc;
const _opts = { transform: false, virtuals: false };
@@ -387,11 +402,16 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
value = new MongooseDocumentArray(value, this.path, doc);
}
if (options.arrayPath != null) {
value[arrayPathSymbol] = options.arrayPath;
if (prev != null) {
value[arrayAtomicsSymbol] = prev[arrayAtomicsSymbol] || {};
}
if (options.arrayPathIndex != null) {
value[arrayPathSymbol] = this.path + '.' + options.arrayPathIndex;
}
const len = value.length;
const initDocumentOptions = { skipId: true, willInit: true };
for (let i = 0; i < len; ++i) {
if (!value[i]) {
@@ -424,7 +444,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
selected = true;
}
subdoc = new Constructor(null, value, true, selected, i);
subdoc = new Constructor(null, value, initDocumentOptions, selected, i);
value[i] = subdoc.init(value[i]);
} else {
if (prev && typeof prev.id === 'function') {
@@ -473,6 +493,14 @@ DocumentArrayPath.prototype.clone = function() {
return schematype;
};
/*!
* ignore
*/
DocumentArrayPath.prototype.applyGetters = function(value, scope) {
return SchemaType.prototype.applyGetters.call(this, value, scope);
};
/*!
* Scopes paths selected in a query to this array.
* Necessary for proper default application of subdocument values.
@@ -513,6 +541,26 @@ function scopePaths(array, fields, init) {
return hasKeys && selected || undefined;
}
/**
* Sets a default option for all DocumentArray instances.
*
* ####Example:
*
* // Make all numbers have option `min` equal to 0.
* mongoose.Schema.DocumentArray.set('_id', false);
*
* @param {String} option - The option you'd like to set the value for
* @param {*} value - value for option
* @return {undefined}
* @function set
* @static
* @api public
*/
DocumentArrayPath.defaultOptions = {};
DocumentArrayPath.set = SchemaType.set;
/*!
* Module exports.
*/