1 | # jest-validate
|
2 |
|
3 | Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.
|
4 |
|
5 | ```bash
|
6 | npm install --save jest-validate
|
7 | ```
|
8 |
|
9 | ## Usage
|
10 |
|
11 | ```js
|
12 | import {validate} from 'jest-validate';
|
13 |
|
14 | validate(config, validationOptions); // => {hasDeprecationWarnings: boolean, isValid: boolean}
|
15 | ```
|
16 |
|
17 | Where `ValidationOptions` are:
|
18 |
|
19 | ```ts
|
20 | type ValidationOptions = {
|
21 | comment?: string;
|
22 | condition?: (option: unknown, validOption: unknown) => boolean;
|
23 | deprecate?: (
|
24 | config: Record<string, unknown>,
|
25 | option: string,
|
26 | deprecatedOptions: DeprecatedOptions,
|
27 | options: ValidationOptions,
|
28 | ) => boolean;
|
29 | deprecatedConfig?: DeprecatedOptions;
|
30 | error?: (
|
31 | option: string,
|
32 | received: unknown,
|
33 | defaultValue: unknown,
|
34 | options: ValidationOptions,
|
35 | path?: Array<string>,
|
36 | ) => void;
|
37 | exampleConfig: Record<string, unknown>;
|
38 | recursive?: boolean;
|
39 | recursiveBlacklist?: Array<string>;
|
40 | recursiveDenylist?: Array<string>;
|
41 | title?: Title;
|
42 | unknown?: (
|
43 | config: Record<string, unknown>,
|
44 | exampleConfig: Record<string, unknown>,
|
45 | option: string,
|
46 | options: ValidationOptions,
|
47 | path?: Array<string>,
|
48 | ) => void;
|
49 | };
|
50 |
|
51 | type Title = {
|
52 | deprecation?: string;
|
53 | error?: string;
|
54 | warning?: string;
|
55 | };
|
56 | ```
|
57 |
|
58 | `exampleConfig` is the only option required.
|
59 |
|
60 | ## API
|
61 |
|
62 | 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:
|
63 |
|
64 | Almost anything can be overwritten to suite your needs.
|
65 |
|
66 | ### Options
|
67 |
|
68 | - `recursiveDenylist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
|
69 | - `comment` – optional string to be rendered below error/warning message.
|
70 | - `condition` – an optional function with validation condition.
|
71 | - `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages.
|
72 | - `deprecatedConfig` – optional object with deprecated config keys.
|
73 | - `exampleConfig` – the only **required** option with configuration against which you'd like to test.
|
74 | - `recursive` - optional boolean determining whether recursively compare `exampleConfig` to `config` (default: `true`).
|
75 | - `title` – optional object of titles for errors and messages.
|
76 |
|
77 | You will find examples of `condition`, `deprecate`, `error`, `unknown`, and `deprecatedConfig` inside source of this repository, named respectively.
|
78 |
|
79 | ## exampleConfig syntax
|
80 |
|
81 | `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:
|
82 |
|
83 | - it matches the JavaScript type of the example value, e.g. `string`, `number`, `array`, `boolean`, `function`, or `object`
|
84 | - it is `null` or `undefined`
|
85 | - it matches the Javascript type of any of arguments passed to `MultipleValidOptions(...)`
|
86 |
|
87 | 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.
|
88 |
|
89 | ## Examples
|
90 |
|
91 | Minimal example:
|
92 |
|
93 | ```js
|
94 | validate(config, {exampleConfig});
|
95 | ```
|
96 |
|
97 | Example with slight modifications:
|
98 |
|
99 | ```js
|
100 | validate(config, {
|
101 | comment: ' Documentation: http://custom-docs.com',
|
102 | deprecatedConfig,
|
103 | exampleConfig,
|
104 | title: {
|
105 | deprecation: 'Custom Deprecation',
|
106 | // leaving 'error' and 'warning' as default
|
107 | },
|
108 | });
|
109 | ```
|
110 |
|
111 | This will output:
|
112 |
|
113 | #### Warning:
|
114 |
|
115 | ```bash
|
116 | ● Validation Warning:
|
117 |
|
118 | Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
|
119 | This is either a typing error or a user mistake. Fixing it will remove this message.
|
120 |
|
121 | Documentation: http://custom-docs.com
|
122 | ```
|
123 |
|
124 | #### Error:
|
125 |
|
126 | ```bash
|
127 | ● Validation Error:
|
128 |
|
129 | Option transform must be of type:
|
130 | object
|
131 | but instead received:
|
132 | string
|
133 |
|
134 | Example:
|
135 | {
|
136 | "transform": {
|
137 | "\\.js$": "<rootDir>/preprocessor.js"
|
138 | }
|
139 | }
|
140 |
|
141 | Documentation: http://custom-docs.com
|
142 | ```
|
143 |
|
144 | ## Example validating multiple types
|
145 |
|
146 | ```js
|
147 | import {multipleValidOptions} from 'jest-validate';
|
148 |
|
149 | validate(config, {
|
150 | // `bar` will accept either a string or a number
|
151 | bar: multipleValidOptions('string is ok', 2),
|
152 | });
|
153 | ```
|
154 |
|
155 | #### Error:
|
156 |
|
157 | ```bash
|
158 | ● Validation Error:
|
159 |
|
160 | Option foo must be of type:
|
161 | string or number
|
162 | but instead received:
|
163 | array
|
164 |
|
165 | Example:
|
166 | {
|
167 | "bar": "string is ok"
|
168 | }
|
169 |
|
170 | or
|
171 |
|
172 | {
|
173 | "bar": 2
|
174 | }
|
175 |
|
176 | Documentation: http://custom-docs.com
|
177 | ```
|
178 |
|
179 | #### Deprecation
|
180 |
|
181 | Based on `deprecatedConfig` object with proper deprecation messages. Note custom title:
|
182 |
|
183 | ```bash
|
184 | Custom Deprecation:
|
185 |
|
186 | Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.
|
187 |
|
188 | Jest now treats your current configuration as:
|
189 | {
|
190 | "transform": {".*": "xxx"}
|
191 | }
|
192 |
|
193 | Please update your configuration.
|
194 |
|
195 | Documentation: http://custom-docs.com
|
196 | ```
|
197 |
|
198 | ## Example validating CLI arguments
|
199 |
|
200 | ```js
|
201 | import {validate} from 'jest-validate';
|
202 |
|
203 | validateCLIOptions(argv, {...allowedOptions, deprecatedOptions});
|
204 | ```
|
205 |
|
206 | If `argv` contains a deprecated option that is not specifid in `allowedOptions`, `validateCLIOptions` will throw an error with the message specified in the `deprecatedOptions` config:
|
207 |
|
208 | ```bash
|
209 | ● collectCoverageOnlyFrom:
|
210 |
|
211 | Option "collectCoverageOnlyFrom" was replaced by "collectCoverageFrom"
|
212 |
|
213 | CLI Options Documentation: https://jestjs.io/docs/en/cli.html
|
214 | ```
|
215 |
|
216 | If the deprecation option is still listed in the `allowedOptions` config, then `validateCLIOptions` will print the warning wihout throwing an error.
|