MCPcopy Create free account
hub / github.com/Automattic/mongoose / getPositionalPathType

Function getPositionalPathType

lib/schema.js:2022–2081  ·  view source on GitHub ↗

* ignore. Deprecated re: #6405 * @param {any} self * @param {string} path * @api private

(self, path, cleanPath)

Source from the content-addressed store, hash-verified

2020 */
2021
2022function getPositionalPathType(self, path, cleanPath) {
2023 const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
2024 if (subpaths.length < 2) {
2025 return Object.hasOwn(self.paths, subpaths[0]) ?
2026 self.paths[subpaths[0]] :
2027 'adhocOrUndefined';
2028 }
2029
2030 let val = self.path(subpaths[0]);
2031 let isNested = false;
2032 if (!val) {
2033 return 'adhocOrUndefined';
2034 }
2035
2036 const last = subpaths.length - 1;
2037
2038 for (let i = 1; i < subpaths.length; ++i) {
2039 isNested = false;
2040 const subpath = subpaths[i];
2041
2042 if (i === last && val && !/\D/.test(subpath)) {
2043 if (val.$isMongooseDocumentArray) {
2044 val = val.embeddedSchemaType;
2045 } else if (val instanceof MongooseTypes.Array) {
2046 // StringSchema, NumberSchema, etc
2047 val = val.embeddedSchemaType;
2048 } else {
2049 val = undefined;
2050 }
2051 break;
2052 }
2053
2054 // ignore if its just a position segment: path.0.subpath
2055 if (!/\D/.test(subpath)) {
2056 // Nested array
2057 if (val instanceof MongooseTypes.Array && i !== last) {
2058 val = val.embeddedSchemaType;
2059 }
2060 continue;
2061 }
2062
2063 if (!val?.schema) {
2064 val = undefined;
2065 break;
2066 }
2067
2068 const type = val.schema.pathType(subpath);
2069 isNested = (type === 'nested');
2070 val = val.schema.path(subpath);
2071 }
2072
2073 self.subpaths[cleanPath] = val;
2074 if (val) {
2075 return 'real';
2076 }
2077 if (isNested) {
2078 return 'nested';
2079 }

Callers 2

schema.jsFile · 0.85
getPositionalPathFunction · 0.85

Calls 1

testMethod · 0.80

Tested by

no test coverage detected