All files / mongoose/lib/helpers/query sanitizeFilter.js

13.04% Statements 3/23
0% Branches 0/20
0% Functions 0/1
13.04% Lines 3/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39    1x 1x   1x                                                                  
'use strict';
 
const hasDollarKeys = require('./hasDollarKeys');
const { trustedSymbol } = require('./trusted');
 
module.exports = function sanitizeFilter(filter) {
  if (filter == null || typeof filter !== 'object') {
    return filter;
  }
  if (Array.isArray(filter)) {
    for (const subfilter of filter) {
      sanitizeFilter(subfilter);
    }
    return filter;
  }
 
  const filterKeys = Object.keys(filter);
  for (const key of filterKeys) {
    const value = filter[key];
    if (value != null && value[trustedSymbol]) {
      continue;
    }
    if (key === '$and' || key === '$or') {
      sanitizeFilter(value);
      continue;
    }
 
    if (hasDollarKeys(value)) {
      const keys = Object.keys(value);
      if (keys.length === 1 && keys[0] === '$eq') {
        continue;
      }
      filter[key] = { $eq: filter[key] };
    }
  }
 
  return filter;
};