Skip to content

Commit

Permalink
Merge branch 'master' into 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 1, 2022
2 parents 9c26d86 + 9f897ea commit cc6a697
Show file tree
Hide file tree
Showing 37 changed files with 241 additions and 176 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
6.1.9 / 2022-01-31
==================
* fix(query): respect nested schema strict as default when casting query filters #11291
* fix(mongoose): make isValidObjectId() consistent with isValid(), make `ObjectId()` casting more flexible #11209
* fix(setDefaultsOnInsert): ignore defaults underneath maps #11235
* fix(query): avoid setting nested schema properties that are stripped out by strictQuery to undefined in query filter #11291
* fix: check for all flags in a regex deepequal #11242 [Uzlopak](https://github.com/Uzlopak)
* fix: replace substr with substring #11278 [Uzlopak](https://github.com/Uzlopak)
* docs: port for documentation testing in CONTRIBUTING.md #11273 [Uzlopak](https://github.com/Uzlopak)

6.1.8 / 2022-01-24
==================
* fix(connection): stop leaking sessions in .transaction() #11259 #11256 [Uzlopak](https://github.com/Uzlopak)
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ If you have a question about Mongoose (not a bug report) please post it to eithe
reproduces your issue. Do **not** describe your issue in prose. **Show your code.**
- If the bug involves an error, please post the stack trace.
- Please post the version of Mongoose and MongoDB that you're using.
 - Please write bug reports in JavaScript (ES5, ES6, etc) that runs in Node.js, **not** CoffeeScript, TypeScript, JSX, etc.
- Please write bug reports in JavaScript (ES5, ES6, etc) that runs in Node.js, **not** CoffeeScript, TypeScript, JSX, etc.

### Requesting new features

Expand Down Expand Up @@ -57,7 +57,7 @@ If you'd like to preview your documentation changes, first commit your changes t
* `make gendocs`
* `node static.js`

Visit `http://localhost:8088` and you should see the docs with your local changes. Make sure you `git reset --hard` before committing, because changes to `docs/*` should **not** be in PRs.
Visit `http://localhost:8089` and you should see the docs with your local changes. Make sure you `git reset --hard` before committing, because changes to `docs/*` should **not** be in PRs.

### Plugins website

Expand Down
55 changes: 55 additions & 0 deletions docs/loadSponsorData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const config = require('../.config');
const fs = require('fs');
const mongoose = require('../');

run().catch(err => {
console.error(err);
process.exit(-1);
});

async function run() {
await mongoose.connect(config.uri);

const Subscriber = mongoose.model('Subscriber', mongoose.Schema({
companyName: String,
description: String,
url: String,
logo: String
}), 'Subscriber');

const Job = mongoose.model('Job', mongoose.Schema({
logo: String,
company: String,
title: String,
location: {
type: String,
required: true
},
description: {
type: String,
required: true
},
url: {
type: String,
required: true
}
}), 'Job');

try {
fs.mkdirSync(`${__dirname}/data`);
} catch(err) {}

const subscribers = await Subscriber.
find({ companyName: { $exists: true }, description: { $exists: true }, logo: { $exists: true } }).
sort({ createdAt: 1 }).
select({ companyName: 1, description: 1, url: 1, logo: 1 });
fs.writeFileSync(`${__dirname}/data/sponsors.json`, JSON.stringify(subscribers, null, ' '));

const jobs = await Job.find().select({ logo: 1, company: 1, title: 1, location: 1, description: 1, url: 1 });
fs.writeFileSync(`${__dirname}/data/jobs.json`, JSON.stringify(jobs, null, ' '));

console.log('Done');
process.exit(0);
}
8 changes: 5 additions & 3 deletions docs/source/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

'use strict';

const fs = require('fs');
const yaml = require('js-yaml');
let sponsors = [];
try {
sponsors = require('../data/sponsors.json');
} catch (err) {}

exports['index.pug'] = require('./home');
exports['docs/api.pug'] = require('./api');
Expand Down Expand Up @@ -48,6 +50,6 @@ exports['docs/search.pug'] = { title: 'Search' };
exports['docs/enterprise.md'] = { title: 'Mongoose for Enterprise', markdown: true };
exports['docs/sponsors.pug'] = {
title: 'Mongoose Sponsors',
sponsors: yaml.load(fs.readFileSync(`${__dirname}/../sponsor-data/sponsors.yml`, 'utf8'))
sponsors
};
exports['docs/async-await.md'] = { title: 'Using Async/Await with Mongoose', markdown: true };
20 changes: 0 additions & 20 deletions docs/sponsor-data/sponsors.yml

This file was deleted.

2 changes: 1 addition & 1 deletion docs/sponsors.pug
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ block content
div(style='position: relative; margin-top: 1em;')
div(style='position: absolute; width: 100px; height: 100px')
a(href=sponsor.url)
img(src=sponsor.logoSquare)
img(src=sponsor.logo)
div(style='padding-left: 110px; min-height: 100px')
h4(style='margin-top: 0.2em; margin-bottom: 0.25em;')
a(href=sponsor.url) #{sponsor.name}
Expand Down
3 changes: 3 additions & 0 deletions index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ html(lang='en')
<a rel="sponsored" href="https://casino.guide/crypto-casinos/">
<img class="sponsor" src="https://casino.guide/wp-content/themes/casinoguide/assets/lotti/en/default/images/img_0.png" style="height: 100px">
</a>
<a rel="sponsored" href="https://opencollective.com/seo-pmi">
<img class="sponsor" src="https://images.opencollective.com/seo-pmi/avatar/256.png" style="height: 100px">
</a>
</div>
</div>

Expand Down
24 changes: 19 additions & 5 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ module.exports = function cast(schema, obj, options, context) {
remainingConds = {};
pathLastHalf = split.slice(j).join('.');
remainingConds[pathLastHalf] = val;
obj[path] = cast(schematype.caster.schema, remainingConds, options, context)[pathLastHalf];

const ret = cast(schematype.caster.schema, remainingConds, options, context)[pathLastHalf];
if (ret === void 0) {
delete obj[path];
} else {
obj[path] = ret;
}
} else {
obj[path] = val;
}
Expand Down Expand Up @@ -250,16 +256,24 @@ module.exports = function cast(schema, obj, options, context) {
if (schema.nested[path]) {
continue;
}
if (options.upsert && options.strict) {
if (options.strict === 'throw') {

const strict = 'strict' in options ? options.strict : schema.options.strict;
const strictQuery = 'strictQuery' in options ?
options.strictQuery :
'strict' in options ?
options.strict :
'strict' in schema._userProvidedOptions ? schema._userProvidedOptions.strict :
schema.options.strictQuery;
if (options.upsert && strict) {
if (strict === 'throw') {
throw new StrictModeError(path);
}
throw new StrictModeError(path, 'Path "' + path + '" is not in ' +
'schema, strict mode is `true`, and upsert is `true`.');
} if (options.strictQuery === 'throw') {
} if (strictQuery === 'throw') {
throw new StrictModeError(path, 'Path "' + path + '" is not in ' +
'schema and strictQuery is \'throw\'.');
} else if (options.strictQuery) {
} else if (strictQuery) {
delete obj[path];
}
} else if (val == null) {
Expand Down
3 changes: 1 addition & 2 deletions lib/cast/objectid.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const ObjectId = require('../driver').get().ObjectId;
const assert = require('assert');

module.exports = function castObjectId(value) {
if (value == null) {
Expand All @@ -25,5 +24,5 @@ module.exports = function castObjectId(value) {
return new ObjectId(value.toString());
}

assert.ok(false);
return new ObjectId(value);
};
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,7 @@ function applyVirtuals(self, json, options, toObjectOptions) {
if (!path.startsWith(options.path + '.')) {
continue;
}
assignPath = path.substr(options.path.length + 1);
assignPath = path.substring(options.path.length + 1);
}
const parts = assignPath.split('.');
v = clone(self.get(path), options);
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
schematype.caster.discriminators != null &&
Object.keys(schematype.caster.discriminators).length > 0) {
const subdocs = utils.getValue(cur, doc);
const remnant = options.path.substr(cur.length + 1);
const remnant = options.path.substring(cur.length + 1);
const discriminatorKey = schematype.caster.schema.options.discriminatorKey;
modelNames = [];
for (const subdoc of subdocs) {
Expand All @@ -708,7 +708,7 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
}
const _path = discriminatorSchema.path(remnant);
if (_path == null || _path.options.refPath == null) {
const docValue = utils.getValue(data.localField.substr(cur.length + 1), subdoc);
const docValue = utils.getValue(data.localField.substring(cur.length + 1), subdoc);
ret.forEach((v, i) => {
if (v === docValue) {
ret[i] = SkipPopulateValue(v);
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/populate/modelNamesFromRefPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = function modelNamesFromRefPath(refPath, doc, populatedPath, mod
for (let i = 0; i < chunks.length; i += 2) {
const chunk = chunks[i];
if (_remaining.startsWith(chunk + '.')) {
_refPath += _remaining.substr(0, chunk.length) + chunks[i + 1];
_remaining = _remaining.substr(chunk.length + 1);
_refPath += _remaining.substring(0, chunk.length) + chunks[i + 1];
_remaining = _remaining.substring(chunk.length + 1);
} else if (i === chunks.length - 1) {
_refPath += _remaining;
_remaining = '';
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/update/applyTimestampsToChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ function applyTimestampsToUpdateKey(schema, key, update, now) {
// Single nested is easy
update[parentPath + '.' + updatedAt] = now;
} else if (parentSchemaType.$isMongooseDocumentArray) {
let childPath = key.substr(parentPath.length + 1);
let childPath = key.substring(parentPath.length + 1);

if (/^\d+$/.test(childPath)) {
update[parentPath + '.' + childPath][updatedAt] = now;
continue;
}

const firstDot = childPath.indexOf('.');
childPath = firstDot !== -1 ? childPath.substr(0, firstDot) : childPath;
childPath = firstDot !== -1 ? childPath.substring(0, firstDot) : childPath;

update[parentPath + '.' + childPath + '.' + updatedAt] = now;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/update/castArrayFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
const dot = key.indexOf('.');
let filterPath = dot === -1 ?
updatedPathsByFilter[key] + '.0' :
updatedPathsByFilter[key.substr(0, dot)] + '.0' + key.substr(dot);
updatedPathsByFilter[key.substring(0, dot)] + '.0' + key.substring(dot);
if (filterPath == null) {
throw new Error(`Filter path not found for ${key}`);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/update/updatedPathsByArrayFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function updatedPathsByArrayFilter(update) {
throw new Error(`Path '${path}' contains the same array filter multiple times`);
}
cur[match.substring(2, match.length - 1)] = path.
substr(0, firstMatch - 1).
substring(0, firstMatch - 1).
replace(/\$\[[^\]]+\]/g, '0');
}
return cur;
Expand Down
43 changes: 7 additions & 36 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,50 +949,21 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
*
* mongoose.isValidObjectId(new mongoose.Types.ObjectId()); // true
* mongoose.isValidObjectId('0123456789ab'); // true
* mongoose.isValidObjectId(6); // false
* mongoose.isValidObjectId(6); // true
* mongoose.isValidObjectId({ test: 42 }); // false
*
* @method isValidObjectId
* @api public
*/

Mongoose.prototype.isValidObjectId = function(v) {
if (v == null) {
return true;
}
const base = this || mongoose;
const ObjectId = base.driver.get().ObjectId;
if (v instanceof ObjectId) {
return true;
}

if (v._id != null) {
if (v._id instanceof ObjectId) {
return true;
}
if (v._id.toString instanceof Function) {
v = v._id.toString();
if (typeof v === 'string' && v.length === 12) {
return true;
}
if (typeof v === 'string' && /^[0-9A-Fa-f]{24}$/.test(v)) {
return true;
}
return false;
}
}

if (v.toString instanceof Function) {
v = v.toString();
}

if (typeof v === 'string' && v.length === 12) {
return true;
}
if (typeof v === 'string' && /^[0-9A-Fa-f]{24}$/.test(v)) {
return true;
try {
new this.Types.ObjectId(v);
} catch (err) {
return false;
}

return false;
return true;
};

Mongoose.prototype.syncIndexes = function() {
Expand Down
23 changes: 12 additions & 11 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -5057,18 +5057,19 @@ Query.prototype.cast = function(model, obj) {
model = getDiscriminatorByValue(model.discriminators, obj[discriminatorKey]) || model;
}

const opts = { upsert: this.options && this.options.upsert };
if (this.options) {
if ('strict' in this.options) {
opts.strict = this.options.strict;
opts.strictQuery = opts.strict;
}
if ('strictQuery' in this.options) {
opts.strictQuery = this.options.strictQuery;
}
}

try {
return cast(model.schema, obj, {
upsert: this.options && this.options.upsert,
strict: (this.options && 'strict' in this.options) ?
this.options.strict :
get(model, 'schema.options.strict', null),
strictQuery: (this.options && 'strictQuery' in this.options) ?
this.options.strictQuery :
(this.options && 'strict' in this.options) ?
this.options.strict :
get(model, 'schema.options.strictQuery', null)
}, this);
return cast(model.schema, obj, opts, this);
} catch (err) {
// CastError, assign model
if (typeof err.setModel === 'function') {
Expand Down

0 comments on commit cc6a697

Please sign in to comment.