Skip to content
Merged
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
35 changes: 35 additions & 0 deletions src/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,45 @@ import type {
} from "./types";
import { wrapAction } from "./wrap-action";

const ROUTE_SEGMENT_RE = /^[a-z0-9_-]+$/;

function assertValidRoutePath(path: string): void {
if (path.trim() !== path) {
throw new Error("Path cannot have leading or trailing whitespace");
}

if (path.length === 0) {
throw new Error("Path cannot be empty");
}

if (!path.startsWith("/")) {
throw new Error("Path must start with /");
}

if (path === "/") {
return;
}

if (path.endsWith("/")) {
throw new Error("Path must not end with / (except root /)");
}

if (path.includes("//")) {
throw new Error("Path must not contain consecutive slashes");
}

if (path !== path.toLowerCase()) {
throw new Error("Path must be lowercase");
}

const segments = path.split("/").slice(1);
for (const segment of segments) {
if (!ROUTE_SEGMENT_RE.test(segment)) {
throw new Error(
"Each path segment may only contain lowercase letters, numbers, hyphens, and underscores"
);
}
}
}

export async function createDocumentAction(
Expand Down
Loading