webpack/lib/util/create-schema-validation.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const memoize = require("./memoize");
2025-07-09 18:59:21 +08:00
/** @typedef {import("schema-utils").Schema} Schema */
2025-10-02 23:26:16 +08:00
/** @typedef {import("schema-utils").ValidationErrorConfiguration} ValidationErrorConfiguration */
2024-10-24 05:07:47 +08:00
2024-10-24 05:41:15 +08:00
const getValidate = memoize(() => require("schema-utils").validate);
2024-10-24 05:07:47 +08:00
/**
* @template {object | object[]} T
* @param {((value: T) => boolean) | undefined} check check
2025-07-09 18:59:21 +08:00
* @param {() => Schema} getSchema get schema fn
2024-10-24 05:07:47 +08:00
* @param {ValidationErrorConfiguration} options options
* @returns {(value?: T) => void} validate
2024-10-24 05:07:47 +08:00
*/
const createSchemaValidation = (check, getSchema, options) => {
getSchema = memoize(getSchema);
return (value) => {
2025-03-27 08:07:25 +08:00
if (check && value && !check(value)) {
getValidate()(
getSchema(),
/** @type {EXPECTED_OBJECT | EXPECTED_OBJECT[]} */
(value),
options
);
2024-10-24 05:07:47 +08:00
require("util").deprecate(
() => {},
"webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
"DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
)();
}
};
};
module.exports = createSchemaValidation;