Skip to content

Commit

Permalink
Zod env validation WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Mihovil Ilakovac <mihovil@ilakovac.com>
  • Loading branch information
infomiho committed Oct 25, 2024
1 parent 03258a4 commit db0e709
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 2 deletions.
11 changes: 11 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/client/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as z from 'zod'

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

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

export const env = ensureEnvSchema(import.meta.env, serverEnvSchema)
17 changes: 17 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as z from 'zod'

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) {
throw new Error(e.errors.map((error) => error.message).join('\n'))
} else {
throw e
}
}
}
2 changes: 2 additions & 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,8 @@
"./client/test": "./dist/client/test/index.js",
"./client": "./dist/client/index.js",
"./dev": "./dist/dev/index.js",
"./client/env": "./dist/client/env/index.js",
"./server/env": "./dist/server/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
19 changes: 19 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/server/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as z from 'zod'

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

const serverEnvSchema = z.object({
NODE_ENV: z.enum(['development', 'production']).default('development'),
PORT: z.coerce.number().default(3000),
SERVER_URL: z.string({
required_error: 'SERVER_URL is required',
}),
CLIENT_URL: z.string({
required_error: 'CLIENT_URL is required',
}),
JWT_SECRET: z.string({
required_error: 'JWT_SECRET is required',
}),
})

export const env = ensureEnvSchema(process.env, serverEnvSchema)
11 changes: 10 additions & 1 deletion waspc/examples/todoApp/package-lock.json

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

1 change: 1 addition & 0 deletions waspc/examples/todoApp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSocket } from 'wasp/client/webSocket'
import { Link } from 'wasp/client/router'
import { logout, useAuth } from 'wasp/client/auth'
import { useQuery, getDate } from 'wasp/client/operations'
import { env } from 'wasp/client/env'

import './Main.css'
import { getName } from './user'
Expand Down
5 changes: 4 additions & 1 deletion waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import qualified Wasp.Generator.SdkGenerator.Client.OperationsGenerator as Clien
import Wasp.Generator.SdkGenerator.Client.RouterGenerator (genNewClientRouterApi)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.CrudG (genCrud)
import Wasp.Generator.SdkGenerator.EnvValidation (depsRequiredByEnvValidation, genEnvValidation)
import Wasp.Generator.SdkGenerator.Server.AuthG (genNewServerApi)
import Wasp.Generator.SdkGenerator.Server.CrudG (genNewServerCrudApi)
import Wasp.Generator.SdkGenerator.Server.EmailSenderG (depsRequiredByEmail, genNewEmailSenderApi)
Expand Down Expand Up @@ -125,6 +126,7 @@ genSdk spec =
<++> genNewEmailSenderApi spec
<++> genNewJobsApi spec
<++> genNewClientRouterApi spec
<++> genEnvValidation spec
where
genFileCopy = return . C.mkTmplFd

Expand Down Expand Up @@ -217,7 +219,8 @@ npmDepsForSdk spec =
-- These deps need to be installed in the SDK becasue when we run client tests,
-- we are running them from the project root dir and PostCSS and Tailwind
-- can't be resolved from WebApp node_modules, so we need to install them in the SDK.
++ depsRequiredByTailwind spec,
++ depsRequiredByTailwind spec
++ depsRequiredByEnvValidation,
N.devDependencies =
AS.Dependency.fromList
[ ("@tsconfig/node" <> majorNodeVersionStr, "latest"),
Expand Down
28 changes: 28 additions & 0 deletions waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Wasp.Generator.SdkGenerator.EnvValidation
( genEnvValidation,
depsRequiredByEnvValidation,
)
where

import StrongPath (relfile)
import Wasp.AppSpec (AppSpec)
import qualified Wasp.AppSpec.App.Dependency as AS.Dependency
import Wasp.Generator.FileDraft (FileDraft)
import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.SdkGenerator.Common as C

genEnvValidation :: AppSpec -> Generator [FileDraft]
genEnvValidation _spec =
sequence
[ genFileCopy [relfile|env/index.ts|],
genFileCopy [relfile|client/env/index.ts|],
genFileCopy [relfile|server/env/index.ts|]
]
where
genFileCopy = return . C.mkTmplFd

depsRequiredByEnvValidation :: [AS.Dependency.Dependency]
depsRequiredByEnvValidation =
AS.Dependency.fromList
[ ("zod", "^3.23.8")
]
1 change: 1 addition & 0 deletions waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ library
Wasp.Generator.SdkGenerator.Common
Wasp.Generator.SdkGenerator.CrudG
Wasp.Generator.SdkGenerator.EmailSender.Providers
Wasp.Generator.SdkGenerator.EnvValidation
Wasp.Generator.SdkGenerator.Server.AuthG
Wasp.Generator.SdkGenerator.Server.OAuthG
Wasp.Generator.SdkGenerator.Server.CrudG
Expand Down

0 comments on commit db0e709

Please sign in to comment.