Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(env): replace conditionalRequiredSchema and requiredStorageC…
Browse files Browse the repository at this point in the history
…onfigSchema with requireIfEnvEnabled
luis-herasme committed Jan 28, 2025
1 parent 74ee125 commit 393cfe7
Showing 2 changed files with 19 additions and 34 deletions.
35 changes: 19 additions & 16 deletions packages/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { RefinementCtx } from "@blobscan/zod";
import {
z,
blobStorageSchema,
booleanSchema,
conditionalRequiredSchema,
createEnv,
presetEnvOptions,
nodeEnvSchema,
@@ -12,16 +12,15 @@ import {
prismaBatchOperationsMaxSizeSchema,
} from "@blobscan/zod";

export function requiredStorageConfigSchema<T extends z.ZodTypeAny>(
storageName: "FILE_SYSTEM" | "GOOGLE" | "POSTGRES" | "SWARM",
schema: T
) {
return conditionalRequiredSchema(
schema,
process.env[`${storageName}_STORAGE_ENABLED`],
"true",
`This configuration variable is required when ${storageName} storage is enabled.`
);
function requireIfEnvEnabled(envName: string) {
return (value: unknown, ctx: RefinementCtx) => {
if (process.env[envName] === "true" && value === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `This field is required when ${envName} is set to "true"`,
});
}
};

Check warning on line 23 in packages/env/index.ts

Codecov / codecov/patch

packages/env/index.ts#L17-L23

Added lines #L17 - L23 were not covered by tests
}

export const env = createEnv({
@@ -31,7 +30,11 @@ export const env = createEnv({
POSTHOG_ID: z.string().optional(),
POSTHOG_HOST: z.string().default("https://us.i.posthog.com"),

BEE_ENDPOINT: requiredStorageConfigSchema("SWARM", z.string().url()),
BEE_ENDPOINT: z
.string()
.url()
.optional()
.superRefine(requireIfEnvEnabled("SWARM_STORAGE_ENABLED")),
BLOBSCAN_API_BASE_URL: z
.string()
.url()
@@ -50,10 +53,10 @@ export const env = createEnv({
FILE_SYSTEM_STORAGE_ENABLED: booleanSchema.default("false"),
FILE_SYSTEM_STORAGE_PATH: z.string().default("/tmp/blobscan-blobs"),
GOOGLE_STORAGE_API_ENDPOINT: z.string().optional(),
GOOGLE_STORAGE_BUCKET_NAME: requiredStorageConfigSchema(
"GOOGLE",
z.string()
),
GOOGLE_STORAGE_BUCKET_NAME: z
.string()
.optional()
.superRefine(requireIfEnvEnabled("GOOGLE_STORAGE_ENABLED")),
GOOGLE_STORAGE_ENABLED: booleanSchema.default("false"),
GOOGLE_STORAGE_PROJECT_ID: z.string().optional(),
GOOGLE_SERVICE_KEY: z.string().optional(),
18 changes: 0 additions & 18 deletions packages/zod/src/schemas.ts
Original file line number Diff line number Diff line change
@@ -42,21 +42,3 @@ export const blobStorageSchema = z.enum([
"POSTGRES",
"SWARM",
] as const);

export function conditionalRequiredSchema<T extends z.ZodTypeAny>(
schema: T,
conditionalField?: string,
expectedValue?: string,
errorMessage?: string
) {
return schema.optional().refine(
(value) => {
const isConditionalFieldSet = conditionalField === expectedValue;

return !isConditionalFieldSet || !!value;
},
{
message: errorMessage,
}
);
}

0 comments on commit 393cfe7

Please sign in to comment.