Skip to content

Commit a54295c

Browse files
committed
feat(models): do not throw if docs url invalid, treat as missing and log warning
1 parent 29a1bc6 commit a54295c

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

packages/models/src/lib/audit.unit.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ describe('auditSchema', () => {
2323
).not.toThrow();
2424
});
2525

26-
it('should throw for an invalid URL', () => {
27-
expect(() =>
26+
it('should ignore invalid docs URL', () => {
27+
expect(
2828
auditSchema.parse({
2929
slug: 'consistent-test-it',
3030
title: 'Use a consistent test function.',
3131
docsUrl: 'invalid-url',
3232
} satisfies Audit),
33-
).toThrow('Invalid url');
33+
).toEqual<Audit>({
34+
slug: 'consistent-test-it',
35+
title: 'Use a consistent test function.',
36+
docsUrl: '',
37+
});
3438
});
3539
});
3640

packages/models/src/lib/implementation/schemas.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,21 @@ export const urlSchema = z.string().url();
5353
/** Schema for a docsUrl */
5454
export const docsUrlSchema = urlSchema
5555
.optional()
56-
.or(z.literal(''))
57-
.describe('Documentation site'); // allow empty string (no URL validation)
56+
.or(z.literal('')) // allow empty string (no URL validation)
57+
// eslint-disable-next-line unicorn/prefer-top-level-await, unicorn/catch-error-name
58+
.catch(ctx => {
59+
// if only URL validation fails, supress error since this metadata is optional anyway
60+
if (
61+
ctx.error.errors.length === 1 &&
62+
ctx.error.errors[0]?.code === 'invalid_string' &&
63+
ctx.error.errors[0].validation === 'url'
64+
) {
65+
console.warn(`Ignoring invalid docsUrl: ${ctx.input}`);
66+
return '';
67+
}
68+
throw ctx.error;
69+
})
70+
.describe('Documentation site');
5871

5972
/** Schema for a title of a plugin, category and audit */
6073
export const titleSchema = z

packages/models/src/lib/implementation/schemas.unit.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22
import {
33
type TableCellValue,
4+
docsUrlSchema,
45
tableCellValueSchema,
56
weightSchema,
67
} from './schemas.js';
@@ -34,3 +35,32 @@ describe('weightSchema', () => {
3435
expect(() => weightSchema.parse(-1)).toThrow('too_small');
3536
});
3637
});
38+
39+
describe('docsUrlSchema', () => {
40+
it('should accept a valid URL', () => {
41+
expect(() =>
42+
docsUrlSchema.parse(
43+
'https://eslint.org/docs/latest/rules/no-unused-vars',
44+
),
45+
).not.toThrow();
46+
});
47+
48+
it('should accept an empty string', () => {
49+
expect(() => docsUrlSchema.parse('')).not.toThrow();
50+
});
51+
52+
it('should tolerate invalid URL - treat as missing and log warning', () => {
53+
expect(
54+
docsUrlSchema.parse(
55+
'/home/user/project/tools/eslint-rules/rules/my-custom-rule.ts',
56+
),
57+
).toBe('');
58+
expect(console.warn).toHaveBeenCalledWith(
59+
'Ignoring invalid docsUrl: /home/user/project/tools/eslint-rules/rules/my-custom-rule.ts',
60+
);
61+
});
62+
63+
it('should throw if not a string', () => {
64+
expect(() => docsUrlSchema.parse(false)).toThrow('invalid_type');
65+
});
66+
});

0 commit comments

Comments
 (0)