jest-validate
Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.
Last updated a month ago by davidzilburg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jest-validate 
SYNC missed versions from official npm registry.

jest-validate

Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.

npm install --save jest-validate

Usage

import {validate} from 'jest-validate';

validate((config: Object), (options: ValidationOptions)); // => {hasDeprecationWarnings: boolean, isValid: boolean}

Where ValidationOptions are:

type ValidationOptions = {
  blacklist?: Array<string>,
  comment?: string,
  condition?: (option: any, validOption: any) => boolean,
  deprecate?: (
    config: Object,
    option: string,
    deprecatedOptions: Object,
    options: ValidationOptions,
  ) => true,
  deprecatedConfig?: {[key: string]: Function},
  error?: (
    option: string,
    received: any,
    defaultValue: any,
    options: ValidationOptions,
  ) => void,
  exampleConfig: Object,
  recursive?: boolean,
  title?: Title,
  unknown?: (
    config: Object,
    exampleConfig: Object,
    option: string,
    options: ValidationOptions,
  ) => void,
};

type Title = {|
  deprecation?: string,
  error?: string,
  warning?: string,
|};

exampleConfig is the only option required.

API

By default jest-validate will print generic warning and error messages. You can however customize this behavior by providing options: ValidationOptions object as a second argument:

Almost anything can be overwritten to suite your needs.

Options

  • recursiveBlacklist – optional array of string keyPaths that should be excluded from deep (recursive) validation.
  • comment – optional string to be rendered below error/warning message.
  • condition – an optional function with validation condition.
  • deprecate, error, unknown – optional functions responsible for displaying warning and error messages.
  • deprecatedConfig – optional object with deprecated config keys.
  • exampleConfig – the only required option with configuration against which you'd like to test.
  • recursive - optional boolean determining whether recursively compare exampleConfig to config (default: true).
  • title – optional object of titles for errors and messages.

You will find examples of condition, deprecate, error, unknown, and deprecatedConfig inside source of this repository, named respectively.

exampleConfig syntax

exampleConfig should be an object with key/value pairs that contain an example of a valid value for each key. A configuration value is considered valid when:

  • it matches the JavaScript type of the example value, e.g. string, number, array, boolean, function, or object
  • it is null or undefined
  • it matches the Javascript type of any of arguments passed to MultipleValidOptions(...)

The last condition is a special syntax that allows validating where more than one type is permissible; see example below. It's acceptable to have multiple values of the same type in the example, so you can also use this syntax to provide more than one example. When a validation failure occurs, the error message will show all other values in the array as examples.

Examples

Minimal example:

validate(config, {exampleConfig});

Example with slight modifications:

validate(config, {
  comment: '  Documentation: http://custom-docs.com',
  deprecatedConfig,
  exampleConfig,
  title: {
    deprecation: 'Custom Deprecation',
    // leaving 'error' and 'warning' as default
  },
});

This will output:

Warning:

● Validation Warning:

  Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
  This is either a typing error or a user mistake. Fixing it will remove this message.

  Documentation: http://custom-docs.com

Error:

● Validation Error:

  Option transform must be of type:
    object
  but instead received:
    string

  Example:
  {
    "transform": {
      "^.+\\.js$": "<rootDir>/preprocessor.js"
    }
  }

  Documentation: http://custom-docs.com

Example validating multiple types

import {multipleValidOptions} from 'jest-validate';

validate(config, {
  // `bar` will accept either a string or a number
  bar: multipleValidOptions('string is ok', 2),
});

Error:

● Validation Error:

  Option foo must be of type:
    string or number
  but instead received:
    array

  Example:
  {
    "bar": "string is ok"
  }

  or

  {
    "bar": 2
  }

  Documentation: http://custom-docs.com

Deprecation

Based on deprecatedConfig object with proper deprecation messages. Note custom title:

Custom Deprecation:

  Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.

  Jest now treats your current configuration as:
  {
    "transform": {".*": "xxx"}
  }

  Please update your configuration.

  Documentation: http://custom-docs.com

Current Tags

  • 24.2.0-alpha.0                                ...           beta (a year ago)
  • 25.1.0                                ...           latest (a month ago)
  • 25.0.0                                ...           next (6 months ago)
  • 21.3.0-beta.15                                ...           test (2 years ago)

116 Versions

  • 25.1.0                                ...           a month ago
  • 25.0.0                                ...           6 months ago
  • 24.9.0                                ...           6 months ago
  • 24.8.0                                ...           10 months ago
  • 24.7.0                                ...           a year ago
  • 24.6.0                                ...           a year ago
  • 24.5.0                                ...           a year ago
  • 24.4.0                                ...           a year ago
  • 24.3.1                                ...           a year ago
  • 24.3.0                                ...           a year ago
  • 24.2.0-alpha.0                                ...           a year ago
  • 24.0.0                                ...           a year ago
  • 24.0.0-alpha.16                                ...           a year ago
  • 24.0.0-alpha.15                                ...           a year ago
  • 24.0.0-alpha.13                                ...           a year ago
  • 24.0.0-alpha.12                                ...           a year ago
  • 24.0.0-alpha.11                                ...           a year ago
  • 24.0.0-alpha.10                                ...           a year ago
  • 24.0.0-alpha.9                                ...           a year ago
  • 24.0.0-alpha.8                                ...           a year ago
  • 24.0.0-alpha.7                                ...           a year ago
  • 24.0.0-alpha.6                                ...           a year ago
  • 24.0.0-alpha.5                                ...           a year ago
  • 24.0.0-alpha.4                                ...           a year ago
  • 24.0.0-alpha.2                                ...           a year ago
  • 24.0.0-alpha.1                                ...           a year ago
  • 24.0.0-alpha.0                                ...           a year ago
  • 23.6.0                                ...           a year ago
  • 23.5.0                                ...           2 years ago
  • 23.4.0                                ...           2 years ago
  • 23.3.0                                ...           2 years ago
  • 23.2.0                                ...           2 years ago
  • 23.0.1                                ...           2 years ago
  • 23.0.0                                ...           2 years ago
  • 23.0.0-charlie.4                                ...           2 years ago
  • 23.0.0-charlie.3                                ...           2 years ago
  • 22.4.4                                ...           2 years ago
  • 23.0.0-charlie.2                                ...           2 years ago
  • 23.0.0-charlie.1                                ...           2 years ago
  • 23.0.0-charlie.0                                ...           2 years ago
  • 23.0.0-beta.3r                                ...           2 years ago
  • 23.0.0-alpha.3r                                ...           2 years ago
  • 23.0.0-beta.2                                ...           2 years ago
  • 23.0.0-beta.1                                ...           2 years ago
  • 23.0.0-beta.0                                ...           2 years ago
  • 23.0.0-alpha.7                                ...           2 years ago
  • 23.0.0-alpha.6r                                ...           2 years ago
  • 23.0.0-alpha.5r                                ...           2 years ago
  • 23.0.0-alpha.5                                ...           2 years ago
  • 23.0.0-alpha.4                                ...           2 years ago
  • 23.0.0-alpha.2                                ...           2 years ago
  • 22.4.3                                ...           2 years ago
  • 23.0.0-alpha.1                                ...           2 years ago
  • 23.0.0-alpha.0                                ...           2 years ago
  • 22.4.2                                ...           2 years ago
  • 22.4.1                                ...           2 years ago
  • 22.4.0                                ...           2 years ago
  • 22.2.2                                ...           2 years ago
  • 22.2.0                                ...           2 years ago
  • 22.1.2                                ...           2 years ago
  • 22.1.0                                ...           2 years ago
  • 22.0.6                                ...           2 years ago
  • 22.0.5                                ...           2 years ago
  • 22.0.3                                ...           2 years ago
  • 22.0.2                                ...           2 years ago
  • 22.0.1                                ...           2 years ago
  • 22.0.0                                ...           2 years ago
  • 21.3.0-beta.15                                ...           2 years ago
  • 21.3.0-beta.14                                ...           2 years ago
  • 21.3.0-beta.13                                ...           2 years ago
  • 21.3.0-beta.12                                ...           2 years ago
  • 21.3.0-beta.11                                ...           2 years ago
  • 21.3.0-beta.10                                ...           2 years ago
  • 21.3.0-beta.9                                ...           2 years ago
  • 21.3.0-beta.8                                ...           2 years ago
  • 21.3.0-beta.7                                ...           2 years ago
  • 21.3.0-beta.6                                ...           2 years ago
  • 21.3.0-beta.5                                ...           2 years ago
  • 21.3.0-beta.4                                ...           2 years ago
  • 21.3.0-beta.3                                ...           2 years ago
  • 21.3.0-beta.2                                ...           2 years ago
  • 21.3.0-beta.1                                ...           2 years ago
  • 21.3.0-alpha.eff7a1cf                                ...           2 years ago
  • 21.3.0-alpha.1e3ee68e                                ...           2 years ago
  • 21.2.1                                ...           2 years ago
  • 21.2.0                                ...           2 years ago
  • 21.1.0                                ...           2 years ago
  • 21.0.2                                ...           2 years ago
  • 21.0.0                                ...           2 years ago
  • 21.0.0-beta.1                                ...           3 years ago
  • 21.0.0-alpha.2                                ...           3 years ago
  • 21.0.0-alpha.1                                ...           3 years ago
  • 20.1.0-echo.1                                ...           3 years ago
  • 20.1.0-delta.5                                ...           3 years ago
  • 20.1.0-delta.4                                ...           3 years ago
  • 20.1.0-delta.3                                ...           3 years ago
  • 20.1.0-delta.2                                ...           3 years ago
  • 20.1.0-delta.1                                ...           3 years ago
  • 20.1.0-chi.1                                ...           3 years ago
  • 20.1.0-beta.1                                ...           3 years ago
  • 20.1.0-alpha.3                                ...           3 years ago
  • 20.1.0-alpha.2                                ...           3 years ago
  • 20.1.0-alpha.1                                ...           3 years ago
  • 20.0.3                                ...           3 years ago
  • 20.0.2                                ...           3 years ago
  • 20.0.1                                ...           3 years ago
  • 20.0.0                                ...           3 years ago
  • 19.3.0-alpha.85402254                                ...           3 years ago
  • 19.2.0-alpha.993e64af                                ...           3 years ago
  • 19.1.0-alpha.eed82034                                ...           3 years ago
  • 19.0.2                                ...           3 years ago
  • 19.0.0                                ...           3 years ago
  • 18.5.0-alpha.7da3df39                                ...           3 years ago
  • 18.2.0                                ...           3 years ago
  • 18.1.0                                ...           3 years ago
  • 0.0.0                                ...           3 years ago
Downloads
Today 11,429
This Week 11,429
This Month 199,253
Last Day 4,586
Last Week 66,030
Last Month 199,515
Dependencies (6)
Dev Dependencies (0)
None

Copyright 2014 - 2017 © taobao.org |