Skip to content

Commit 22ecfb0

Browse files
committed
refactor(models): use schema title from meta registry
1 parent d3bfaeb commit 22ecfb0

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

packages/core/src/lib/implementation/read-rc-file.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,24 @@ export class ConfigPathError extends Error {
1515
}
1616

1717
export async function readRcByPath(
18-
filepath: string,
18+
filePath: string,
1919
tsconfig?: string,
2020
): Promise<CoreConfig> {
21-
if (filepath.length === 0) {
21+
if (filePath.length === 0) {
2222
throw new Error('The path to the configuration file is empty.');
2323
}
2424

25-
if (!(await fileExists(filepath))) {
26-
throw new ConfigPathError(filepath);
25+
if (!(await fileExists(filePath))) {
26+
throw new ConfigPathError(filePath);
2727
}
2828

2929
const cfg: CoreConfig = await importModule({
30-
filepath,
30+
filepath: filePath,
3131
tsconfig,
3232
format: 'esm',
3333
});
3434

35-
return validate(coreConfigSchema, cfg, {
36-
schemaType: 'core config',
37-
sourcePath: filepath,
38-
});
35+
return validate(coreConfigSchema, cfg, { filePath });
3936
}
4037

4138
export async function autoloadRc(tsconfig?: string): Promise<CoreConfig> {
Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1-
import { bold } from 'ansis';
1+
import ansis from 'ansis';
22
import path from 'node:path';
3-
import { ZodError, z } from 'zod';
3+
import { ZodError, type ZodType, z } from 'zod';
44

55
type SchemaValidationContext = {
6-
schemaType: string;
7-
sourcePath?: string;
6+
filePath?: string;
87
};
98

109
export class SchemaValidationError extends Error {
1110
constructor(
12-
{ schemaType, sourcePath }: SchemaValidationContext,
1311
error: ZodError,
12+
schema: ZodType,
13+
{ filePath }: SchemaValidationContext,
1414
) {
1515
const formattedError = z.prettifyError(error);
16-
const pathDetails = sourcePath
17-
? ` in ${bold(path.relative(process.cwd(), sourcePath))}`
18-
: '';
19-
super(`Failed parsing ${schemaType}${pathDetails}.\n\n${formattedError}`);
16+
const schemaTitle = z.globalRegistry.get(schema)?.title;
17+
const summary = [
18+
'Invalid',
19+
schemaTitle ? ansis.bold(schemaTitle) : 'data',
20+
filePath &&
21+
`in ${ansis.bold(path.relative(process.cwd(), filePath))} file`,
22+
]
23+
.filter(Boolean)
24+
.join(' ');
25+
super(`${summary}\n${formattedError}\n`);
2026
}
2127
}
2228

23-
export function validate<T extends z.ZodTypeAny>(
29+
export function validate<T extends ZodType>(
2430
schema: T,
2531
data: z.input<T>,
26-
{ schemaType, sourcePath }: SchemaValidationContext,
32+
context: SchemaValidationContext = {},
2733
): z.output<T> {
28-
try {
29-
return schema.parse(data);
30-
} catch (error) {
31-
if (error instanceof ZodError) {
32-
throw new SchemaValidationError({ schemaType, sourcePath }, error);
33-
}
34-
throw error;
34+
const result = schema.safeParse(data);
35+
if (result.success) {
36+
return result.data;
3537
}
38+
throw new SchemaValidationError(result.error, schema, context);
3639
}

packages/plugin-eslint/src/lib/eslint-plugin.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,13 @@ export async function eslintPlugin(
3535
config: ESLintPluginConfig,
3636
options?: ESLintPluginOptions,
3737
): Promise<PluginConfig> {
38-
const targets = validate(eslintPluginConfigSchema, config, {
39-
schemaType: 'ESLint plugin config',
40-
});
38+
const targets = validate(eslintPluginConfigSchema, config);
4139

4240
const {
4341
groups: customGroups,
4442
artifacts,
4543
scoreTargets,
46-
} = options
47-
? validate(eslintPluginOptionsSchema, options, {
48-
schemaType: 'ESLint plugin options',
49-
})
50-
: {};
44+
} = options ? validate(eslintPluginOptionsSchema, options) : {};
5145

5246
const { audits, groups } = await listAuditsAndGroups(targets, customGroups);
5347

0 commit comments

Comments
 (0)