create-schema-validation.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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").Schema} Schema */
  8. /** @typedef {import("schema-utils/declarations/validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
  9. /** @typedef {import("./fs").JsonObject} JsonObject */
  10. const getValidate = memoize(() => require("schema-utils").validate);
  11. /**
  12. * @template {object | object[]} T
  13. * @param {((value: T) => boolean) | undefined} check check
  14. * @param {() => Schema} getSchema get schema fn
  15. * @param {ValidationErrorConfiguration} options options
  16. * @returns {(value?: T) => void} validate
  17. */
  18. const createSchemaValidation = (check, getSchema, options) => {
  19. getSchema = memoize(getSchema);
  20. return value => {
  21. if (check && value && !check(value)) {
  22. getValidate()(
  23. getSchema(),
  24. /** @type {EXPECTED_OBJECT | EXPECTED_OBJECT[]} */
  25. (value),
  26. options
  27. );
  28. require("util").deprecate(
  29. () => {},
  30. "webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
  31. "DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
  32. )();
  33. }
  34. };
  35. };
  36. module.exports = createSchemaValidation;