applyPlugins.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. for (const plugin of plugins) {
  9. schema.plugin(plugin[0], plugin[1]);
  10. }
  11. }
  12. options = Object.assign({}, options);
  13. delete options.skipTopLevel;
  14. if (options.applyPluginsToChildSchemas !== false) {
  15. for (const path of Object.keys(schema.paths)) {
  16. const type = schema.paths[path];
  17. if (type.schema != null) {
  18. applyPlugins(type.schema, plugins, options, cacheKey);
  19. // Recompile schema because plugins may have changed it, see gh-7572
  20. type.caster.prototype.$__setSchema(type.schema);
  21. }
  22. }
  23. }
  24. const discriminators = schema.discriminators;
  25. if (discriminators == null) {
  26. return;
  27. }
  28. const applyPluginsToDiscriminators = options.applyPluginsToDiscriminators;
  29. const keys = Object.keys(discriminators);
  30. for (const discriminatorKey of keys) {
  31. const discriminatorSchema = discriminators[discriminatorKey];
  32. applyPlugins(discriminatorSchema, plugins,
  33. { skipTopLevel: !applyPluginsToDiscriminators }, cacheKey);
  34. }
  35. };