Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
reexport the schema validation function from schema-utils
  • Loading branch information
sokra committed Dec 17, 2020
commit 16d5c35b1eb42acafe11da7b0ce534d7c6290abc
157 changes: 84 additions & 73 deletions lib/validateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,94 +69,105 @@ const REMOVED = {
};
/* cSpell:enable */

const validateSchema = (schema, options) => {
validate(schema, options, {
name: "Webpack",
postFormatter: (formattedError, error) => {
const children = error.children;
if (
children &&
children.some(
child =>
child.keyword === "absolutePath" &&
child.dataPath === ".output.filename"
)
) {
return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`;
}

if (
children &&
children.some(
child => child.keyword === "pattern" && child.dataPath === ".devtool"
)
) {
return (
`${formattedError}\n` +
"BREAKING CHANGE since webpack 5: The devtool option is more strict.\n" +
"Please strictly follow the order of the keywords in the pattern."
);
}

if (error.keyword === "additionalProperties") {
const params = /** @type {import("ajv").AdditionalPropertiesParams} */ (error.params);
/**
* @param {Parameters<typeof validate>[0]} schema a json schema
* @param {Parameters<typeof validate>[1]} options the options that should be validated
* @param {Parameters<typeof validate>[2]=} validationConfiguration configuration for generating errors
* @returns {void}
*/
const validateSchema = (schema, options, validationConfiguration) => {
validate(
schema,
options,
validationConfiguration || {
name: "Webpack",
postFormatter: (formattedError, error) => {
const children = error.children;
if (
Object.prototype.hasOwnProperty.call(
DID_YOU_MEAN,
params.additionalProperty
children &&
children.some(
child =>
child.keyword === "absolutePath" &&
child.dataPath === ".output.filename"
)
) {
return `${formattedError}\nDid you mean ${
DID_YOU_MEAN[params.additionalProperty]
}?`;
return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`;
}

if (
Object.prototype.hasOwnProperty.call(
REMOVED,
params.additionalProperty
children &&
children.some(
child =>
child.keyword === "pattern" && child.dataPath === ".devtool"
)
) {
return `${formattedError}\n${REMOVED[params.additionalProperty]}?`;
return (
`${formattedError}\n` +
"BREAKING CHANGE since webpack 5: The devtool option is more strict.\n" +
"Please strictly follow the order of the keywords in the pattern."
);
}

if (!error.dataPath) {
if (params.additionalProperty === "debug") {
return (
`${formattedError}\n` +
"The 'debug' property was removed in webpack 2.0.0.\n" +
"Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
"Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
"plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" debug: true\n" +
" })\n" +
"]"
);
if (error.keyword === "additionalProperties") {
const params = /** @type {import("ajv").AdditionalPropertiesParams} */ (error.params);
if (
Object.prototype.hasOwnProperty.call(
DID_YOU_MEAN,
params.additionalProperty
)
) {
return `${formattedError}\nDid you mean ${
DID_YOU_MEAN[params.additionalProperty]
}?`;
}

if (params.additionalProperty) {
return (
`${formattedError}\n` +
"For typos: please correct them.\n" +
"For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
" Loaders should be updated to allow passing options via loader options in module.rules.\n" +
" Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
" plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" // test: /\\.xxx$/, // may apply this only for some modules\n" +
" options: {\n" +
` ${params.additionalProperty}: …\n` +
" }\n" +
" })\n" +
" ]"
);
if (
Object.prototype.hasOwnProperty.call(
REMOVED,
params.additionalProperty
)
) {
return `${formattedError}\n${REMOVED[params.additionalProperty]}?`;
}

if (!error.dataPath) {
if (params.additionalProperty === "debug") {
return (
`${formattedError}\n` +
"The 'debug' property was removed in webpack 2.0.0.\n" +
"Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
"Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
"plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" debug: true\n" +
" })\n" +
"]"
);
}

if (params.additionalProperty) {
return (
`${formattedError}\n` +
"For typos: please correct them.\n" +
"For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" +
" Loaders should be updated to allow passing options via loader options in module.rules.\n" +
" Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
" plugins: [\n" +
" new webpack.LoaderOptionsPlugin({\n" +
" // test: /\\.xxx$/, // may apply this only for some modules\n" +
" options: {\n" +
` ${params.additionalProperty}: …\n` +
" }\n" +
" })\n" +
" ]"
);
}
}
}
}

return formattedError;
return formattedError;
}
}
});
);
};
module.exports = validateSchema;
17 changes: 15 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ import {
YieldExpression
} from "estree";
import { Stats as FsStats, WriteStream } from "fs";
import { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema";
import { default as ValidationError } from "schema-utils/declarations/ValidationError";
import {
Extend,
ValidationErrorConfiguration
} from "schema-utils/declarations/validate";
import {
AsArray,
AsyncParallelHook,
Expand Down Expand Up @@ -8604,6 +8609,10 @@ declare abstract class RuntimeValue {
fileDependencies: any;
exec(parser?: any): any;
}
type Schema =
| (JSONSchema4 & Extend)
| (JSONSchema6 & Extend)
| (JSONSchema7 & Extend);
declare interface ScopeInfo {
definitions: StackedMap<string, ScopeInfo | VariableInfo>;
topLevelScope: boolean | "arrow";
Expand Down Expand Up @@ -9568,7 +9577,7 @@ declare interface UpdateHashContextGenerator {
chunkGraph: ChunkGraph;
runtime: RuntimeSpec;
}
type UsageStateType = 0 | 1 | 2 | 3 | 4;
type UsageStateType = 0 | 2 | 3 | 1 | 4;
declare interface UserResolveOptions {
/**
* A list of module alias configurations or an object which maps key to value
Expand Down Expand Up @@ -10196,7 +10205,11 @@ declare namespace exports {
): MultiCompiler;
};
export const validate: (options?: any) => void;
export const validateSchema: (schema?: any, options?: any) => void;
export const validateSchema: (
schema: Schema,
options: {} | {}[],
validationConfiguration?: ValidationErrorConfiguration
) => void;
export const version: string;
export namespace cli {
export let getArguments: (schema?: any) => Record<string, Argument>;
Expand Down