applyPlugins.js 1.2 KB

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