Skip to content

Commit

Permalink
chore: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Nov 25, 2023
1 parent 00a1148 commit b3a10c1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default defineConfig({

Beware, there are some limitations if you use Zod. For example, you can't use a boolean or number type directly. Because everything that comes from your `.env` file is a string by default.

So to validate a boolean you must use `preprocess`, and `transform`, like this:
So to validate other types than string you must use `preprocess`, and `transform`, like this:
```ts
// env.ts
import { defineConfig } from '@julr/vite-plugin-validate-env'
Expand All @@ -147,8 +147,21 @@ import { z } from 'zod'
export default defineConfig({
validator: 'zod',
schema: {
// This will transform the string 'true' or '1' to a boolean
VITE_BOOLEAN_VARIABLE: z
.preprocess((value) => value === 'true' || value === '1', z.boolean())
.preprocess((value) => value === 'true' || value === '1', z.boolean()),

// Will convert the string to a number
VITE_NUMBER: z.preprocess((value) => Number(value), z.number()),

// Will parse the string to an object
VITE_OBJECT: z.preprocess(
(value) => JSON.parse(value as string),
z.object({
a: z.string(),
b: z.number(),
}),
),
}
})
```
Expand Down

0 comments on commit b3a10c1

Please sign in to comment.