getVirtual.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. module.exports = getVirtual;
  3. /*!
  4. * ignore
  5. */
  6. function getVirtual(schema, name) {
  7. if (schema.virtuals[name]) {
  8. return schema.virtuals[name];
  9. }
  10. const parts = name.split('.');
  11. let cur = '';
  12. let nestedSchemaPath = '';
  13. for (let i = 0; i < parts.length; ++i) {
  14. cur += (cur.length > 0 ? '.' : '') + parts[i];
  15. if (schema.virtuals[cur]) {
  16. if (i === parts.length - 1) {
  17. schema.virtuals[cur].$nestedSchemaPath = nestedSchemaPath;
  18. return schema.virtuals[cur];
  19. }
  20. continue;
  21. }
  22. if (schema.nested[cur]) {
  23. continue;
  24. }
  25. if (schema.paths[cur] && schema.paths[cur].schema) {
  26. schema = schema.paths[cur].schema;
  27. const rest = parts.slice(i + 1).join('.');
  28. if (schema.virtuals[rest]) {
  29. if (i === parts.length - 2) {
  30. schema.virtuals[rest].$nestedSchemaPath =
  31. [nestedSchemaPath, cur].filter(v => !!v).join('.');
  32. return schema.virtuals[rest];
  33. }
  34. continue;
  35. }
  36. if (i + 1 < parts.length && schema.discriminators) {
  37. for (const key of Object.keys(schema.discriminators)) {
  38. const _virtual = getVirtual(schema.discriminators[key], rest);
  39. if (_virtual != null) {
  40. _virtual.$nestedSchemaPath = [nestedSchemaPath, cur].
  41. filter(v => !!v).join('.');
  42. return _virtual;
  43. }
  44. }
  45. }
  46. nestedSchemaPath += (nestedSchemaPath.length > 0 ? '.' : '') + cur;
  47. cur = '';
  48. continue;
  49. }
  50. return null;
  51. }
  52. }