Skip to content

Commit

Permalink
Merge branch 'master' into 6.9
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 23, 2023
2 parents d1dea8f + 59e5338 commit 32a0d08
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 168 deletions.
14 changes: 7 additions & 7 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Before we get into the specifics of validation syntax, please keep the following
Mongoose has several built-in validators.

- All [SchemaTypes](schematypes.html) have the built-in [required](api/schematype.html#schematype_SchemaType-required) validator. The required validator uses the [SchemaType's `checkRequired()` function](api/schematype.html#schematype_SchemaType-checkRequired) to determine if the value satisfies the required validator.
- [Numbers](api/schema-number-js.html#schema-number-js) have [`min` and `max`](schematypes.html#number-validators) validators.
- [Strings](api/schema-string-js.html#schema-string-js) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators.
- [Numbers](schematypes.html#numbers) have [`min` and `max`](schematypes.html#number-validators) validators.
- [Strings](schematypes.html#strings) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators.

Each of the validator links above provide more information about how to enable them and customize their error messages.

Expand Down Expand Up @@ -96,7 +96,7 @@ the value `false`, Mongoose will consider that a validation error.

Errors returned after failed validation contain an `errors` object
whose values are `ValidatorError` objects. Each
[ValidatorError](api/error-validation-js.html#error-validation-js) has `kind`, `path`,
[ValidatorError](api/error.html#error_Error-ValidatorError) has `kind`, `path`,
`value`, and `message` properties.
A ValidatorError also may have a `reason` property. If an error was
thrown in the validator, this property will contain the error that was
Expand Down Expand Up @@ -142,10 +142,10 @@ nested objects are not fully fledged paths.
### Update Validators

In the above examples, you learned about document validation. Mongoose also
supports validation for [`update()`](query.html#query_Query-update),
[`updateOne()`](query.html#query_Query-updateOne),
[`updateMany()`](query.html#query_Query-updateMany),
and [`findOneAndUpdate()`](query.html#query_Query-findOneAndUpdate) operations.
supports validation for [`update()`](api/query.html#query_Query-update),
[`updateOne()`](api/query.html#query_Query-updateOne),
[`updateMany()`](api/query.html#query_Query-updateMany),
and [`findOneAndUpdate()`](api/query.html#query_Query-findOneAndUpdate) operations.
Update validators are off by default - you need to specify
the `runValidators` option.

Expand Down
3 changes: 2 additions & 1 deletion lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions,
if ('strict' in schemaUserProvidedOptions) {
return schemaUserProvidedOptions.strict;
}
const mongooseOptions = context.mongooseCollection &&
const mongooseOptions = context &&
context.mongooseCollection &&
context.mongooseCollection.conn &&
context.mongooseCollection.conn.base &&
context.mongooseCollection.conn.base.options;
Expand Down
12 changes: 12 additions & 0 deletions lib/cursor/ChangeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class ChangeStream extends EventEmitter {

['close', 'change', 'end', 'error'].forEach(ev => {
this.driverChangeStream.on(ev, data => {
// Sometimes Node driver still polls after close, so
// avoid any uncaught exceptions due to closed change streams
// See tests for gh-7022
if (ev === 'error' && this.closed) {
return;
}
if (data != null && data.fullDocument != null && this.options && this.options.hydrate) {
data.fullDocument = this.options.model.hydrate(data.fullDocument);
}
Expand All @@ -71,6 +77,12 @@ class ChangeStream extends EventEmitter {

['close', 'change', 'end', 'error'].forEach(ev => {
this.driverChangeStream.on(ev, data => {
// Sometimes Node driver still polls after close, so
// avoid any uncaught exceptions due to closed change streams
// See tests for gh-7022
if (ev === 'error' && this.closed) {
return;
}
this.emit(ev, data);
});
});
Expand Down
12 changes: 6 additions & 6 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,17 @@ function cast$all(val) {
val = [val];
}

val = val.map(function(v) {
val = val.map((v) => {
if (!utils.isObject(v)) {
return v;
}
if (v.$elemMatch != null) {
return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch) };
return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch, null, this && this.$$context) };
}

const o = {};
o[this.path] = v;
return cast(this.casterConstructor.schema, o)[this.path];
return cast(this.casterConstructor.schema, o, null, this && this.$$context)[this.path];
}, this);

return this.castForQuery(val);
Expand Down Expand Up @@ -598,10 +598,10 @@ function cast$elemMatch(val) {
if (discriminatorKey != null &&
val[discriminatorKey] != null &&
discriminators[val[discriminatorKey]] != null) {
return cast(discriminators[val[discriminatorKey]], val);
return cast(discriminators[val[discriminatorKey]], val, null, this && this.$$context);
}

return cast(this.casterConstructor.schema, val);
return cast(this.casterConstructor.schema, val, null, this && this.$$context);
}

const handle = SchemaArray.prototype.$conditionalHandlers = {};
Expand All @@ -622,7 +622,7 @@ function createLogicalQueryOperatorHandler(op) {

const ret = [];
for (const obj of val) {
ret.push(cast(this.casterConstructor.schema, obj));
ret.push(cast(this.casterConstructor.schema, obj, null, this && this.$$context));
}

return ret;
Expand Down
1 change: 1 addition & 0 deletions test/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ describe('aggregate: ', function() {

describe('exec', function() {
beforeEach(async function() {
this.timeout(4000); // double the default of 2 seconds
await setupData(db);
});

Expand Down
18 changes: 8 additions & 10 deletions test/collection.capped.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ const Schema = mongoose.Schema;
describe('collections: capped:', function() {
let db;

const connectionsToClose = [];

before(function() {
db = start();
});

after(async function() {
afterEach(async function() {
if (db == null) {
return;
}
await db.close();
await Promise.all(connectionsToClose.map((v) => v.close()));
db = null;
});

it('schemas should have option size', function() {
Expand All @@ -41,6 +38,8 @@ describe('collections: capped:', function() {
it('creation', async function() {
this.timeout(15000);

db = start();

await db.dropCollection('Test').catch(() => {});

const capped = new Schema({ key: String });
Expand All @@ -54,8 +53,7 @@ describe('collections: capped:', function() {
});

it('skips when setting autoCreate to false (gh-8566)', async function() {
const db = start();
connectionsToClose.push(db);
db = start();
this.timeout(30000);
await db.dropDatabase();

Expand Down
19 changes: 10 additions & 9 deletions test/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ const assert = require('assert');
const mongoose = start.mongoose;

describe('collections:', function() {
const connectionsToClose = [];
let db = null;

after(async function() {
await Promise.all(connectionsToClose.map((v) => v.close()));
afterEach(async function() {
if (db == null) {
return;
}
await db.close();
db = null;
});

it('should buffer commands until connection is established', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
db = mongoose.createConnection();
const collection = db.collection('test-buffering-collection');
let connected = false;
let insertedId = undefined;
Expand Down Expand Up @@ -49,8 +52,7 @@ describe('collections:', function() {
});

it('returns a promise if buffering and no callback (gh-7676)', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
db = mongoose.createConnection();
const collection = db.collection('gh7676');

const promise = collection.insertOne({ foo: 'bar' }, {})
Expand Down Expand Up @@ -152,8 +154,7 @@ describe('collections:', function() {
});

it('buffers for sync methods (gh-10610)', function(done) {
const db = mongoose.createConnection();
connectionsToClose.push(db);
db = mongoose.createConnection();
const collection = db.collection('gh10610');

collection.find({}, {}, function(err, res) {
Expand Down
16 changes: 8 additions & 8 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,29 +773,29 @@ describe('connections:', function() {
db2.close();
});

it('cache connections to the same db', function(done) {
it('cache connections to the same db', function() {
const db = start();
const db2 = db.useDb(start.databases[1], { useCache: true });
const db3 = db.useDb(start.databases[1], { useCache: true });

assert.strictEqual(db2, db3);
db.close(done);
return db.close();
});
});

describe('shouldAuthenticate()', function() {
describe('when using standard authentication', function() {
describe('when username and password are undefined', function() {
it('should return false', function(done) {
it('should return false', function() {
const db = mongoose.createConnection(start.uri, {});

assert.equal(db.shouldAuthenticate(), false);

db.close(done);
return db.close();
});
});
describe('when username and password are empty strings', function() {
it('should return false', function(done) {
it('should return false', function() {
const db = mongoose.createConnection(start.uri, {
user: '',
pass: ''
Expand All @@ -804,7 +804,7 @@ describe('connections:', function() {

assert.equal(db.shouldAuthenticate(), false);

db.close(done);
return db.close();
});
});
describe('when both username and password are defined', function() {
Expand All @@ -823,14 +823,14 @@ describe('connections:', function() {
});
describe('when using MONGODB-X509 authentication', function() {
describe('when username and password are undefined', function() {
it('should return false', function(done) {
it('should return false', function() {
const db = mongoose.createConnection(start.uri, {});
db.on('error', function() {
});

assert.equal(db.shouldAuthenticate(), false);

db.close(done);
return db.close();
});
});
describe('when only username is defined', function() {
Expand Down
Loading

0 comments on commit 32a0d08

Please sign in to comment.