1 | const mongoose = require('mongoose');
|
2 |
|
3 | module.exports = (modelName, path) => {
|
4 | try {
|
5 | const schemaType = mongoose
|
6 | .model(modelName)
|
7 | .schema.path(path);
|
8 |
|
9 | const { name } = schemaType.constructor;
|
10 |
|
11 | return name === 'DocumentArrayPath' ||
|
12 | name === 'SingleNestedPath'
|
13 | ? Object.entries(schemaType.schema.paths)
|
14 | .filter(([, value]) => {
|
15 | return value.isRequired;
|
16 | })
|
17 | .map(([key]) => {
|
18 | return `${path}.${key}`;
|
19 | })
|
20 | : path;
|
21 | } catch (e) {
|
22 | return path;
|
23 | }
|
24 | };
|