fix(custom-widgets): keep URL errors inline

This commit is contained in:
Thomas Camlong committed 2026-07-20 19:11:58 +02:00
1 parent dc63dc750f
commit c1fca4c5a2
7 files changed
+151 -111

No files matched your search

@@ -61,6 +61,7 @@ export const customWidgetDefinitionSchema = z
.string()
.url()
.refine((value) => {
if (!URL.canParse(value)) return false;
const url = new URL(value);
return (url.protocol === "http:" || url.protocol === "https:") && !url.username && !url.password;
}, "Widget icons must use HTTP or HTTPS without embedded credentials")
@@ -44,7 +44,7 @@ export function validateCredentialFreeExport(definition: SecurityDefinition, ctx
if (credentialTextPattern.test(definition.template)) {
ctx.addIssue({ code: "custom", path: ["template"], message: "Templates cannot contain literal credentials" });
}
if (definition.iconUrl) {
if (definition.iconUrl && URL.canParse(definition.iconUrl)) {
const icon = new URL(definition.iconUrl);
if ([...icon.searchParams].some(([key, value]) => isCredentialKeyName(key) && value.trim().length > 0)) {
ctx.addIssue({ code: "custom", path: ["iconUrl"], message: "Widget icon URLs cannot contain credentials" });
@@ -120,6 +120,24 @@ const authSchema = z.discriminatedUnion("type", [
}),
]);
export type CustomWidgetSourceUrlIssue = "invalid" | "protocol" | "credentials" | "queryOrFragment";
export function getCustomWidgetSourceUrlIssue(value: string): CustomWidgetSourceUrlIssue | null {
if (!URL.canParse(value)) return "invalid";
const url = new URL(value);
if (url.protocol !== "http:" && url.protocol !== "https:") return "protocol";
if (url.username || url.password) return "credentials";
if (url.search || url.hash) return "queryOrFragment";
return null;
}
const sourceUrlIssueMessages: Record<CustomWidgetSourceUrlIssue, string> = {
invalid: "Must be a valid URL",
protocol: "API sources must use HTTP or HTTPS",
credentials: "Credentials must not be embedded in an API source URL",
queryOrFragment: "API source URLs cannot contain a query string or fragment",
};
export const customWidgetSourceSchema = z.object({
id: customWidgetIdentifierSchema,
name: z.string().trim().min(1).max(128),
@@ -128,16 +146,8 @@ export const customWidgetSourceSchema = z.object({
.url()
.max(2048)
.superRefine((value, ctx) => {
const url = new URL(value);
if (url.protocol !== "http:" && url.protocol !== "https:") {
ctx.addIssue({ code: "custom", message: "API sources must use HTTP or HTTPS" });
}
if (url.username || url.password) {
ctx.addIssue({ code: "custom", message: "Credentials must not be embedded in an API source URL" });
}
if (url.search || url.hash) {
ctx.addIssue({ code: "custom", message: "API source URLs cannot contain a query string or fragment" });
}
const issue = getCustomWidgetSourceUrlIssue(value);
if (issue && issue !== "invalid") ctx.addIssue({ code: "custom", message: sourceUrlIssueMessages[issue] });
}),
networkScope: z.enum(customJsxNetworkScopes),
auth: authSchema,