Skip to content

Commit 35018b6

Browse files
committed
Remove Array.isArray check, add // empty object validation in requireFilter (Automattic#14913)
1 parent 19fa17b commit 35018b6

File tree

2 files changed

+212
-53
lines changed

2 files changed

+212
-53
lines changed

lib/query.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,28 @@ function Query(conditions, options, model, collection) {
165165
}
166166
}
167167

168-
// Helper function to check empty/invalid filter
168+
// Helper function to check if an object is empty or contains only empty objects/arrays
169+
function isEmptyFilter(obj) {
170+
if (obj == null) return true;
171+
if (typeof obj !== 'object' || obj === null) return false;
172+
if (Object.keys(obj).length === 0) return true;
173+
174+
// Check $and, $or, $nor arrays
175+
for (const key of ['$and', '$or', '$nor']) {
176+
if (Array.isArray(obj[key])) {
177+
// If array is empty or all elements are empty objects, consider it empty
178+
if (obj[key].length === 0 || obj[key].every(item => isEmptyFilter(item))) {
179+
return true;
180+
}
181+
}
182+
}
183+
184+
return false;
185+
}
186+
187+
// Helper function to check for empty/invalid filter
169188
function checkRequireFilter(filter, options) {
170-
if (options && options.requireFilter &&
171-
(filter == null ||
172-
(typeof filter === 'object' && filter !== null && !Array.isArray(filter) && Object.keys(filter).length === 0))) {
189+
if (options && options.requireFilter && isEmptyFilter(filter)) {
173190
throw new Error('Empty or invalid filter not allowed with requireFilter enabled');
174191
}
175192
}

0 commit comments

Comments
 (0)