Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(lib/data): ReplacementsSchema #31714

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/config/presets/internal/replacements.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import replacementGroups from '../../../data/replacements.json';
import replacementGroupsJson from '../../../data/replacements.json';
import type { Preset } from '../types';
import type { PresetTemplate, Replacement } from './auto-generate-replacements';
import { addPresets } from './auto-generate-replacements';

const { $schema, ...replacementPresets } = replacementGroupsJson;

/* eslint sort-keys: ["error", "asc", {"caseSensitive": false, "natural": true}] */
export const presets: Record<string, Preset> = replacementGroups;
export const presets: Record<string, Preset> = replacementPresets;

const muiReplacement: Replacement[] = [
[['@material-ui/codemod'], '@mui/codemod'],
Expand Down
1 change: 1 addition & 0 deletions lib/data/replacements.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tools/schemas/replacements-schema.json",
"all": {
"description": "Apply crowd-sourced package replacement rules.",
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion tools/schemas/monorepo-schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "https://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"repoGroups": {
Expand Down
61 changes: 61 additions & 0 deletions tools/schemas/replacements-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"$schema": "https://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"all": {
"type": "object",
"properties": {
"description": { "type": "string" },
"extends": {
"type": "array",
"items": { "type": "string" }
},
"ignoreDeps": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["description", "extends"]
}
},
"patternProperties": {
"^[a-zA-Z0-9-]+$": {
"type": "object",
"properties": {
"description": { "type": "string" },
"packageRules": {
"type": "array",
"items": {
"type": "object",
"properties": {
"matchCurrentVersion": { "type": "string" },
"matchDatasources": {
"type": "array",
"items": { "type": "string" }
},
"matchPackageNames": {
"type": "array",
"items": { "type": "string" }
},
"replacementName": { "type": "string" },
"replacementVersion": { "type": "string" },
"description": { "type": "string" },
"replacementNameTemplate": { "type": "string" }
},
"required": ["matchDatasources", "matchPackageNames"]
},
"contains": {
"type": "object",
"oneOf": [
{ "required": ["replacementName"] },
{ "required": ["replacementNameTemplate"] }
]
},
"minItems": 1
}
},
"required": ["description", "packageRules"]
}
},
"additionalProperties": false
}
44 changes: 43 additions & 1 deletion tools/schemas/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,46 @@ const MonorepoSchema = z.object({
patternGroups: UrlSchema,
});

export { MonorepoSchema };
const PackageRuleSchema = z.object({
matchCurrentVersion: z.string().optional(),
matchDatasources: z.array(z.string()),
matchPackageNames: z.array(z.string()),
replacementName: z.string().optional(),
replacementVersion: z.string().optional(),
description: z.string().optional(),
replacementNameTemplate: z.string().optional(),
});

const RuleSetSchema = z.object({
description: z.string(),
packageRules: z
.array(PackageRuleSchema)
.min(1)
.refine(
(rules) =>
rules.some(
(rule) =>
rule.replacementName !== undefined ||
rule.replacementNameTemplate !== undefined,
),
{
message:
'At least one package rule must use either the replacementName config option, or the replacementNameTemplate config option',
secustor marked this conversation as resolved.
Show resolved Hide resolved
},
),
});

const AllSchema = z.object({
description: z.string(),
extends: z.array(z.string()),
ignoreDeps: z.array(z.string()).optional(),
});

const ReplacementsSchema = z
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
.object({
$schema: z.string(),
all: AllSchema,
})
.catchall(RuleSetSchema);

export { MonorepoSchema, ReplacementsSchema };
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved