Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show proper validation error message when user inputs old path in redirect section #5082

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "@webstudio-is/design-system";
import { ArrowRightIcon, TrashIcon } from "@webstudio-is/icons";
import {
PagePath,
OldPagePath,
PageRedirect,
ProjectNewRedirectPath,
} from "@webstudio-is/sdk";
Expand Down Expand Up @@ -58,7 +58,7 @@ export const SectionRedirects = () => {
};

const validateOldPath = (oldPath: string): string[] => {
const oldPathValidationResult = PagePath.safeParse(oldPath);
const oldPathValidationResult = OldPagePath.safeParse(oldPath);

if (oldPathValidationResult.success === true) {
if (oldPath.startsWith("/") === true) {
Expand Down
17 changes: 12 additions & 5 deletions packages/sdk/src/schema/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ const HomePage = z.object({
path: HomePagePath,
});

export const PagePath = z
const DefaultPagePage = z
.string()
.refine((path) => path !== "", "Can't be empty")
.refine((path) => path !== "/", "Can't be just a /")
.refine(
(path) => path === "" || path.startsWith("/"),
"Must start with a / or a full URL e.g. https://website.org"
)
.refine((path) => path.endsWith("/") === false, "Can't end with a /")
.refine((path) => path.includes("//") === false, "Can't contain repeating /")
.refine(
Expand All @@ -114,6 +110,17 @@ export const PagePath = z
"/build prefix is reserved for the system"
);

export const PagePath = DefaultPagePage.refine(
(path) => path === "" || path.startsWith("/"),
"Must start with a / or a full URL e.g. https://website.org"
);

// added this for old path input under redirect section of page settings
export const OldPagePath = DefaultPagePage.refine(
(path) => path === "" || path.startsWith("/"),
"Must start with a / and it must be full path e.g. /project/id"
);

const Page = z.object({
...commonPageFields,
path: PagePath,
Expand Down