File tree Expand file tree Collapse file tree 3 files changed +52
-5
lines changed Expand file tree Collapse file tree 3 files changed +52
-5
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,18 @@ describe('auditSchema', () => {
23
23
) . not . toThrow ( ) ;
24
24
} ) ;
25
25
26
- it ( 'should throw for an invalid URL' , ( ) => {
27
- expect ( ( ) =>
26
+ it ( 'should ignore invalid docs URL' , ( ) => {
27
+ expect (
28
28
auditSchema . parse ( {
29
29
slug : 'consistent-test-it' ,
30
30
title : 'Use a consistent test function.' ,
31
31
docsUrl : 'invalid-url' ,
32
32
} satisfies Audit ) ,
33
- ) . toThrow ( 'Invalid url' ) ;
33
+ ) . toEqual < Audit > ( {
34
+ slug : 'consistent-test-it' ,
35
+ title : 'Use a consistent test function.' ,
36
+ docsUrl : '' ,
37
+ } ) ;
34
38
} ) ;
35
39
} ) ;
36
40
Original file line number Diff line number Diff line change @@ -53,8 +53,21 @@ export const urlSchema = z.string().url();
53
53
/** Schema for a docsUrl */
54
54
export const docsUrlSchema = urlSchema
55
55
. 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' ) ;
58
71
59
72
/** Schema for a title of a plugin, category and audit */
60
73
export const titleSchema = z
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it } from 'vitest' ;
2
2
import {
3
3
type TableCellValue ,
4
+ docsUrlSchema ,
4
5
tableCellValueSchema ,
5
6
weightSchema ,
6
7
} from './schemas.js' ;
@@ -34,3 +35,32 @@ describe('weightSchema', () => {
34
35
expect ( ( ) => weightSchema . parse ( - 1 ) ) . toThrow ( 'too_small' ) ;
35
36
} ) ;
36
37
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments