next-zodenv
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -1,4 +0,9 @@ | ||
import { ZodIssue, z } from 'zod'; | ||
import { z, ZodIssue } from 'zod'; | ||
declare type Environment = Record<string, string | undefined>; | ||
declare type ZodType<TOut = unknown, TIn = unknown> = z.ZodType<TOut, z.ZodTypeDef, TIn>; | ||
declare type Schema = Record<string, ZodType>; | ||
declare type ParsedSchema<Validators extends Schema> = { | ||
[K in keyof Validators]: z.infer<Validators[K]>; | ||
}; | ||
declare type ZodErrors = Record<string, ZodIssue[]>; | ||
@@ -20,4 +25,4 @@ declare type ZenvOptions = { | ||
declare function zenv<EnvVar extends z.ZodRawShape>(validators: z.ZodObject<EnvVar>, { nextPublic, env, reporter, }?: ZenvOptions): z.infer<typeof validators>; | ||
declare function zenv<Validators extends Schema>(validators: Validators, { nextPublic, env, reporter, }?: ZenvOptions): ParsedSchema<Validators>; | ||
export { bool, defaultReporter, email, num, port, str, url, zenv }; |
@@ -67,4 +67,3 @@ "use strict"; | ||
const errors = {}; | ||
for (const key in validators.shape) { | ||
const validator = validators.shape[key]; | ||
for (const [key, validator] of Object.entries(validators)) { | ||
const value = nextPublic[key] ?? env[key]; | ||
@@ -71,0 +70,0 @@ const resolved = validator.safeParse(value); |
{ | ||
"name": "next-zodenv", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Shinya Fujino <shf0811@gamil.com> (https://github.com/morinokami)", | ||
@@ -5,0 +5,0 @@ "description": "Safe environment variables for Next.js, powered by Zod", |
@@ -35,7 +35,7 @@ # next-zodenv | ||
const env = zenv(z.object({ | ||
const env = zenv({ | ||
FOO: z.string(), | ||
PORT: z.preprocess(Number, z.number().int().gte(1).lte(65535)), | ||
API_URL: z.string().url(), | ||
})) | ||
}) | ||
@@ -54,9 +54,8 @@ env.FOO // string | ||
import { zenv, str, port, url } from 'next-zodenv' | ||
import { z } from 'zod' | ||
const env = zenv(z.object({ | ||
const env = zenv({ | ||
FOO: str(), | ||
PORT: port(), | ||
API_URL: url(), | ||
})) | ||
}) | ||
``` | ||
@@ -77,9 +76,9 @@ | ||
In order to expose environment variables to the browser in Next.js, you need to pass the `nextPublic` property to `zenv` like this: | ||
In order to expose environment variables to the browser in Next.js, you need to pass the `nextPublic` option to `zenv` like this: | ||
```ts | ||
const env = zenv( | ||
z.object({ | ||
{ | ||
NEXT_PUBLIC_VAR: z.string(), | ||
}), | ||
}, | ||
{ | ||
@@ -86,0 +85,0 @@ nextPublic: { |
Sorry, the diff of this file is not supported yet
9465
164
99