fix(custom-widgets): keep URL errors inline
This commit is contained in:
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,
|
||||
|
||||
@@ -16,6 +16,15 @@ describe("Custom JSX v2 validation", () => {
|
||||
expect(result.success, result.error?.issues.map((issue) => issue.message).join("; ")).toBe(true);
|
||||
});
|
||||
|
||||
test.each(["q", "http://["])("reports an incomplete URL inline without throwing: %s", (value) => {
|
||||
const source = { ...CUSTOM_WIDGET_STARTER.sources[0], baseUrl: value };
|
||||
|
||||
expect(() => customWidgetDefinitionSchema.safeParse({ ...CUSTOM_WIDGET_STARTER, sources: [source] })).not.toThrow();
|
||||
expect(customWidgetDefinitionSchema.safeParse({ ...CUSTOM_WIDGET_STARTER, sources: [source] }).success).toBe(false);
|
||||
expect(() => customWidgetDefinitionSchema.safeParse({ ...CUSTOM_WIDGET_STARTER, iconUrl: value })).not.toThrow();
|
||||
expect(customWidgetDefinitionSchema.safeParse({ ...CUSTOM_WIDGET_STARTER, iconUrl: value }).success).toBe(false);
|
||||
});
|
||||
|
||||
test("keeps the bundled seed set stable, disabled-ready, and credential-free", () => {
|
||||
expect(BUNDLED_CUSTOM_WIDGETS.map(({ id }) => id)).toEqual([
|
||||
"seed-dog-facts",
|
||||
|
||||
Reference in new issue
Block a user