Skip to content

Commit

Permalink
Merge pull request #13088 from Automattic/revert-12898-fix-strict-ret…
Browse files Browse the repository at this point in the history
…urn-all

Revert "fix(cast): remove empty conditions after strict applied"
  • Loading branch information
vkarpov15 committed Feb 28, 2023
2 parents a006210 + 1ca84b3 commit ccd6164
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 55 deletions.
9 changes: 1 addition & 8 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 29 additions & 47 deletions test/docs/cast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -172,4 +154,4 @@ describe('Cast Tutorial', function() {
});
// acquit:ignore:end
});
});
});

0 comments on commit ccd6164

Please sign in to comment.