From 1ca84b334f2ac8e75641eed108f5f17ad1c82f43 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 26 Feb 2023 13:46:29 -0500 Subject: [PATCH] Revert "fix(cast): remove empty conditions after strict applied" --- lib/cast.js | 9 +---- test/docs/cast.test.js | 76 ++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/lib/cast.js b/lib/cast.js index d0b3d536986..8a5bb696999 100644 --- a/lib/cast.js +++ b/lib/cast.js @@ -64,18 +64,11 @@ module.exports = function cast(schema, obj, options, context) { if (!Array.isArray(val)) { throw new CastError('Array', val, path); } - for (let k = val.length - 1; k >= 0; k--) { + for (let k = 0; k < val.length; ++k) { if (val[k] == null || typeof val[k] !== 'object') { throw new CastError('Object', val[k], path + '.' + k); } val[k] = cast(schema, val[k], options, context); - if (Object.keys(val[k]).length === 0) { - val.splice(k, 1); - } - } - - if (val.length === 0) { - delete obj[path]; } } else if (path === '$where') { type = typeof val; diff --git a/test/docs/cast.test.js b/test/docs/cast.test.js index 02b66d13b58..b450f3a8f20 100644 --- a/test/docs/cast.test.js +++ b/test/docs/cast.test.js @@ -101,58 +101,40 @@ describe('Cast Tutorial', function() { await query.exec(); }); - describe('strictQuery', function() { - it('strictQuery true - simple object', async function() { - mongoose.deleteModel('Character'); - const schema = new mongoose.Schema({ name: String, age: Number }, { - strictQuery: true - }); - Character = mongoose.model('Character', schema); - - const query = Character.findOne({ notInSchema: { $lt: 'not a number' } }); - - await query.exec(); - query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema` - // acquit:ignore:start - assert.deepEqual(query.getFilter(), {}); - // acquit:ignore:end + it('strictQuery true', async function() { + mongoose.deleteModel('Character'); + const schema = new mongoose.Schema({ name: String, age: Number }, { + strictQuery: true }); + Character = mongoose.model('Character', schema); - it('strictQuery true - conditions', async function() { - mongoose.deleteModel('Character'); - const schema = new mongoose.Schema({ name: String, age: Number }, { - strictQuery: true - }); - Character = mongoose.model('Character', schema); + const query = Character.findOne({ notInSchema: { $lt: 'not a number' } }); - const query = Character.findOne({ $or: [{ notInSchema: { $lt: 'not a number' } }], $and: [{ name: 'abc' }, { age: { $gt: 18 } }, { notInSchema: { $lt: 'not a number' } }] }); + await query.exec(); + query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema` + // acquit:ignore:start + assert.deepEqual(query.getFilter(), {}); + // acquit:ignore:end + }); - await query.exec(); - query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema` - // acquit:ignore:start - assert.deepEqual(query.getFilter(), { $and: [{ name: 'abc' }, { age: { $gt: 18 } }] }); - // acquit:ignore:end + it('strictQuery throw', async function() { + mongoose.deleteModel('Character'); + const schema = new mongoose.Schema({ name: String, age: Number }, { + strictQuery: 'throw' }); + Character = mongoose.model('Character', schema); - it('strictQuery throw', async function() { - mongoose.deleteModel('Character'); - const schema = new mongoose.Schema({ name: String, age: Number }, { - strictQuery: 'throw' - }); - Character = mongoose.model('Character', schema); - - const query = Character.findOne({ notInSchema: { $lt: 'not a number' } }); - - const err = await query.exec().then(() => null, err => err); - err.name; // 'StrictModeError' - // Path "notInSchema" is not in schema and strictQuery is 'throw'. - err.message; - // acquit:ignore:start - assert.equal(err.name, 'StrictModeError'); - assert.equal(err.message, 'Path "notInSchema" is not in schema and ' + - 'strictQuery is \'throw\'.'); - // acquit:ignore:end - }); + const query = Character.findOne({ notInSchema: { $lt: 'not a number' } }); + + const err = await query.exec().then(() => null, err => err); + err.name; // 'StrictModeError' + // Path "notInSchema" is not in schema and strictQuery is 'throw'. + err.message; + // acquit:ignore:start + assert.equal(err.name, 'StrictModeError'); + assert.equal(err.message, 'Path "notInSchema" is not in schema and ' + + 'strictQuery is \'throw\'.'); + // acquit:ignore:end }); it('implicit in', async function() { @@ -172,4 +154,4 @@ describe('Cast Tutorial', function() { }); // acquit:ignore:end }); -}); +}); \ No newline at end of file