|
1 | 1 | import { MATERIAL_ICONS } from 'vscode-material-icons'; |
2 | | -import { |
3 | | - ZodError, |
4 | | - type ZodIssue, |
5 | | - type ZodObject, |
6 | | - type ZodOptional, |
7 | | - type ZodString, |
8 | | - z, |
9 | | -} from 'zod'; |
| 2 | +import { type ZodObject, type ZodOptional, type ZodString, z } from 'zod'; |
10 | 3 | import { |
11 | 4 | MAX_DESCRIPTION_LENGTH, |
12 | 5 | MAX_SLUG_LENGTH, |
@@ -65,28 +58,24 @@ export const descriptionSchema = z |
65 | 58 | .optional(); |
66 | 59 |
|
67 | 60 | /* Schema for a URL */ |
68 | | -export const urlSchema = z.string().url().meta({ title: 'URL' }); |
| 61 | +export const urlSchema = z.url().meta({ title: 'URL' }); |
69 | 62 |
|
70 | 63 | /** Schema for a docsUrl */ |
71 | | -export const docsUrlSchema = urlSchema |
| 64 | +export const docsUrlSchema = z |
| 65 | + .union([ |
| 66 | + z.literal(''), // allow empty string (no URL validation) |
| 67 | + // eslint-disable-next-line unicorn/prefer-top-level-await, unicorn/catch-error-name |
| 68 | + urlSchema.optional().catch(ctx => { |
| 69 | + const issue = ctx.issues[0]; |
| 70 | + if (issue?.code === 'invalid_format' && issue?.format === 'url') { |
| 71 | + console.warn(`Ignoring invalid docsUrl: ${ctx.value}`); |
| 72 | + return ''; |
| 73 | + } |
| 74 | + // re-parse to throw formatted error for non-URL issues |
| 75 | + return urlSchema.parse(ctx.value); |
| 76 | + }), |
| 77 | + ]) |
72 | 78 | .optional() |
73 | | - .or(z.literal('')) // allow empty string (no URL validation) |
74 | | - // eslint-disable-next-line unicorn/prefer-top-level-await, unicorn/catch-error-name |
75 | | - .catch(ctx => { |
76 | | - // if only URL validation fails, supress error since this metadata is optional anyway |
77 | | - if ( |
78 | | - ctx.issues.length === 1 && |
79 | | - (ctx.issues[0]?.errors as ZodIssue[][]) |
80 | | - .flat() |
81 | | - .some( |
82 | | - error => error.code === 'invalid_format' && error.format === 'url', |
83 | | - ) |
84 | | - ) { |
85 | | - console.warn(`Ignoring invalid docsUrl: ${ctx.value}`); |
86 | | - return ''; |
87 | | - } |
88 | | - throw new ZodError(ctx.error.issues); |
89 | | - }) |
90 | 79 | .meta({ title: 'DocsUrl', description: 'Documentation site' }); |
91 | 80 |
|
92 | 81 | /** Schema for a title of a plugin, category and audit */ |
|
0 commit comments