Skip to content

Commit 194701f

Browse files
authored
Add support for recursiveDenylist option as an alternative to recursiveBlacklist (#10649)
1 parent 33eab98 commit 194701f

File tree

7 files changed

+60
-15
lines changed

7 files changed

+60
-15
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-validate]` Add support for `recursiveDenylist` option as an alternative to `recursiveBlacklist` ([#10236](https://github.com/facebook/jest/pull/10236))
6+
57
### Fixes
68

79
### Chore & Maintenance

packages/jest-validate/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Almost anything can be overwritten to suite your needs.
6262

6363
### Options
6464

65-
- `recursiveBlacklist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
65+
- `recursiveDenylist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
6666
- `comment` – optional string to be rendered below error/warning message.
6767
- `condition` – an optional function with validation condition.
6868
- `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages.

packages/jest-validate/src/__tests__/validate.test.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ test.each([
101101
},
102102
);
103103

104-
test('respects blacklist', () => {
104+
test('respects recursiveBlacklist', () => {
105105
const warn = console.warn;
106106
console.warn = jest.fn();
107107
const config = {
@@ -135,6 +135,40 @@ test('respects blacklist', () => {
135135
console.warn = warn;
136136
});
137137

138+
test('respects recursiveDenylist', () => {
139+
const warn = console.warn;
140+
console.warn = jest.fn();
141+
const config = {
142+
something: {
143+
nested: {
144+
some_random_key: 'value',
145+
some_random_key2: 'value2',
146+
},
147+
},
148+
};
149+
const exampleConfig = {
150+
something: {
151+
nested: {
152+
test: true,
153+
},
154+
},
155+
};
156+
157+
validate(config, {exampleConfig});
158+
159+
expect(console.warn).toBeCalled();
160+
161+
console.warn.mockReset();
162+
163+
validate(config, {
164+
exampleConfig,
165+
recursiveDenylist: ['something.nested'],
166+
});
167+
168+
expect(console.warn).not.toBeCalled();
169+
console.warn = warn;
170+
});
171+
138172
test('displays warning for unknown config options', () => {
139173
const config = {unkwon: {}};
140174
const validConfig = {unknown: 'string'};
@@ -292,7 +326,7 @@ test('Comments in config JSON using "//" key are not warned', () => {
292326

293327
validate(config, {
294328
exampleConfig: validConfig,
295-
recursiveBlacklist: ['myCustomKey' as "don't validate this"],
329+
recursiveDenylist: ['myCustomKey' as "don't validate this"],
296330
});
297331
expect(console.warn).not.toBeCalled();
298332

packages/jest-validate/src/defaultConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const validationOptions: ValidationOptions = {
2222
exampleConfig: {},
2323
recursive: true,
2424
// Allow NPM-sanctioned comments in package.json. Use a "//" key.
25-
recursiveBlacklist: ['//'],
25+
recursiveDenylist: ['//'],
2626
title: {
2727
deprecation: DEPRECATION,
2828
error: ERROR,

packages/jest-validate/src/exampleConfig.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const config: ValidationOptions = {
1717
error: () => {},
1818
exampleConfig: {key: 'value', test: 'case'},
1919
recursive: true,
20-
recursiveBlacklist: [],
20+
recursiveDenylist: [],
2121
title: {
2222
deprecation: 'Deprecation Warning',
2323
error: 'Validation Error',

packages/jest-validate/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type ValidationOptions = {
3535
exampleConfig: Record<string, unknown>;
3636
recursive?: boolean;
3737
recursiveBlacklist?: Array<string>;
38+
recursiveDenylist?: Array<string>;
3839
title?: Title;
3940
unknown?: (
4041
config: Record<string, unknown>,

packages/jest-validate/src/validate.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ let hasDeprecationWarnings = false;
1414
const shouldSkipValidationForPath = (
1515
path: Array<string>,
1616
key: string,
17-
blacklist?: Array<string>,
18-
) => (blacklist ? blacklist.includes([...path, key].join('.')) : false);
17+
denylist?: Array<string>,
18+
) => (denylist ? denylist.includes([...path, key].join('.')) : false);
1919

2020
const _validate = (
2121
config: Record<string, any>,
@@ -70,7 +70,11 @@ const _validate = (
7070
options.error(key, config[key], exampleConfig[key], options, path);
7171
}
7272
} else if (
73-
shouldSkipValidationForPath(path, key, options.recursiveBlacklist)
73+
shouldSkipValidationForPath(
74+
path,
75+
key,
76+
options.recursiveDenylist || options.recursiveBlacklist,
77+
)
7478
) {
7579
// skip validating unknown options inside blacklisted paths
7680
} else {
@@ -81,8 +85,12 @@ const _validate = (
8185
if (
8286
options.recursive &&
8387
!Array.isArray(exampleConfig[key]) &&
84-
options.recursiveBlacklist &&
85-
!shouldSkipValidationForPath(path, key, options.recursiveBlacklist)
88+
(options.recursiveDenylist || options.recursiveBlacklist) &&
89+
!shouldSkipValidationForPath(
90+
path,
91+
key,
92+
options.recursiveDenylist || options.recursiveBlacklist,
93+
)
8694
) {
8795
_validate(config[key], exampleConfig[key], options, [...path, key]);
8896
}
@@ -101,16 +109,16 @@ const validate = (
101109
): {hasDeprecationWarnings: boolean; isValid: boolean} => {
102110
hasDeprecationWarnings = false;
103111

104-
// Preserve default blacklist entries even with user-supplied blacklist
105-
const combinedBlacklist: Array<string> = [
106-
...(defaultConfig.recursiveBlacklist || []),
107-
...(options.recursiveBlacklist || []),
112+
// Preserve default denylist entries even with user-supplied denylist
113+
const combinedDenylist: Array<string> = [
114+
...(defaultConfig.recursiveDenylist || []),
115+
...(options.recursiveDenylist || options.recursiveBlacklist || []),
108116
];
109117

110118
const defaultedOptions: ValidationOptions = Object.assign({
111119
...defaultConfig,
112120
...options,
113-
recursiveBlacklist: combinedBlacklist,
121+
recursiveDenylist: combinedDenylist,
114122
title: options.title || defaultConfig.title,
115123
});
116124

0 commit comments

Comments
 (0)