Improve /complete-account/signup body schema

This commit is contained in:
carlosmonastyrski
2025-05-02 13:06:45 -03:00
parent e12f4ad253
commit 46755f724c
2 changed files with 18 additions and 21 deletions

View File

@@ -48,22 +48,3 @@ export const SecretNameSchema = BaseSecretNameSchema.refine(
)
.refine((el) => !el.includes(":"), "Secret name cannot contain colon.")
.refine((el) => !el.includes("/"), "Secret name cannot contain forward slash.");
const DefaultOrgSchema = z.object({
useDefaultOrg: z.literal(true)
});
const CustomOrgSchema = z.object({
useDefaultOrg: z.literal(false),
organizationName: GenericResourceNameSchema
});
export const OrganizationInputSchema = z.preprocess(
(data) => {
if (typeof data === "object" && data && "useDefaultOrg" in data === false) {
return { ...data, useDefaultOrg: false };
}
return data;
},
z.discriminatedUnion("useDefaultOrg", [DefaultOrgSchema, CustomOrgSchema])
);

View File

@@ -4,7 +4,7 @@ import { UsersSchema } from "@app/db/schemas";
import { getConfig } from "@app/lib/config/env";
import { ForbiddenRequestError } from "@app/lib/errors";
import { authRateLimit } from "@app/server/config/rateLimiter";
import { OrganizationInputSchema } from "@app/server/lib/schemas";
import { GenericResourceNameSchema } from "@app/server/lib/schemas";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
@@ -106,7 +106,23 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
attributionSource: z.string().trim().optional(),
password: z.string()
})
.and(OrganizationInputSchema),
.and(
z.preprocess(
(data) => {
if (typeof data === "object" && data && "useDefaultOrg" in data === false) {
return { ...data, useDefaultOrg: false };
}
return data;
},
z.discriminatedUnion("useDefaultOrg", [
z.object({ useDefaultOrg: z.literal(true) }),
z.object({
useDefaultOrg: z.literal(false),
organizationName: GenericResourceNameSchema
})
])
)
),
response: {
200: z.object({
message: z.string(),