-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathfindUppercaseKeys.ts
29 lines (25 loc) · 1.21 KB
/
findUppercaseKeys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { strictEqual } from 'node:assert/strict';
import { JsonMap, isJsonMap } from '@salesforce/ts-types';
import { Messages } from '../messages';
Messages.importMessagesDirectory(__dirname);
const coreMessages = Messages.loadMessages('@salesforce/core', 'core');
/** will throw on any upperCase unless they are present in the allowList. Recursively searches the object, returning valid keys */
export const ensureNoUppercaseKeys =
(path: string) =>
(allowList: string[] = []) =>
(data: JsonMap): string[] => {
const keys = getKeys(data, allowList);
const upperCaseKeys = keys.filter((key) => /^[A-Z]/.test(key)).join(', ');
strictEqual(upperCaseKeys.length, 0, coreMessages.getMessage('invalidJsonCasing', [upperCaseKeys, path]));
return keys;
};
const getKeys = (data: JsonMap, allowList: string[]): string[] =>
Object.entries(data)
.filter(([k]) => !allowList.includes(k))
.flatMap(([key, value]) => (isJsonMap(value) ? [key, ...getKeys(value, allowList)] : [key]));