Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

schema-utils

0.4.5 • Public • Published

npm node deps test coverage chat

Schema Utils

Install

npm i schema-utils

Usage

validateOptions

schema.json

{
  "type": "object",
  "properties": {
    // Options...
  },
  "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')
 
  // Code...
}

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) {
    // Code...
  }
}

Maintainers


Juho Vepsäläinen

Joshua Wiens

Michael Ciniawsky

Keywords

none

install

npm i schema-utils

Downloadsweekly downloads

3,269,023

version

0.4.5

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
  • avatar