Skip to content

Commit

Permalink
fix(document): handle required when schema has property named `isSele…
Browse files Browse the repository at this point in the history
…cted`

Fix #9438
  • Loading branch information
vkarpov15 committed Sep 23, 2020
1 parent 1b2202d commit b128c9b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const isMongooseObject = utils.isMongooseObject;
const arrayAtomicsBackupSymbol = Symbol('mongoose.Array#atomicsBackup');
const arrayAtomicsSymbol = require('./helpers/symbols').arrayAtomicsSymbol;
const documentArrayParent = require('./helpers/symbols').documentArrayParent;
const documentIsSelected = require('./helpers/symbols').documentIsSelected;
const documentIsModified = require('./helpers/symbols').documentIsModified;
const documentModifiedPaths = require('./helpers/symbols').documentModifiedPaths;
const documentSchemaSymbol = require('./helpers/symbols').documentSchemaSymbol;
const getSymbol = require('./helpers/symbols').getSymbol;
const populateModelSymbol = require('./helpers/symbols').populateModelSymbol;
Expand Down Expand Up @@ -1844,6 +1847,8 @@ Document.prototype.modifiedPaths = function(options) {
}, []);
};

Document.prototype[documentModifiedPaths] = Document.prototype.modifiedPaths;

/**
* Returns true if this document was modified, else false.
*
Expand All @@ -1868,7 +1873,7 @@ Document.prototype.isModified = function(paths, modifiedPaths) {
if (!Array.isArray(paths)) {
paths = paths.split(' ');
}
const modified = modifiedPaths || this.modifiedPaths();
const modified = modifiedPaths || this[documentModifiedPaths]();
const directModifiedPaths = Object.keys(this.$__.activePaths.states.modify);
const isModifiedChild = paths.some(function(path) {
return !!~modified.indexOf(path);
Expand All @@ -1884,6 +1889,8 @@ Document.prototype.isModified = function(paths, modifiedPaths) {
return this.$__.activePaths.some('modify');
};

Document.prototype[documentIsModified] = Document.prototype.isModified;

/**
* Checks if a path is set to its default.
*
Expand Down Expand Up @@ -2040,6 +2047,8 @@ Document.prototype.isSelected = function isSelected(path) {
return true;
};

Document.prototype[documentIsSelected] = Document.prototype.isSelected;

/**
* Checks if `path` was explicitly selected. If no projection, always returns
* true.
Expand Down
3 changes: 3 additions & 0 deletions lib/helpers/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ exports.arrayParentSymbol = Symbol('mongoose#Array#_parent');
exports.arrayPathSymbol = Symbol('mongoose#Array#_path');
exports.arraySchemaSymbol = Symbol('mongoose#Array#_schema');
exports.documentArrayParent = Symbol('mongoose:documentArrayParent');
exports.documentIsSelected = Symbol('mongoose#Document#isSelected');
exports.documentIsModified = Symbol('mongoose#Document#isModified');
exports.documentModifiedPaths = Symbol('mongoose#Document#modifiedPaths');
exports.documentSchemaSymbol = Symbol('mongoose#Document#schema');
exports.getSymbol = Symbol('mongoose#Document#get');
exports.modelSymbol = Symbol('mongoose#Model');
Expand Down
5 changes: 4 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const util = require('util');
const utils = require('./utils');
const validatorErrorSymbol = require('./helpers/symbols').validatorErrorSymbol;

const documentIsSelected = require('./helpers/symbols').documentIsSelected;
const documentIsModified = require('./helpers/symbols').documentIsModified;

const CastError = MongooseError.CastError;
const ValidatorError = MongooseError.ValidatorError;

Expand Down Expand Up @@ -915,7 +918,7 @@ SchemaType.prototype.required = function(required, message) {
const cachedRequired = get(this, '$__.cachedRequired');

// no validation when this path wasn't selected in the query.
if (cachedRequired != null && !this.isSelected(_this.path) && !this.isModified(_this.path)) {
if (cachedRequired != null && !this[documentIsSelected](_this.path) && !this[documentIsModified](_this.path)) {

This comment has been minimized.

Copy link
@AbdelrahmanHafez

AbdelrahmanHafez Sep 23, 2020

Collaborator

Nice 👍

return true;
}

Expand Down

0 comments on commit b128c9b

Please sign in to comment.