Skip to content

Commit 979c137

Browse files
committed
docs(review): resolve PR #1157 review comments, document safeArkEnv
1 parent c3fc46a commit 979c137

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

apps/www/content/docs/arkenv/quickstart.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ const dbConfig = {
164164
console.log(`Connecting to ${dbConfig.host}:${dbConfig.port}...`);
165165
```
166166

167+
## Safe parsing (Non-throwing)
168+
169+
By default, calling `arkenv()` will throw an `ArkEnvError` if validation fails. If you prefer to handle validation issues without catching thrown exceptions, you can use the non-throwing `safeArkEnv()` function instead. It returns a result object containing either the successfully parsed data or the caught error.
170+
171+
```ts title="env.ts" twoslash
172+
import { safeArkEnv } from 'arkenv';
173+
174+
const result = safeArkEnv({
175+
PORT: "number = 3000",
176+
});
177+
178+
if (result.success) {
179+
console.log("Validated config:", result.data);
180+
// env.PORT is guaranteed to be a number here
181+
} else {
182+
console.error("Validation failed:", result.error.message);
183+
}
184+
```
185+
167186
## Next steps
168187

169188
<Cards>

packages/arkenv/src/standard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function arkenv<const T extends Record<string, StandardSchemaV1>>(
5858
*
5959
* @param def - An object mapping variable names to Standard Schema validators
6060
* @param config - Optional configuration
61-
* @returns The SafeArkenvResult containing the data or the validation error object
61+
* @returns The SafeArkEnvResult containing the data or the validation error object
6262
*/
6363
export function safeArkEnv<const T extends Record<string, StandardSchemaV1>>(
6464
def: T,

packages/arkenv/src/utils/errors.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import { styleText } from "./style-text";
1616
* maintained manually based on ArkType's internal error reporting behavior.
1717
*
1818
* @see https://arktype.io/docs/intro
19+
* @internal
1920
*/
20-
const ARKTYPE_CODE_MAP = {
21+
const ARKTYPE_CODE_MAP: Record<string, EnvIssueCode> = {
2122
required: "MISSING_VARIABLE",
2223
pattern: "PATTERN_MISMATCH",
2324
min: "VALUE_TOO_SMALL",
@@ -29,26 +30,25 @@ const ARKTYPE_CODE_MAP = {
2930
sequence: "INVALID_TYPE",
3031
intersection: "INVALID_TYPE",
3132
union: "INVALID_TYPE",
32-
} satisfies Record<string, EnvIssueCode>;
33+
};
3334

3435
/**
3536
* Map an ArkType validation error code to a normalized EnvIssueCode.
3637
*
3738
* @param engineCode The raw code returned by the ArkType engine
3839
* @returns The normalized EnvIssueCode classification
40+
* @internal
3941
*/
4042
export function mapArkTypeCode(engineCode: string): EnvIssueCode {
41-
return (
42-
(ARKTYPE_CODE_MAP as Record<string, EnvIssueCode>)[engineCode] ??
43-
"INVALID_FORMAT"
44-
);
43+
return ARKTYPE_CODE_MAP[engineCode] ?? "INVALID_FORMAT";
4544
}
4645

4746
/**
4847
* Extract validation boundary metadata from an ArkType error object.
4948
*
5049
* @param error The raw error object from ArkType
5150
* @returns An object containing normalized min and/or max values if present
51+
* @internal
5252
*/
5353
export function getArkTypeMeta(error: any): { min?: number; max?: number } {
5454
const min = error.min ?? error.rule;
@@ -59,6 +59,16 @@ export function getArkTypeMeta(error: any): { min?: number; max?: number } {
5959
};
6060
}
6161

62+
/**
63+
* Mapping of Standard Schema validation issue codes to normalized EnvIssueCode classification codes.
64+
*
65+
* This serves as an internal translation map specifically for Standard Schema validators
66+
* (such as Zod or Valibot) to map their engine-specific error keys to our unified union type.
67+
* It is not a duplicate Source of Truth for the allowed issue codes themselves, which are
68+
* defined solely by the `EnvIssueCode` type in `core.ts`.
69+
*
70+
* @internal
71+
*/
6272
const STANDARD_CODE_MAP: Record<string, EnvIssueCode> = {
6373
too_small: "VALUE_TOO_SMALL",
6474
too_big: "VALUE_TOO_LARGE",
@@ -74,6 +84,7 @@ const STANDARD_CODE_MAP: Record<string, EnvIssueCode> = {
7484
* @param message The error message associated with the issue
7585
* @param receivedVal The raw value received by the validator
7686
* @returns The normalized EnvIssueCode classification
87+
* @internal
7788
*/
7889
export function mapStandardCode(
7990
engineCode: string,
@@ -102,6 +113,7 @@ export function mapStandardCode(
102113
*
103114
* @param issue The raw issue from Standard Schema
104115
* @returns An object containing normalized min and/or max values if present
116+
* @internal
105117
*/
106118
export function getStandardMeta(issue: any): { min?: number; max?: number } {
107119
const min = issue.minimum ?? issue.min;
@@ -117,6 +129,7 @@ export function getStandardMeta(issue: any): { min?: number; max?: number } {
117129
*
118130
* @param parseFn The function that parses the environment variables and might throw an ArkEnvError
119131
* @returns A SafeArkEnvResult containing either the parsed data or the caught ArkEnvError
132+
* @internal
120133
*/
121134
export function executeSafe<T>(parseFn: () => T): SafeArkEnvResult<T> {
122135
try {
@@ -139,6 +152,7 @@ export function executeSafe<T>(parseFn: () => T): SafeArkEnvResult<T> {
139152
* @param expected The expected type or value shape description
140153
* @param received The raw value received (redacted in string formatting if sensitive)
141154
* @returns A fully populated EnvIssue
155+
* @internal
142156
*/
143157
export function buildEnvIssue(
144158
path: string,
@@ -165,6 +179,7 @@ export function buildEnvIssue(
165179
* @param path The environment variable name/path under validation
166180
* @param config Optional config containing the debugSecrets override
167181
* @returns The formatted message string
182+
* @internal
168183
*/
169184
export function formatStandardIssueMessage(
170185
baseMessage: string,
@@ -203,6 +218,7 @@ export function formatStandardIssueMessage(
203218
* @param path The environment variable name/path under validation
204219
* @param debugSecrets Optional override for debug secrets mode
205220
* @returns The message with redacted/styled `(was …)` value
221+
* @internal
206222
*/
207223
export function redactMessageWasValue(
208224
message: string,

0 commit comments

Comments
 (0)