Fix support for array defaults using type().default() syntax#199
Fix support for array defaults using type().default() syntax#199
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 3ff8f4c The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: yamcodes <2014360+yamcodes@users.noreply.github.com>
Co-authored-by: yamcodes <2014360+yamcodes@users.noreply.github.com>
|
@copilot Since this is an environment variable valiaditor, we should still constrain it to a string-keyed record, although your note that the type is too restrictive is taken. Maybe there is a middle ground where it's a record (string keyed, but the value can be unconstrained) or, we can just somehow "copy" the type from ArkType's type.object Also, we need a changeset. |
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Note Free review on us!CodeRabbit is offering free reviews until Wed Oct 08 2025 to showcase some of the refinements we've made. Comment |
commit: |
Co-authored-by: yamcodes <2014360+yamcodes@users.noreply.github.com>
Updated the constraint to The fix is in commit 80a0b2e. |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fix to an issue where `type("array[]").default(() => [...])` syntax was not accepted by `createEnv` due to overly restrictive type constraints. The function now accepts any string-keyed record while still maintaining type safety through ArkType's validation system. Added support for array defaults using `type("string[]").default(() => [])` syntax and complex array types.
- Added ALLOWED_ORIGINS array type - Added .env.example file
- Array defaults to empty - Support complex array types - Mixed schemas supported
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## arkenv@0.7.2 ### Patch Changes - #### Support array defaults using type().default() syntax _[`#199`](#199) [`e50dba1`](e50dba1) [@copilot-swe-agent](https://github.com/apps/copilot-swe-agent)_ Fix to an issue where `type("array[]").default(() => [...])` syntax was not accepted by `createEnv` due to overly restrictive type constraints. The function now accepts any string-keyed record while still maintaining type safety through ArkType's validation system. ##### New Features - Array defaults to empty using `type("string[]").default(() => [])` syntax - Support for complex array types with defaults - Mixed schemas combining string-based and type-based defaults ##### Example ```typescript const env = arkenv({ ALLOWED_ORIGINS: type("string[]").default(() => ["localhost"]), FEATURE_FLAGS: type("string[]").default(() => []), PORT: "number.port", }); ``` ## @arkenv/vite-plugin@0.0.12 ### Patch Changes <details><summary>Updated 1 dependency</summary> <small> [`e50dba1`](e50dba1) </small> - `arkenv@0.7.2` </details> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Updated dependencies in pnpm-lock.yaml, including vitest and jsdom. - Added new environment variables (VITE_MY_NUMBER, VITE_MY_VAR, VITE_MY_BOOLEAN) to the Vite development environment. - Enhanced the Vite configuration to utilize the new environment variables. - Updated the App component to display the new environment variables. These changes improve the development experience and ensure compatibility with the latest dependencies. Related: #162 #163 #199 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Added support for default values on array environment variables, including complex array types; can initialize to empty arrays. - Enabled mixed schemas combining string-based and type-based defaults. - Playground now displays example environment variables in the UI. - Bug Fixes - Resolved issue preventing array defaults from being accepted, improving validation while maintaining type safety. - Documentation - Documented array defaults in the Vite plugin with example usage. - Chores - Added a script to manage changesets. - Updated playground configuration to include new example environment variables. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This PR contains the following updates: | Package | Change | Age | Confidence | |---|---|---|---| | [arkenv](https://arkenv.js.org) ([source](https://redirect.github.com/yamcodes/arkenv)) | [`0.7.0` -> `0.7.3`](https://renovatebot.com/diffs/npm/arkenv/0.7.0/0.7.3) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>yamcodes/arkenv (arkenv)</summary> ### [`v0.7.3`](https://redirect.github.com/yamcodes/arkenv/releases/tag/arkenv%400.7.3) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/arkenv@0.7.2...arkenv@0.7.3) ##### Patch Changes - #### Automatic boolean string conversion *[`#218`](https://redirect.github.com/yamcodes/arkenv/pull/218) [`e554e2b`](https://redirect.github.com/yamcodes/arkenv/commit/e554e2b41aab1b8e29d873982ea587c069f4732d) [@​yamcodes](https://redirect.github.com/yamcodes)* The `boolean` type now accepts `"true"`/`"false"` strings from environment variables and converts them to actual boolean values. This also works with boolean defaults. Example: ```ts import arkenv from "arkenv"; const env = arkenv({ DEBUG: "boolean", ENABLE_FEATURE: "boolean = true", }); console.log(env.DEBUG); console.log(env.ENABLE_FEATURE); ``` Result: ```sh ❯ DEBUG=true npx tsx index.ts true true ``` ### [`v0.7.2`](https://redirect.github.com/yamcodes/arkenv/releases/tag/arkenv%400.7.2) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/arkenv@0.7.1...arkenv@0.7.2) ##### Patch Changes - #### Support array defaults using type().default() syntax *[`#199`](https://redirect.github.com/yamcodes/arkenv/pull/199) [`e50dba1`](https://redirect.github.com/yamcodes/arkenv/commit/e50dba1f19418f8fc007dc786df1172067e3d07c) [@​copilot-swe-agent](https://redirect.github.com/apps/copilot-swe-agent)* Fix to an issue where `type("array[]").default(() => [...])` syntax was not accepted by `createEnv` due to overly restrictive type constraints. The function now accepts any string-keyed record while still maintaining type safety through ArkType's validation system. ##### New Features - Array defaults to empty using `type("string[]").default(() => [])` syntax - Support for complex array types with defaults - Mixed schemas combining string-based and type-based defaults ##### Example ```typescript const env = arkenv({ ALLOWED_ORIGINS: type("string[]").default(() => ["localhost"]), FEATURE_FLAGS: type("string[]").default(() => []), PORT: "number.port", }); ``` ### [`v0.7.1`](https://redirect.github.com/yamcodes/arkenv/releases/tag/arkenv%400.7.1) [Compare Source](https://redirect.github.com/yamcodes/arkenv/compare/arkenv@0.7.0...arkenv@0.7.1) ##### Patch Changes - Export `ArkEnvError` *[`#161`](https://redirect.github.com/yamcodes/arkenv/pull/161) [`221f9ef`](https://redirect.github.com/yamcodes/arkenv/commit/221f9efdef65691b0c5155b12ec460404dddbe82) [@​yamcodes](https://redirect.github.com/yamcodes)* You can now import `ArkEnvError` from `arkenv`: ```ts import { ArkEnvError } from "arkenv"; ``` - Improve JSDoc *[`#161`](https://redirect.github.com/yamcodes/arkenv/pull/161) [`221f9ef`](https://redirect.github.com/yamcodes/arkenv/commit/221f9efdef65691b0c5155b12ec460404dddbe82) [@​yamcodes](https://redirect.github.com/yamcodes)* The JSDoc for `arkenv` and `createEnv` is now more descriptive. </details> --- ### Configuration 📅 **Schedule**: Branch creation - "on friday" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/yamcodes/arkenv). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNTkuNCIsInVwZGF0ZWRJblZlciI6IjQxLjE1OS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Fixes the issue where using
type("array[]").default(() => [...])syntax was not accepted bycreateEnvdue to overly restrictive type constraints.Problem
The
createEnvfunction had a type constraint that only acceptedRecord<string, string | undefined>, which prevented ArkType objects with default values from being used. This meant the following syntax from the ArkType documentation didn't work:This would result in a TypeScript error:
Solution
Updated the type constraint to maintain string-keyed requirement while allowing flexible value types:
Before:
After:
This approach preserves the environment variable paradigm (string keys) while supporting ArkType objects with defaults. The
EnvSchema<T>type provides proper validation through ArkType's type system.What Now Works
✅ Array defaults with type function syntax:
✅ Complex array types:
✅ Mixed schemas with both string-based and type-based defaults work together
Backward Compatibility
All existing functionality continues to work unchanged:
"string","number.port", etc."'development' | 'production' = 'development'""API_KEY?": "string""string.host","number.port"Testing
Added comprehensive test coverage including:
Documentation
Updated the quickstart guide to include examples of array defaults, helping users discover this powerful feature.
Changeset
Added changeset for minor version bump reflecting the new functionality.
Fixes #163
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Note
Allow array defaults using type("...[]").default(() => [...]) by loosening createEnv input types; add tests, docs, and changeset.
createEnvsignature to acceptRecord<string, any>(fromRecord<string, string | undefined>) enablingtype("...[]").default(() => [...])schemas.packages/arkenv/src/array-defaults-integration.test.tscovering array defaults, complex arrays, and env overrides.create-env.test.tsandtype.test.tsto validate array defaults usingtype("...[]").default(() => [...]).apps/www/content/docs/quickstart.mdxto import{ type }and demonstrate array defaults (ALLOWED_ORIGINS,FEATURE_FLAGS)..changeset/array-defaults-support.mdfor a minor release.Written by Cursor Bugbot for commit 77d51fb. This will update automatically on new commits. Configure here.