
Security News
Deno Goes the Way of the Dinosaur
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.
schema-utils
Advanced tools
Package for validate options in loaders and plugins.
To begin, you'll need to install schema-utils:
npm install schema-utils
schema.json
{
"type": "object",
"properties": {
"option": {
"type": "boolean"
}
},
"additionalProperties": false
}
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";
const options = { option: true };
const configuration = { name: "Loader Name/Plugin Name/Name" };
validate(schema, options, configuration);
schemaType: String
JSON schema.
Simple example of schema:
{
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
optionsType: Object
Object with options.
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";
const options = { foo: "bar" };
validate(schema, { name: 123 }, { name: "MyPlugin" });
configurationAllow to configure validator.
There is an alternative method to configure the name andbaseDataPath options via the title property in the schema.
For example:
{
"title": "My Loader options",
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
The last word used for the baseDataPath option, other words used for the name option.
Based on the example above the name option equals My Loader, the baseDataPath option equals options.
nameType: Object
Default: "Object"
Allow to setup name in validation errors.
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";
const options = { foo: "bar" };
validate(schema, options, { name: "MyPlugin" });
Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
- configuration.optionName should be a integer.
baseDataPathType: String
Default: "configuration"
Allow to setup base data path in validation errors.
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";
const options = { foo: "bar" };
validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
- options.optionName should be a integer.
postFormatterType: Function
Default: undefined
Allow to reformat errors.
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";
const options = { foo: "bar" };
validate(schema, options, {
name: "MyPlugin",
postFormatter: (formattedError, error) => {
if (error.keyword === "type") {
return `${formattedError}\nAdditional Information.`;
}
return formattedError;
},
});
Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
- options.optionName should be a integer.
Additional Information.
schema.json
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"test": {
"anyOf": [
{ "type": "array" },
{ "type": "string" },
{ "instanceof": "RegExp" }
]
},
"transform": {
"instanceof": "Function"
},
"sourceMap": {
"type": "boolean"
}
},
"additionalProperties": false
}
Loaderimport schema from "path/to/schema.json";
import { getOptions } from "loader-utils";
import { validate } from "schema-utils";
function loader(src, map) {
const options = getOptions(this);
validate(schema, options, {
name: "Loader Name",
baseDataPath: "options",
});
// Code...
}
export default loader;
Pluginimport schema from "path/to/schema.json";
import { validate } from "schema-utils";
class Plugin {
constructor(options) {
validate(schema, options, {
name: "Plugin Name",
baseDataPath: "options",
});
this.options = options;
}
apply(compiler) {
// Code...
}
}
export default Plugin;
Compiling a schema at runtime loads ajv, which costs about 150ms for the first schema of a
process and a few to tens of milliseconds for every other one. A loader or a plugin can compile its
schema ahead of time instead, into a check that validate takes as its fourth argument:
npx schema-utils precompile src/options.json
This writes src/options.check.js next to the schema. Commit it, and publish it with the package:
import { validate } from "schema-utils";
import check from "./options.check.js";
import schema from "./options.json";
validate(schema, options, { name: "Loader Name" }, check);
When the options are valid, the check is all that runs and ajv is never loaded. When they are
not, validate runs the full validation to report what is wrong, so the errors are the same as
without a check.
Run schema-utils precompile --check src/options.json in CI to fail when the check is missing or
out of date with its schema. The generated module needs schema-utils as a dependency of the
package and is CommonJS.
A relative $ref to another file resolves against the schema. To compile a schema from code, use
precompile(schema, { loadSchema }), it returns the source of the module.
validate function do nothing)This can be useful when you don't want to do validation for production builds.
import { disableValidation, enableValidation, validate } from "schema-utils";
// Disable validation
disableValidation();
// Do nothing
validate(schema, options);
// Enable validation
enableValidation();
// Will throw an error if schema is not valid
validate(schema, options);
// Allow to undestand do you need validation or not
const need = needValidate();
console.log(need);
Also you can enable/disable validation using the process.env.SKIP_VALIDATION env variable.
Supported values (case insensitive):
yes/y/true/1/onno/n/false/0/offThe variable is read when schema-utils is loaded, so set it before starting the process:
SKIP_VALIDATION=y webpack
Use enableValidation()/disableValidation() to change it while the process is running - they
take effect immediately and apply to every copy of schema-utils in the process.
Please take a moment to read our contributing guidelines if you haven't yet done so.
The package itself runs on Node.js 10.13.0 and above, but the dev dependencies require an active LTS release, so use one to work on it. CI installs older ones to run the tests on the older versions of Node.js the package supports.
Ajv is a JSON schema validator that is fast and supports draft-06/07/2019 JSON Schema. It is more feature-rich and supports more JSON Schema features compared to schema-utils, which is more focused on webpack-related configuration validation.
Joi is an object schema validation library that can validate objects based on a schema with a fluent API. It is different from schema-utils in that it is not specifically tied to JSON Schema and provides a more expressive way to define validation rules and constraints.
Yup is a JavaScript schema builder for value parsing and validation. It defines a schema using a declarative API and is often used in the context of form validation. Unlike schema-utils, Yup is not specifically designed for webpack configurations and offers a different API for schema construction.
FAQs
webpack Validation Utils
The npm package schema-utils receives a total of 0 weekly downloads. As such, schema-utils popularity was classified as not popular.
We found that schema-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.

Security News
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.

Security News
Anthropic’s free OSS Scanner uses Mythos and other Claude models to find open source vulnerabilities and send maintainers reports with proposed fixes.

Security News
On Front Lines’ Early Adopters: CISO podcast, Andrew Becherer shares lessons from Datadog, weighs the long-term costs of AI-built tools, and explains how founders can actually gain a CISO’s trust.