-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EnvValidationFn user facing type
- Loading branch information
Showing
7 changed files
with
38 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters