Socket
Socket
Sign inDemoInstall

schema-utils

Package Overview
Dependencies
5
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    schema-utils

Webpack Schema Validation Utilities


Version published
Weekly downloads
82M
decreased by-1.63%
Maintainers
2
Install size
2.06 MB
Created
Weekly downloads
 

Package description

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

Changelog

Source

0.3.0 (2017-04-29)

Features

  • add ValidationError (#8) (d48f0fb)

<a name="0.2.1"></a>

Readme

Source

npm node deps test coverage chat

Schema Utils

Install

npm install --save schema-utils

Usage

validateOptions

import validateOptions from 'schema-utils'

validateOptions('path/to/schema.json', options, 'Loader/Plugin Name')

Examples

Loader

import { getOptions } from 'loader-utils'
import validateOptions from 'schema-utils'

function loader (src, map) {
  const options = getOptions(this) || {}

  validateOptions('path/to/schema.json', options, 'Loader Name')
}

Plugin

import Tapable from 'tapable'
import validateOptions from 'schema-utils'

class Plugin extends Tapable {
  constructor (options) {
    validateOptions('path/to/schema.json', options, 'Plugin Name')
  }
}

Maintainers


Juho Vepsäläinen

Joshua Wiens

Michael Ciniawsky

Keywords

FAQs

Last updated on 29 Apr 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc