Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor: update create org form validation (#8455)

Leo Weigand0531b57e1a77
diff --git a/web/src/features/organizations/components/NewOrganizationForm.tsx b/web/src/features/organizations/components/NewOrganizationForm.tsx
index 55cf6bde60..db0d9035d0 100644
--- a/web/src/features/organizations/components/NewOrganizationForm.tsx
+++ b/web/src/features/organizations/components/NewOrganizationForm.tsx
@@ -22,7 +22,7 @@ import {
} from "@/src/components/ui/select";
import { api } from "@/src/utils/api";
import { useSession } from "next-auth/react";
-import { organizationNameSchema } from "@/src/features/organizations/utils/organizationNameSchema";
+import { organizationFormSchema } from "@/src/features/organizations/utils/organizationNameSchema";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { SurveyName } from "@prisma/client";
import { env } from "@/src/env.mjs";
@@ -35,7 +35,7 @@ export const NewOrganizationForm = ({
const { update: updateSession } = useSession();
const form = useForm({
- resolver: zodResolver(organizationNameSchema),
+ resolver: zodResolver(organizationFormSchema),
defaultValues: {
name: "",
type: "Personal",
@@ -50,7 +50,7 @@ export const NewOrganizationForm = ({
const watchedType = form.watch("type");
const isCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);
- function onSubmit(values: z.infer<typeof organizationNameSchema>) {
+ function onSubmit(values: z.infer<typeof organizationFormSchema>) {
capture("organizations:new_form_submit");
createOrgMutation
.mutateAsync({
@@ -130,13 +130,10 @@ export const NewOrganizationForm = ({
<FormDescription>
What would best describe your organization?
</FormDescription>
- <Select
- onValueChange={field.onChange}
- defaultValue={field.value}
- >
+ <Select onValueChange={field.onChange} value={field.value}>
<FormControl>
- <SelectTrigger>
- <SelectValue placeholder="Select organization type" />
+ <SelectTrigger ref={field.ref}>
+ <SelectValue placeholder="Please choose" />
</SelectTrigger>
</FormControl>
<SelectContent>
@@ -162,13 +159,10 @@ export const NewOrganizationForm = ({
<FormDescription>
How many people are in your {watchedType}?
</FormDescription>
- <Select
- onValueChange={field.onChange}
- defaultValue={field.value}
- >
+ <Select onValueChange={field.onChange} value={field.value}>
<FormControl>
- <SelectTrigger>
- <SelectValue placeholder="Select organization size" />
+ <SelectTrigger ref={field.ref}>
+ <SelectValue placeholder="Please choose" />
</SelectTrigger>
</FormControl>
<SelectContent>
diff --git a/web/src/features/organizations/utils/organizationNameSchema.ts b/web/src/features/organizations/utils/organizationNameSchema.ts
index 11955850d7..4ea76b34bb 100644
--- a/web/src/features/organizations/utils/organizationNameSchema.ts
+++ b/web/src/features/organizations/utils/organizationNameSchema.ts
@@ -18,11 +18,29 @@ const organizationSizeOptions = [
"More than 300",
] as const;
+// Base schema for org creation, used for server-side validation too
export const organizationNameSchema = z.object({
name: StringNoHTML.min(3, "Must have at least 3 characters").max(
60,
"Must have at most 60 characters",
),
- type: z.enum(organizationTypeOptions).optional(),
- size: z.enum(organizationSizeOptions).optional(),
});
+
+// Extended schema for client-side form validation including type and size,
+// which are posted separately as a survey response.
+export const organizationFormSchema = organizationNameSchema
+ .extend({
+ type: z.enum(organizationTypeOptions),
+ size: z.enum(organizationSizeOptions).optional(),
+ })
+ .check((ctx) => {
+ const { type, size } = ctx.value;
+ if ((type === "Company" || type === "Agency") && !size) {
+ ctx.issues.push({
+ code: z.ZodIssueCode.custom,
+ path: ["size"],
+ input: ctx.value.size,
+ message: "Please specify the size of your organization",
+ });
+ }
+ });