create-schema-validation.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const memoize = require("./memoize");
  7. /** @typedef {import("schema-utils/declarations/validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
  8. /** @typedef {import("./fs").JsonObject} JsonObject */
  9. const getValidate = memoize(() => require("schema-utils").validate);
  10. /**
  11. * @template {object | object[]} T
  12. * @param {(function(T): boolean) | undefined} check check
  13. * @param {() => JsonObject} getSchema get schema fn
  14. * @param {ValidationErrorConfiguration} options options
  15. * @returns {function(T=): void} validate
  16. */
  17. const createSchemaValidation = (check, getSchema, options) => {
  18. getSchema = memoize(getSchema);
  19. return value => {
  20. if (check && !check(/** @type {T} */ (value))) {
  21. getValidate()(
  22. getSchema(),
  23. /** @type {object | object[]} */
  24. (value),
  25. options
  26. );
  27. require("util").deprecate(
  28. () => {},
  29. "webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
  30. "DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
  31. )();
  32. }
  33. };
  34. };
  35. module.exports = createSchemaValidation;