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

Enable users to define their env var validations #2363

Draft
wants to merge 5 commits into
base: miho-zod-env
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions waspc/data/Generator/templates/sdk/wasp/client/env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
{{={= =}=}}
import * as z from 'zod'

import { ensureEnvSchema } from '../env/index.js'
import { ensureEnvSchema } from '../env/validation.js'

const clientEnvSchema = z.object({
{=# envValidationFn.isDefined =}
{=& envValidationFn.importStatement =}
const userClientEnvSchema = {= envValidationFn.importIdentifier =}()
{=/ envValidationFn.isDefined =}
{=^ envValidationFn.isDefined =}
const userClientEnvSchema = z.object({})
{=/ envValidationFn.isDefined =}

const waspClientEnvSchema = z.object({
REACT_APP_API_URL: z
.string({
required_error: 'REACT_APP_API_URL is required',
})
.default('{= defaultServerUrl =}')
})

const clientEnvSchema = waspClientEnvSchema.merge(userClientEnvSchema)

export const env = ensureEnvSchema(import.meta.env, clientEnvSchema)
30 changes: 2 additions & 28 deletions waspc/data/Generator/templates/sdk/wasp/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
import * as z from 'zod'
import type { ZodObject } from 'zod'

const redColor = '\x1b[31m'

export function ensureEnvSchema<Schema extends z.ZodTypeAny>(
data: unknown,
schema: Schema
): z.infer<Schema> {
try {
return schema.parse(data)
} catch (e) {
// TODO: figure out how to output the error message in a better way
if (e instanceof z.ZodError) {
console.error()
console.error(redColor, '╔═════════════════════════════╗');
console.error(redColor, '║ Env vars validation failed ║');
console.error(redColor, '╚═════════════════════════════╝');
console.error()
for (const error of e.errors) {
console.error(redColor, `- ${error.message}`)
}
console.error()
console.error(redColor, '═══════════════════════════════');
throw new Error('Error parsing environment variables')
} else {
throw e
}
}
}
export type EnvValidationFn = () => ZodObject<any>
28 changes: 28 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/env/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as z from 'zod'

const redColor = '\x1b[31m'

export function ensureEnvSchema<Schema extends z.ZodTypeAny>(
data: unknown,
schema: Schema
): z.infer<Schema> {
try {
return schema.parse(data)
} catch (e) {
if (e instanceof z.ZodError) {
console.error()
console.error(redColor, '╔═════════════════════════════╗');
console.error(redColor, '║ Env vars validation failed ║');
console.error(redColor, '╚═════════════════════════════╝');
console.error()
for (const error of e.errors) {
console.error(redColor, `- ${error.message}`)
}
console.error()
console.error(redColor, '═══════════════════════════════');
throw new Error('Error parsing environment variables')
} else {
throw e
}
}
}
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"./client/test": "./dist/client/test/index.js",
"./client": "./dist/client/index.js",
"./dev": "./dist/dev/index.js",
"./env": "./dist/env/index.js",

{=! todo(filip): Fixes below are for type errors in 0.13.1, remove ASAP =}
{=! Used by our code (SDK for full-stack type safety), uncodumented (but accessible) for users. =}
Expand Down
13 changes: 11 additions & 2 deletions waspc/data/Generator/templates/sdk/wasp/server/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{{={= =}=}}
import * as z from 'zod'

import { ensureEnvSchema } from '../env/index.js'
import { ensureEnvSchema } from '../env/validation.js'

const serverCommonSchema = z.object({
{=# envValidationFn.isDefined =}
{=& envValidationFn.importStatement =}
const userServerEnvSchema = {= envValidationFn.importIdentifier =}()
{=/ envValidationFn.isDefined =}
{=^ envValidationFn.isDefined =}
const userServerEnvSchema = z.object({})
{=/ envValidationFn.isDefined =}

const waspServerCommonSchema = z.object({
PORT: z.coerce.number().default({= defaultServerPort =}),
{= databaseUrlEnvVarName =}: z.string({
required_error: '{= databaseUrlEnvVarName =} is required',
Expand Down Expand Up @@ -133,6 +141,7 @@ const serverProdSchema = z.object({
{=/ isAuthEnabled =}
})

const serverCommonSchema = waspServerCommonSchema.merge(userServerEnvSchema)
const serverEnvSchema = z.discriminatedUnion('NODE_ENV', [
serverDevSchema.merge(serverCommonSchema),
serverProdSchema.merge(serverCommonSchema)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PgBoss from 'pg-boss'
import { env } from '../../../env.js'
import { config } from '../../../index.js'
import { config, env } from '../../../index.js'

const boss = createPgBoss()

Expand Down
8 changes: 8 additions & 0 deletions waspc/e2e-test/test-outputs/waspBuild-golden/files.manifest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading