Skip to content

Commit 88e1f18

Browse files
test(lib/data): ReplacementsSchema (#31714)
Co-authored-by: HonkingGoose <[email protected]>
1 parent f9ab074 commit 88e1f18

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

lib/config/presets/internal/replacements.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import replacementGroups from '../../../data/replacements.json';
1+
import replacementGroupsJson from '../../../data/replacements.json';
22
import type { Preset } from '../types';
33
import type { PresetTemplate, Replacement } from './auto-generate-replacements';
44
import { addPresets } from './auto-generate-replacements';
55

6+
const { $schema, ...replacementPresets } = replacementGroupsJson;
7+
68
/* eslint sort-keys: ["error", "asc", {"caseSensitive": false, "natural": true}] */
7-
export const presets: Record<string, Preset> = replacementGroups;
9+
export const presets: Record<string, Preset> = replacementPresets;
810

911
const muiReplacement: Replacement[] = [
1012
[['@material-ui/codemod'], '@mui/codemod'],

lib/data/replacements.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "../../tools/schemas/replacements-schema.json",
23
"all": {
34
"description": "Apply crowd-sourced package replacement rules.",
45
"extends": [

tools/schemas/monorepo-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema#",
2+
"$schema": "https://json-schema.org/draft-04/schema#",
33
"type": "object",
44
"properties": {
55
"repoGroups": {
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"$schema": "https://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"properties": {
5+
"all": {
6+
"type": "object",
7+
"properties": {
8+
"description": { "type": "string" },
9+
"extends": {
10+
"type": "array",
11+
"items": { "type": "string" }
12+
},
13+
"ignoreDeps": {
14+
"type": "array",
15+
"items": { "type": "string" }
16+
}
17+
},
18+
"required": ["description", "extends"]
19+
}
20+
},
21+
"patternProperties": {
22+
"^[a-zA-Z0-9-]+$": {
23+
"type": "object",
24+
"properties": {
25+
"description": { "type": "string" },
26+
"packageRules": {
27+
"type": "array",
28+
"items": {
29+
"type": "object",
30+
"properties": {
31+
"matchCurrentVersion": { "type": "string" },
32+
"matchDatasources": {
33+
"type": "array",
34+
"items": { "type": "string" }
35+
},
36+
"matchPackageNames": {
37+
"type": "array",
38+
"items": { "type": "string" }
39+
},
40+
"replacementName": { "type": "string" },
41+
"replacementVersion": { "type": "string" },
42+
"description": { "type": "string" },
43+
"replacementNameTemplate": { "type": "string" }
44+
},
45+
"required": ["matchDatasources", "matchPackageNames"]
46+
},
47+
"contains": {
48+
"type": "object",
49+
"oneOf": [
50+
{ "required": ["replacementName"] },
51+
{ "required": ["replacementNameTemplate"] }
52+
]
53+
},
54+
"minItems": 1
55+
}
56+
},
57+
"required": ["description", "packageRules"]
58+
}
59+
},
60+
"additionalProperties": false
61+
}

tools/schemas/schema.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,50 @@ const UrlSchema = z.record(
55
z.union([z.string(), z.array(z.string())]),
66
);
77

8-
const MonorepoSchema = z.object({
8+
export const MonorepoSchema = z.object({
99
repoGroups: UrlSchema,
1010
orgGroups: UrlSchema,
1111
patternGroups: UrlSchema,
1212
});
1313

14-
export { MonorepoSchema };
14+
const PackageRuleSchema = z.object({
15+
matchCurrentVersion: z.string().optional(),
16+
matchDatasources: z.array(z.string()),
17+
matchPackageNames: z.array(z.string()),
18+
replacementName: z.string().optional(),
19+
replacementVersion: z.string().optional(),
20+
description: z.string().optional(),
21+
replacementNameTemplate: z.string().optional(),
22+
});
23+
24+
const RuleSetSchema = z.object({
25+
description: z.string(),
26+
packageRules: z
27+
.array(PackageRuleSchema)
28+
.min(1)
29+
.refine(
30+
(rules) =>
31+
rules.some(
32+
(rule) =>
33+
rule.replacementName !== undefined ||
34+
rule.replacementNameTemplate !== undefined,
35+
),
36+
{
37+
message:
38+
'At least one package rule must use either the replacementName config option, or the replacementNameTemplate config option',
39+
},
40+
),
41+
});
42+
43+
const AllSchema = z.object({
44+
description: z.string(),
45+
extends: z.array(z.string()),
46+
ignoreDeps: z.array(z.string()).optional(),
47+
});
48+
49+
export const ReplacementsSchema = z
50+
.object({
51+
$schema: z.string(),
52+
all: AllSchema,
53+
})
54+
.catchall(RuleSetSchema);

0 commit comments

Comments
 (0)