Skip to content

Commit 12f6072

Browse files
authored
fix: Add validation for a route's id not being 'root' (#13792)
1 parent 55a876e commit 12f6072

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.changeset/tender-snails-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-router/dev": patch
3+
---
4+
5+
Add validation for a route's id not being 'root'

packages/react-router-dev/__tests__/route-config-test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ describe("route config", () => {
7676
`);
7777
});
7878

79+
it("is invalid it uses the 'root' id", () => {
80+
let result = validateRouteConfig({
81+
routeConfigFile: "routes.ts",
82+
routeConfig: [route("/", "root.tsx", { id: "root" })],
83+
});
84+
85+
expect(result.valid).toBe(false);
86+
expect(!result.valid && result.message).toMatchInlineSnapshot(`
87+
"Route config in "routes.ts" is invalid.
88+
89+
Path: routes.0.id
90+
A route cannot use the reserved id 'root'."
91+
`);
92+
});
93+
7994
it("is invalid when property is wrong type", () => {
8095
let result = validateRouteConfig({
8196
routeConfigFile: "routes.ts",

packages/react-router-dev/config/routes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ export const routeConfigEntrySchema: v.BaseSchema<
112112
);
113113
}, "Invalid type: Expected object but received a promise. Did you forget to await?"),
114114
v.object({
115-
id: v.optional(v.string()),
115+
id: v.optional(
116+
v.pipe(
117+
v.string(),
118+
v.notValue("root", "A route cannot use the reserved id 'root'.")
119+
)
120+
),
116121
path: v.optional(v.string()),
117122
index: v.optional(v.boolean()),
118123
caseSensitive: v.optional(v.boolean()),

0 commit comments

Comments
 (0)