What is schema-utils?
The schema-utils package is a utility library for validating options against a JSON Schema. It is commonly used in the context of webpack loaders and plugins to validate configuration objects. It provides functions to validate options, throw useful errors, and ensure that the configuration adheres to a specified schema.
What are schema-utils's main functionalities?
Validation of options
This feature allows users to validate an options object against a predefined JSON Schema. The 'validate' function takes a schema, options, and a configuration object that includes the name of the plugin or loader using the validation.
const { validate } = require('schema-utils');
const schema = {
type: 'object',
properties: {
name: {
type: 'string'
}
},
required: ['name']
};
const options = { name: 'Example' };
validate(schema, options, { name: 'MyPlugin' });
Custom error messages
This feature allows users to provide custom error messages in the schema that will be displayed when validation fails. The 'validate' function will throw an error with these messages if the options do not match the schema.
const { validate } = require('schema-utils');
const schema = {
type: 'object',
properties: {
age: {
type: 'number',
description: 'Age must be a number'
}
},
required: ['age']
};
const options = { age: 'old' };
try {
validate(schema, options, { name: 'AgeChecker' });
} catch (error) {
// Custom error handling
}
Other packages similar to schema-utils
ajv
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
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
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.
Schema Utils
Install
npm i schema-utils
Usage
validateOptions
schema.json
{
"type": "object",
"properties": {
},
"additionalProperties": false
}
import schema from 'path/to/schema.json'
import validateOptions from 'schema-utils'
validateOptions(schema, options, 'Loader/Plugin Name')
Examples
schema.json
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"test": {
"anyOf": [
{ "type": "array" },
{ "type": "string" },
{ "instanceof": "RegExp" }
]
},
"transform": {
"instanceof": "Function"
},
"sourceMap": {
"type": "boolean"
}
},
"additionalProperties": false
}
Loader
import { getOptions } from 'loader-utils'
import validateOptions from 'schema-utils'
import schema from 'path/to/schema.json'
function loader (src, map) {
const options = getOptions(this) || {}
validateOptions(schema, options, 'Loader Name')
}
Plugin
import validateOptions from 'schema-utils'
import schema from 'path/to/schema.json'
class Plugin {
constructor (options) {
validateOptions(schema, options, 'Plugin Name')
this.options = options
}
apply (compiler) {
}
}