Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions packages/models/src/lib/audit.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ describe('auditSchema', () => {
).not.toThrow();
});

it('should throw for an invalid URL', () => {
expect(() =>
it('should ignore invalid docs URL', () => {
expect(
auditSchema.parse({
slug: 'consistent-test-it',
title: 'Use a consistent test function.',
docsUrl: 'invalid-url',
} satisfies Audit),
).toThrow('Invalid url');
).toEqual<Audit>({
slug: 'consistent-test-it',
title: 'Use a consistent test function.',
docsUrl: '',
});
});
});

Expand Down
17 changes: 15 additions & 2 deletions packages/models/src/lib/implementation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ export const urlSchema = z.string().url();
/** Schema for a docsUrl */
export const docsUrlSchema = urlSchema
.optional()
.or(z.literal(''))
.describe('Documentation site'); // allow empty string (no URL validation)
.or(z.literal('')) // allow empty string (no URL validation)
// eslint-disable-next-line unicorn/prefer-top-level-await, unicorn/catch-error-name
.catch(ctx => {
// if only URL validation fails, supress error since this metadata is optional anyway
if (
ctx.error.errors.length === 1 &&
ctx.error.errors[0]?.code === 'invalid_string' &&
ctx.error.errors[0].validation === 'url'
) {
console.warn(`Ignoring invalid docsUrl: ${ctx.input}`);
return '';
}
throw ctx.error;
})
.describe('Documentation site');

/** Schema for a title of a plugin, category and audit */
export const titleSchema = z
Expand Down
30 changes: 30 additions & 0 deletions packages/models/src/lib/implementation/schemas.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
type TableCellValue,
docsUrlSchema,
tableCellValueSchema,
weightSchema,
} from './schemas.js';
Expand Down Expand Up @@ -34,3 +35,32 @@ describe('weightSchema', () => {
expect(() => weightSchema.parse(-1)).toThrow('too_small');
});
});

describe('docsUrlSchema', () => {
it('should accept a valid URL', () => {
expect(() =>
docsUrlSchema.parse(
'https://eslint.org/docs/latest/rules/no-unused-vars',
),
).not.toThrow();
});

it('should accept an empty string', () => {
expect(() => docsUrlSchema.parse('')).not.toThrow();
});

it('should tolerate invalid URL - treat as missing and log warning', () => {
expect(
docsUrlSchema.parse(
'/home/user/project/tools/eslint-rules/rules/my-custom-rule.ts',
),
).toBe('');
expect(console.warn).toHaveBeenCalledWith(
'Ignoring invalid docsUrl: /home/user/project/tools/eslint-rules/rules/my-custom-rule.ts',
);
});

it('should throw if not a string', () => {
expect(() => docsUrlSchema.parse(false)).toThrow('invalid_type');
});
});
Loading