Big update!Announcing Socket for GitHub 1.0. Learn more
Socket
BlogLoveFAQ
Install
Log in

schema-utils

Package Overview
Dependencies
4
Maintainers
2
Versions
33
Issues
File Explorer

Advanced tools

schema-utils

webpack Validation Utils

    4.0.0latest

Version published
Maintainers
2
Weekly downloads
75,819,798
decreased by-0.11%

Weekly downloads

Changelog

Source

4.0.0 (2021-11-16)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is >= 12.13.0
  • update ajv to 8.8.0 version, please read internal changes, postFormatter require attention due some properties were changed
  • logic for formatExclusiveMaximum and formatExclusiveMinimum was changed (due usage ajv-formats package)

Readme

Source

npm node deps tests coverage chat size

schema-utils

Package for validate options in loaders and plugins.

Getting Started

To begin, you'll need to install schema-utils:

npm install schema-utils

API

schema.json

{ "type": "object", "properties": { "option": { "type": "boolean" } }, "additionalProperties": false } import schema from "./path/to/schema.json"; import { validate } from "schema-utils"; const options = { option: true }; const configuration = { name: "Loader Name/Plugin Name/Name" }; validate(schema, options, configuration);

schema

Type: String

JSON schema.

Simple example of schema:

{ "type": "object", "properties": { "name": { "description": "This is description of option.", "type": "string" } }, "additionalProperties": false }

options

Type: Object

Object with options.

import schema from "./path/to/schema.json"; import { validate } from "schema-utils"; const options = { foo: "bar" }; validate(schema, { name: 123 }, { name: "MyPlugin" });

configuration

Allow 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.

name

Type: Object Default: "Object"

Allow to setup name in validation errors.

import schema from "./path/to/schema.json"; import { validate } from "schema-utils"; 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.
baseDataPath

Type: String Default: "configuration"

Allow to setup base data path in validation errors.

import schema from "./path/to/schema.json"; import { validate } from "schema-utils"; 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.
postFormatter

Type: Function Default: undefined

Allow to reformat errors.

import schema from "./path/to/schema.json"; import { validate } from "schema-utils"; 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.

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 { validate } from "schema-utils"; import schema from "path/to/schema.json"; function loader(src, map) { const options = getOptions(this); validate(schema, options, { name: "Loader Name", baseDataPath: "options", }); // Code... } export default loader;

Plugin

import { validate } from "schema-utils"; import schema from "path/to/schema.json"; class Plugin { constructor(options) { validate(schema, options, { name: "Plugin Name", baseDataPath: "options", }); this.options = options; } apply(compiler) { // Code... } } export default Plugin;

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

Keywords

FAQs

What is schema-utils?

webpack Validation Utils

Is schema-utils popular?

The npm package schema-utils receives a total of 72,828,217 weekly downloads. As such, schema-utils popularity was classified as popular.

Is schema-utils well maintained?

We found that schema-utils demonstrated a healthy version release cadence and project activity. It has 2 open source maintainers collaborating on the project.

Last updated on 16 Nov 2021
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc