getConstructor.js 776 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. const getDiscriminatorByValue = require('./getDiscriminatorByValue');
  3. /*!
  4. * Find the correct constructor, taking into account discriminators
  5. */
  6. module.exports = function getConstructor(Constructor, value) {
  7. const discriminatorKey = Constructor.schema.options.discriminatorKey;
  8. if (value != null &&
  9. Constructor.discriminators &&
  10. value[discriminatorKey] != null) {
  11. if (Constructor.discriminators[value[discriminatorKey]]) {
  12. Constructor = Constructor.discriminators[value[discriminatorKey]];
  13. } else {
  14. const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[discriminatorKey]);
  15. if (constructorByValue) {
  16. Constructor = constructorByValue;
  17. }
  18. }
  19. }
  20. return Constructor;
  21. };