castFilterPath.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const isOperator = require('./isOperator');
  3. module.exports = function castFilterPath(query, schematype, val) {
  4. const ctx = query;
  5. const any$conditionals = Object.keys(val).some(isOperator);
  6. if (!any$conditionals) {
  7. return schematype.castForQueryWrapper({
  8. val: val,
  9. context: ctx
  10. });
  11. }
  12. const ks = Object.keys(val);
  13. let k = ks.length;
  14. while (k--) {
  15. const $cond = ks[k];
  16. const nested = val[$cond];
  17. if ($cond === '$not') {
  18. if (nested && schematype && !schematype.caster) {
  19. const _keys = Object.keys(nested);
  20. if (_keys.length && isOperator(_keys[0])) {
  21. for (const key of Object.keys(nested)) {
  22. nested[key] = schematype.castForQueryWrapper({
  23. $conditional: key,
  24. val: nested[key],
  25. context: ctx
  26. });
  27. }
  28. } else {
  29. val[$cond] = schematype.castForQueryWrapper({
  30. $conditional: $cond,
  31. val: nested,
  32. context: ctx
  33. });
  34. }
  35. continue;
  36. }
  37. } else {
  38. val[$cond] = schematype.castForQueryWrapper({
  39. $conditional: $cond,
  40. val: nested,
  41. context: ctx
  42. });
  43. }
  44. }
  45. return val;
  46. };