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: trim spaces in strings for names #2527

Merged
merged 10 commits into from
Oct 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const formSchema = z.object({
keyId: z.string(),
name: z
.string()
.trim()
.transform((e) => (e === "" ? undefined : e))
.optional(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const formSchema = z.object({
keyId: z.string(),
ownerId: z
.string()
.trim()
.transform((e) => (e === "" ? undefined : e))
.optional(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ const formSchema = z.object({
.default(16),
prefix: z
.string()
.trim()
.max(8, { message: "Please limit the prefix to under 8 characters." })
.optional(),
ownerId: z.string().optional(),
name: z.string().optional(),
ownerId: z.string().trim().optional(),
name: z.string().trim().optional(),
metaEnabled: z.boolean().default(false),
meta: z
.string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { z } from "zod";
const formSchema = z.object({
name: z.string(),
name: z.string().trim().min(3, "Name is required and should be at least 3 characters"),
apiId: z.string(),
workspaceId: z.string(),
});
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/(app)/apis/create-api-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

const formSchema = z.object({
name: z.string().min(2).max(50),
name: z.string().trim().min(3, "Name must be at least 3 characters long").max(50),
});

export const CreateApiButton = ({ ...rest }: React.ButtonHTMLAttributes<HTMLButtonElement>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ type Props = {
};

const formSchema = z.object({
name: z.string(),
name: z
.string()
.min(3)
.regex(/^[a-zA-Z0-9_:\-\.\*]+$/, {
message:
"Must be at least 3 characters long and only contain alphanumeric, colons, periods, dashes and underscores",
}),
description: z.string().optional(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

const formSchema = z.object({
identifier: z.string().min(2).max(250),
identifier: z
.string()
.trim()
.min(3, "Name is required and should be at least 3 characters")
.max(250),
limit: z.coerce.number().int().min(1).max(10_000),
duration: z.coerce
.number()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { z } from "zod";
const formSchema = z.object({
name: z.string(),
name: z
.string()
.trim()
.min(3)
.max(50)
.regex(/^[a-zA-Z0-9_\-\.]+$/, {
message:
"Name must be 3-50 characters long and can only contain letters, numbers, underscores, hyphens, and periods.",
}),
namespaceId: z.string(),
workspaceId: z.string(),
});
Expand Down Expand Up @@ -53,7 +61,7 @@ export const UpdateNamespaceName: React.FC<Props> = ({ namespace }) => {
});
async function onSubmit(values: z.infer<typeof formSchema>) {
if (values.name === namespace.name || !values.name) {
return toast.error("Please provide a valid name before saving.");
return toast.error("Please provide a different name before saving.");
}
await updateName.mutateAsync(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

const formSchema = z.object({
name: z.string().regex(/^[a-zA-Z0-9_\-\.]{3,50}$/),
name: z
.string()
.trim()
.min(1, "Name must not be empty")
.max(50, "Name must not exceed 50 characters")
.regex(
/^[a-zA-Z0-9_\-\.]+$/,
"Only alphanumeric characters, underscores, hyphens, and periods are allowed",
),
});

export const CreateNamespaceButton = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const validCharactersRegex = /^[a-zA-Z0-9-_]+$/;

const formSchema = z.object({
workspaceId: z.string(),
name: z.string().min(3).regex(validCharactersRegex, {
name: z.string().trim().min(3).regex(validCharactersRegex, {
message: "Workspace can only contain letters, numbers, dashes, and underscores",
}),
});
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/new/create-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { z } from "zod";
const formSchema = z.object({
name: z.string().min(3, "Name is required and should be at least 3 characters").max(50),
name: z.string().trim().min(3, "Name is required and should be at least 3 characters").max(50),
});

type Props = {
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/new/create-workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useForm } from "react-hook-form";
import { z } from "zod";

const formSchema = z.object({
name: z.string().min(3, "Name is required and should be at least 3 characters").max(50),
name: z.string().trim().min(3, "Name is required and should be at least 3 characters").max(50),
});

export const CreateWorkspace: React.FC = () => {
Expand Down
Loading