applyPlugins.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. module.exports = function applyPlugins(schema, plugins, options, cacheKey) {
  3. if (schema[cacheKey]) {
  4. return;
  5. }
  6. schema[cacheKey] = true;
  7. if (!options || !options.skipTopLevel) {
  8. let pluginTags = null;
  9. for (const plugin of plugins) {
  10. const tags = plugin[1] == null ? null : plugin[1].tags;
  11. if (!Array.isArray(tags)) {
  12. schema.plugin(plugin[0], plugin[1]);
  13. continue;
  14. }
  15. pluginTags = pluginTags || new Set(schema.options.pluginTags || []);
  16. if (!tags.find(tag => pluginTags.has(tag))) {
  17. continue;
  18. }
  19. schema.plugin(plugin[0], plugin[1]);
  20. }
  21. }
  22. options = Object.assign({}, options);
  23. delete options.skipTopLevel;
  24. if (options.applyPluginsToChildSchemas !== false) {
  25. for (const path of Object.keys(schema.paths)) {
  26. const type = schema.paths[path];
  27. if (type.schema != null) {
  28. applyPlugins(type.schema, plugins, options, cacheKey);
  29. // Recompile schema because plugins may have changed it, see gh-7572
  30. type.caster.prototype.$__setSchema(type.schema);
  31. }
  32. }
  33. }
  34. const discriminators = schema.discriminators;
  35. if (discriminators == null) {
  36. return;
  37. }
  38. const applyPluginsToDiscriminators = options.applyPluginsToDiscriminators;
  39. const keys = Object.keys(discriminators);
  40. for (const discriminatorKey of keys) {
  41. const discriminatorSchema = discriminators[discriminatorKey];
  42. applyPlugins(discriminatorSchema, plugins,
  43. { skipTopLevel: !applyPluginsToDiscriminators }, cacheKey);
  44. }
  45. };