getPath.js 793 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. /*!
  3. * Behaves like `Schema#path()`, except for it also digs into arrays without
  4. * needing to put `.0.`, so `getPath(schema, 'docArr.elProp')` works.
  5. */
  6. module.exports = function getPath(schema, path) {
  7. let schematype = schema.path(path);
  8. if (schematype != null) {
  9. return schematype;
  10. }
  11. const pieces = path.split('.');
  12. let cur = '';
  13. let isArray = false;
  14. for (const piece of pieces) {
  15. if (/^\d+$/.test(piece) && isArray) {
  16. continue;
  17. }
  18. cur = cur.length === 0 ? piece : cur + '.' + piece;
  19. schematype = schema.path(cur);
  20. if (schematype != null && schematype.schema) {
  21. schema = schematype.schema;
  22. cur = '';
  23. if (schematype.$isMongooseDocumentArray) {
  24. isArray = true;
  25. }
  26. }
  27. }
  28. return schematype;
  29. };