Skip to content

Commit 8ddf07a

Browse files
committed
fix: don't stop validation when value is undefined
Close #10
1 parent 117af0c commit 8ddf07a

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/validators/builtin/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function builtinValidation(env: Record<string, string>, schema: PoppinsSc
2828
// Handle undefined aka optional results
2929
if (typeof res === 'undefined') {
3030
delete process.env[key]
31-
return
31+
continue
3232
}
3333

3434
process.env[key] = res

src/validators/zod/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function zodValidation(env: Record<string, string>, schema: ZodSche
3232
// Handle undefined aka optional results
3333
if (typeof result.data === 'undefined') {
3434
delete process.env[key]
35-
return
35+
continue
3636
}
3737

3838
process.env[key] = result.data

tests/common.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,23 @@ test.group('vite-plugin-validate-env', (group) => {
191191
await plugin.config(viteConfig, viteEnvConfig)
192192
assert.equal(process.env.VITE_OPTIONAL, undefined)
193193
})
194+
195+
test('dont stop validation after undefined result', async ({ assert }) => {
196+
assert.plan(2)
197+
198+
const plugin = ValidateEnv({
199+
validator: 'builtin',
200+
schema: {
201+
VITE_OPTIONAL: Schema.number.optional(),
202+
VITE_MY_VAR: Schema.string(),
203+
},
204+
})
205+
206+
await fs.add('.env.development', 'VITE_MY_VAR=hello')
207+
// @ts-ignore
208+
await plugin.config(viteConfig, viteEnvConfig)
209+
210+
assert.equal(process.env.VITE_OPTIONAL, undefined)
211+
assert.equal(process.env.VITE_MY_VAR, 'hello')
212+
})
194213
})

tests/zod.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,23 @@ test.group('Zod validation adaptater', (group) => {
144144
await plugin.config(viteConfig, viteEnvConfig)
145145
assert.equal(process.env.VITE_OPTIONAL_ZOD, undefined)
146146
})
147+
148+
test('dont stop validation after undefined result', async ({ assert }) => {
149+
assert.plan(2)
150+
151+
const plugin = ValidateEnv({
152+
validator: 'zod',
153+
schema: {
154+
VITE_OPTIONAL_ZOD: z.string().max(2).optional(),
155+
VITE_MY_VAR: z.string(),
156+
},
157+
})
158+
159+
await fs.add(ENV_FILENAME, 'VITE_MY_VAR=hello')
160+
// @ts-ignore
161+
await plugin.config(viteConfig, viteEnvConfig)
162+
163+
assert.equal(process.env.VITE_OPTIONAL_ZOD, undefined)
164+
assert.equal(process.env.VITE_MY_VAR, 'hello')
165+
})
147166
})

0 commit comments

Comments
 (0)