-
Notifications
You must be signed in to change notification settings - Fork 5
Support standard validator mode in Bun plugin
#763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yamcodes
merged 7 commits into
main
from
762-make-bun-plugin-work-with-standard-validator-mode
Jan 21, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c72565
docs(bun-plugin-validator-mode): add design, proposal, spec, tasks
yamcodes 928be22
Merge branch 'main' into 762-make-bun-plugin-work-with-standard-valid…
yamcodes f7f7e98
Merge branch 'main' into 762-make-bun-plugin-work-with-standard-valid…
yamcodes b5f5985
feat(bun-plugin): add validator mode support
yamcodes acda623
fix
yamcodes a274d60
feat(bun-plugin): enable standard validator mode
yamcodes 273239f
fix(vite-plugin): Use default env if not provided
yamcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,18 @@ | ||
| --- | ||
| "@arkenv/bun-plugin": patch | ||
| --- | ||
|
|
||
| #### Support configuration | ||
|
|
||
| Add support for an optional configuration object as the second argument. This allows you to set the `validator` mode to `"standard"`, enabling support for libraries like Zod or Valibot without an ArkType dependency. | ||
|
|
||
| ```ts | ||
| import { z } from "zod"; | ||
| import arkenv from "@arkenv/bun-plugin"; | ||
|
|
||
| arkenv({ | ||
| BUN_PUBLIC_API_URL: z.string().url() | ||
| }, { | ||
| validator: "standard" | ||
| }) | ||
| ``` |
This file contains hidden or 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 hidden or 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,71 @@ | ||
| # Design: Bun Plugin Validator Mode Support | ||
|
|
||
| ## Problem | ||
| The Bun plugin needs to support the `validator: "standard"` mode to allow users to build projects without an `arktype` dependency. | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| ### 1. Plugin Signature | ||
| We will update the `arkenv` function in `packages/bun-plugin/src/plugin.ts`: | ||
|
|
||
| ```typescript | ||
| export function arkenv( | ||
| options: CompiledEnvSchema, | ||
| arkenvConfig?: ArkEnvConfig, | ||
| ): BunPlugin; | ||
| export function arkenv<const T extends SchemaShape>( | ||
| options: EnvSchema<T>, | ||
| arkenvConfig?: ArkEnvConfig, | ||
| ): BunPlugin; | ||
| export function arkenv<const T extends SchemaShape>( | ||
| options: EnvSchema<T> | CompiledEnvSchema, | ||
| arkenvConfig?: ArkEnvConfig, | ||
| ): BunPlugin { | ||
| const envMap = processEnvSchema<T>(options, arkenvConfig); | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Update processEnvSchema Utility | ||
| The `processEnvSchema` function in `packages/bun-plugin/src/utils.ts` will be updated to accept and pass through the config: | ||
|
|
||
| ```typescript | ||
| export function processEnvSchema<T extends SchemaShape>( | ||
| options: EnvSchema<T> | CompiledEnvSchema, | ||
| config?: ArkEnvConfig, | ||
| ): Map<string, string> { | ||
| const env: SchemaShape = createEnv(options as any, { | ||
| ...config, | ||
| env: process.env, | ||
| }); | ||
| // ... rest of the function | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Hybrid Mode Consideration | ||
| The `hybrid` export (zero-config mode) will continue to work without config, defaulting to ArkType mode. Users who want Standard Schema support must use the function call syntax. | ||
|
|
||
| ### 4. Bun Config Support | ||
| Users will be able to configure it like this: | ||
|
|
||
| ```ts | ||
| import { z } from 'zod' | ||
| import arkenv from '@arkenv/bun-plugin' | ||
|
|
||
| Bun.build({ | ||
| plugins: [ | ||
| arkenv({ | ||
| BUN_PUBLIC_API_URL: z.string().url() | ||
| }, { | ||
| validator: 'standard' | ||
| }) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
| By passing `validator: 'standard'`, `createEnv` will skip the dynamic loading of ArkType, thus avoiding the `MODULE_NOT_FOUND` error. | ||
|
|
||
| ## Differences from Vite Plugin | ||
| - The Bun plugin uses `processEnvSchema` utility instead of inline `createEnv` call | ||
| - The Bun plugin has a `hybrid` export for zero-config usage (which won't support config parameter) | ||
| - The Bun plugin uses `BUN_PUBLIC_` prefix instead of `VITE_` |
This file contains hidden or 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,16 @@ | ||
| # Proposal: Support Validator Mode in Bun Plugin | ||
|
|
||
| ## Why | ||
| Support the "standard" validator mode in `@arkenv/bun-plugin` to allow users to validate environment variables using libraries like Zod or Valibot without requiring `arktype` as a dependency. This enables a zero-ArkType runtime for Bun projects. | ||
|
|
||
| ## What Changes | ||
| - **MODIFIED**: Update the `arkenv()` plugin factory in `packages/bun-plugin/src/plugin.ts` to accept an optional `ArkEnvConfig` object as a second argument. | ||
| - **MODIFIED**: Update the `processEnvSchema` utility in `packages/bun-plugin/src/utils.ts` to accept and pass through the `ArkEnvConfig` to the `createEnv` call. | ||
| - **FILES AFFECTED**: | ||
| - `packages/bun-plugin/src/plugin.ts` | ||
| - `packages/bun-plugin/src/utils.ts` | ||
|
|
||
| ## Impact | ||
| - **Users**: Enables users who have removed `arktype` to still use ArkEnv's Bun plugin with Standard Schema validators. | ||
| - **Type System**: Leverages downstream type inference improvements via `InferType` (from `vite-plugin-validator-mode`). | ||
| - **Dependencies**: Introduces a dependency on the logic/types developed in the `vite-plugin-validator-mode` change. |
65 changes: 65 additions & 0 deletions
65
openspec/changes/bun-plugin-validator-mode/specs/bun-plugin/spec.md
This file contains hidden or 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,65 @@ | ||
| # bun-plugin Spec Delta | ||
|
|
||
| ## ADDED Requirements | ||
|
|
||
| ### Requirement: Bun Plugin Validator Mode Configuration | ||
|
|
||
| The Bun plugin SHALL accept an optional `ArkEnvConfig` parameter to configure the validator engine, allowing users to choose between ArkType (default) and Standard Schema validators (e.g., Zod, Valibot). | ||
|
|
||
| #### Scenario: Configure plugin with Standard Schema validator | ||
|
|
||
| - **WHEN** a user configures the Bun plugin with a Zod schema | ||
| - **AND** they pass `{ validator: "standard" }` as the second argument | ||
| - **THEN** the plugin SHALL use Standard Schema validation instead of ArkType | ||
| - **AND** the plugin SHALL NOT require `arktype` as a dependency | ||
| - **AND** environment variables SHALL be validated using the Standard Schema validator | ||
| - **AND** the validated values SHALL be statically replaced in the bundle | ||
|
|
||
| ```typescript | ||
| import { z } from 'zod' | ||
| import arkenv from '@arkenv/bun-plugin' | ||
|
|
||
| Bun.build({ | ||
| plugins: [ | ||
| arkenv({ | ||
| BUN_PUBLIC_API_URL: z.string().url(), | ||
| BUN_PUBLIC_DEBUG: z.boolean() | ||
| }, { | ||
| validator: 'standard' | ||
| }) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
| #### Scenario: Default to ArkType when no validator config provided | ||
|
|
||
| - **WHEN** a user configures the Bun plugin without a second argument | ||
| - **THEN** the plugin SHALL default to `validator: "arktype"` | ||
| - **AND** the plugin SHALL use ArkType for validation | ||
| - **AND** existing behavior SHALL remain unchanged | ||
|
|
||
| #### Scenario: Validator config is passed through to createEnv | ||
|
|
||
| - **WHEN** a user provides an `ArkEnvConfig` object to the plugin | ||
| - **THEN** the plugin SHALL merge this config with the environment variables | ||
| - **AND** the config SHALL be passed to `createEnv` along with `env: process.env` | ||
| - **AND** all config options (validator, coerce, onUndeclaredKey, arrayFormat) SHALL be respected | ||
|
|
||
| ## MODIFIED Requirements | ||
|
|
||
| ### Requirement: Bun Plugin Environment Variable Validation and Transformation | ||
|
|
||
| The Bun plugin SHALL validate environment variables at build-time using ArkEnv's schema validation **with configurable validator engine** and statically replace `process.env.VARIABLE` access with validated, transformed values during bundling. The plugin SHALL transform values according to the schema (e.g., string to boolean, apply default values). | ||
|
|
||
| **Changes:** | ||
| - Added support for configurable validator engine via `ArkEnvConfig` parameter | ||
| - Plugin can now use either ArkType or Standard Schema validators | ||
|
|
||
| #### Scenario: Plugin validates with Standard Schema validators | ||
|
|
||
| - **WHEN** a user configures the Bun plugin with a Standard Schema (Zod/Valibot) and `validator: "standard"` | ||
| - **AND** the schema includes variables with various types | ||
| - **THEN** the plugin validates all variables using the Standard Schema validator | ||
| - **AND** the plugin transforms values according to the schema | ||
| - **AND** the plugin statically replaces `process.env.VARIABLE` with the validated value | ||
| - **AND** validation errors from the Standard Schema library are properly reported |
This file contains hidden or 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,8 @@ | ||
| # Tasks: Bun Plugin Validator Mode Support | ||
|
|
||
| 1. - [x] Update `@arkenv/bun-plugin` signature to `arkenv(schema, config?)` | ||
| 2. - [x] Update `processEnvSchema` to accept and pass `config` to `createEnv` | ||
| 3. - [x] Add JSDoc examples showing `validator: "standard"` usage | ||
| 4. - [x] Update plugin documentation with Standard Schema examples | ||
| 5. - [x] Add test case for `validator: "standard"` in Bun plugin tests | ||
| 6. - [x] Verify using a Zod schema in an example project with `arktype` uninstalled |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.