Skip to content

Commit 01dd471

Browse files
committedJul 15, 2024
Reapply "fix(cast): remove empty conditions after strict applied"
This reverts commit 1ca84b3.
1 parent 53d382b commit 01dd471

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed
 

‎lib/cast.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ module.exports = function cast(schema, obj, options, context) {
6565
if (!Array.isArray(val)) {
6666
throw new CastError('Array', val, path);
6767
}
68-
for (let k = 0; k < val.length; ++k) {
68+
for (let k = val.length - 1; k >= 0; k--) {
6969
if (val[k] == null || typeof val[k] !== 'object') {
7070
throw new CastError('Object', val[k], path + '.' + k);
7171
}
7272
val[k] = cast(schema, val[k], options, context);
73+
if (Object.keys(val[k]).length === 0) {
74+
val.splice(k, 1);
75+
}
76+
}
77+
78+
if (val.length === 0) {
79+
delete obj[path];
7380
}
7481
} else if (path === '$where') {
7582
type = typeof val;

‎test/docs/cast.test.js

+46-28
Original file line numberDiff line numberDiff line change
@@ -101,40 +101,58 @@ describe('Cast Tutorial', function() {
101101
await query.exec();
102102
});
103103

104-
it('strictQuery true', async function() {
105-
mongoose.deleteModel('Character');
106-
const schema = new mongoose.Schema({ name: String, age: Number }, {
107-
strictQuery: true
104+
describe('strictQuery', function() {
105+
it('strictQuery true - simple object', async function() {
106+
mongoose.deleteModel('Character');
107+
const schema = new mongoose.Schema({ name: String, age: Number }, {
108+
strictQuery: true
109+
});
110+
Character = mongoose.model('Character', schema);
111+
112+
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
113+
114+
await query.exec();
115+
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
116+
// acquit:ignore:start
117+
assert.deepEqual(query.getFilter(), {});
118+
// acquit:ignore:end
108119
});
109-
Character = mongoose.model('Character', schema);
110120

111-
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
121+
it('strictQuery true - conditions', async function() {
122+
mongoose.deleteModel('Character');
123+
const schema = new mongoose.Schema({ name: String, age: Number }, {
124+
strictQuery: true
125+
});
126+
Character = mongoose.model('Character', schema);
112127

113-
await query.exec();
114-
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
115-
// acquit:ignore:start
116-
assert.deepEqual(query.getFilter(), {});
117-
// acquit:ignore:end
118-
});
128+
const query = Character.findOne({ $or: [{ notInSchema: { $lt: 'not a number' } }], $and: [{ name: 'abc' }, { age: { $gt: 18 } }, { notInSchema: { $lt: 'not a number' } }] });
119129

120-
it('strictQuery throw', async function() {
121-
mongoose.deleteModel('Character');
122-
const schema = new mongoose.Schema({ name: String, age: Number }, {
123-
strictQuery: 'throw'
130+
await query.exec();
131+
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
132+
// acquit:ignore:start
133+
assert.deepEqual(query.getFilter(), { $and: [{ name: 'abc' }, { age: { $gt: 18 } }] });
134+
// acquit:ignore:end
124135
});
125-
Character = mongoose.model('Character', schema);
126136

127-
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
128-
129-
const err = await query.exec().then(() => null, err => err);
130-
err.name; // 'StrictModeError'
131-
// Path "notInSchema" is not in schema and strictQuery is 'throw'.
132-
err.message;
133-
// acquit:ignore:start
134-
assert.equal(err.name, 'StrictModeError');
135-
assert.equal(err.message, 'Path "notInSchema" is not in schema and ' +
136-
'strictQuery is \'throw\'.');
137-
// acquit:ignore:end
137+
it('strictQuery throw', async function() {
138+
mongoose.deleteModel('Character');
139+
const schema = new mongoose.Schema({ name: String, age: Number }, {
140+
strictQuery: 'throw'
141+
});
142+
Character = mongoose.model('Character', schema);
143+
144+
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
145+
146+
const err = await query.exec().then(() => null, err => err);
147+
err.name; // 'StrictModeError'
148+
// Path "notInSchema" is not in schema and strictQuery is 'throw'.
149+
err.message;
150+
// acquit:ignore:start
151+
assert.equal(err.name, 'StrictModeError');
152+
assert.equal(err.message, 'Path "notInSchema" is not in schema and ' +
153+
'strictQuery is \'throw\'.');
154+
// acquit:ignore:end
155+
});
138156
});
139157

140158
it('implicit in', async function() {

0 commit comments

Comments
 (0)
Please sign in to comment.