mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-11-23 01:54:37 +03:00
Add EnvValidationFn user facing type
This commit is contained in:
parent
f0ffeec041
commit
c46f96145b
@ -1,7 +1,7 @@
|
||||
{{={= =}=}}
|
||||
import * as z from 'zod'
|
||||
|
||||
import { ensureEnvSchema } from '../env/index.js'
|
||||
import { ensureEnvSchema } from '../env/validation.js'
|
||||
|
||||
{=# envValidationFn.isDefined =}
|
||||
{=& envValidationFn.importStatement =}
|
||||
|
@ -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
waspc/data/Generator/templates/sdk/wasp/env/validation.ts
vendored
Normal file
28
waspc/data/Generator/templates/sdk/wasp/env/validation.ts
vendored
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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. =}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{{={= =}=}}
|
||||
import * as z from 'zod'
|
||||
|
||||
import { ensureEnvSchema } from '../env/index.js'
|
||||
import { ensureEnvSchema } from '../env/validation.js'
|
||||
|
||||
{=# envValidationFn.isDefined =}
|
||||
{=& envValidationFn.importStatement =}
|
||||
|
@ -1,13 +1,14 @@
|
||||
import * as z from 'zod'
|
||||
import type { EnvValidationFn } from 'wasp/env'
|
||||
|
||||
export const serverEnvValidationFn = () =>
|
||||
export const serverEnvValidationFn: EnvValidationFn = () =>
|
||||
z.object({
|
||||
MY_ENV_VAR: z.string({
|
||||
required_error: 'MY_ENV_VAR is required.',
|
||||
}),
|
||||
})
|
||||
|
||||
export const clientEnvValidationFn = () =>
|
||||
export const clientEnvValidationFn: EnvValidationFn = () =>
|
||||
z.object({
|
||||
REACT_APP_NAME: z.string().default('TODO App'),
|
||||
})
|
||||
|
@ -29,7 +29,8 @@ genEnvValidation spec =
|
||||
sequence
|
||||
[ genServerEnv spec,
|
||||
genClientEnv spec,
|
||||
genFileCopy [relfile|env/index.ts|]
|
||||
genFileCopy [relfile|env/index.ts|],
|
||||
genFileCopy [relfile|env/validation.ts|]
|
||||
]
|
||||
where
|
||||
genFileCopy = return . C.mkTmplFd
|
||||
|
Loading…
Reference in New Issue
Block a user