UNPKG

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