File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,10 +23,45 @@ import type {
2323} from "./types" ;
2424import { wrapAction } from "./wrap-action" ;
2525
26+ const ROUTE_SEGMENT_RE = / ^ [ a - z 0 - 9 _ - ] + $ / ;
27+
2628function assertValidRoutePath ( path : string ) : void {
29+ if ( path . trim ( ) !== path ) {
30+ throw new Error ( "Path cannot have leading or trailing whitespace" ) ;
31+ }
32+
33+ if ( path . length === 0 ) {
34+ throw new Error ( "Path cannot be empty" ) ;
35+ }
36+
2737 if ( ! path . startsWith ( "/" ) ) {
2838 throw new Error ( "Path must start with /" ) ;
2939 }
40+
41+ if ( path === "/" ) {
42+ return ;
43+ }
44+
45+ if ( path . endsWith ( "/" ) ) {
46+ throw new Error ( "Path must not end with / (except root /)" ) ;
47+ }
48+
49+ if ( path . includes ( "//" ) ) {
50+ throw new Error ( "Path must not contain consecutive slashes" ) ;
51+ }
52+
53+ if ( path !== path . toLowerCase ( ) ) {
54+ throw new Error ( "Path must be lowercase" ) ;
55+ }
56+
57+ const segments = path . split ( "/" ) . slice ( 1 ) ;
58+ for ( const segment of segments ) {
59+ if ( ! ROUTE_SEGMENT_RE . test ( segment ) ) {
60+ throw new Error (
61+ "Each path segment may only contain lowercase letters, numbers, hyphens, and underscores"
62+ ) ;
63+ }
64+ }
3065}
3166
3267export async function createDocumentAction (
You can’t perform that action at this time.
0 commit comments