getSchemaDiscriminatorByValue.js 714 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. const areDiscriminatorValuesEqual = require('./areDiscriminatorValuesEqual');
  3. /*!
  4. * returns discriminator by discriminatorMapping.value
  5. *
  6. * @param {Schema} schema
  7. * @param {string} value
  8. */
  9. module.exports = function getSchemaDiscriminatorByValue(schema, value) {
  10. if (schema == null || schema.discriminators == null) {
  11. return null;
  12. }
  13. for (const key of Object.keys(schema.discriminators)) {
  14. const discriminatorSchema = schema.discriminators[key];
  15. if (discriminatorSchema.discriminatorMapping == null) {
  16. continue;
  17. }
  18. if (areDiscriminatorValuesEqual(discriminatorSchema.discriminatorMapping.value, value)) {
  19. return discriminatorSchema;
  20. }
  21. }
  22. return null;
  23. };