Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getEmbeddedDiscriminatorPath.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const get = require('../get');
  3. /*!
  4. * Like `schema.path()`, except with a document, because impossible to
  5. * determine path type without knowing the embedded discriminator key.
  6. */
  7. module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
  8. options = options || {};
  9. const typeOnly = options.typeOnly;
  10. const parts = path.split('.');
  11. let schema = null;
  12. let type = 'adhocOrUndefined';
  13. for (let i = 0; i < parts.length; ++i) {
  14. const subpath = parts.slice(0, i + 1).join('.');
  15. schema = doc.schema.path(subpath);
  16. if (schema == null) {
  17. type = 'adhocOrUndefined';
  18. continue;
  19. }
  20. if (schema.instance === 'Mixed') {
  21. return typeOnly ? 'real' : schema;
  22. }
  23. type = doc.schema.pathType(subpath);
  24. if ((schema.$isSingleNested || schema.$isMongooseDocumentArrayElement) &&
  25. schema.schema.discriminators != null) {
  26. const discriminators = schema.schema.discriminators;
  27. const discriminatorKey = doc.get(subpath + '.' +
  28. get(schema, 'schema.options.discriminatorKey'));
  29. if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
  30. continue;
  31. }
  32. const rest = parts.slice(i + 1).join('.');
  33. return getEmbeddedDiscriminatorPath(doc.get(subpath), rest, options);
  34. }
  35. }
  36. // Are we getting the whole schema or just the type, 'real', 'nested', etc.
  37. return typeOnly ? type : schema;
  38. };