Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"packages/core": {
"release-type": "node",
"package-name": "env-sentry-lib",
"changelog-path": "packages/core/CHANGELOG.md",

"changelog-sections": [
{ "type": "feat", "section": "✨ Features", "hidden": false },
Expand Down
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ coverage:
status:
project:
default:
target: 45% # Overall project coverage target
target: 85% # Overall project coverage target
threshold: 2% # Allow 2% drop while still passing
patch:
default:
target: 45% # Coverage target for new code in PRs
target: 95% # Coverage target for new code in PRs

# Ignore patterns
ignore:
Expand Down
File renamed without changes.
16 changes: 4 additions & 12 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Type-safe environment variable validation for Node.js with Zod.

## Why?

Environment variables are strongly-typed and error-prone. `env-sentry-lib` gives you:
Environment variables are loosely typed and error-prone. `env-sentry-lib` gives you:

- ✅ **Type Safety** - Full TypeScript inference from your schema
- ✅ **Runtime Validation** - Catch configuration errors at startup
Expand All @@ -29,9 +29,6 @@ yarn add env-sentry-lib zod
import { createEnv } from 'env-sentry-lib';
import { z } from 'zod';

import { createEnv } from 'env-sentry-lib';
import { z } from 'zod';

// Define your environment schema
const env = createEnv({
// Server config
Expand Down Expand Up @@ -109,17 +106,12 @@ env.DEBUG_MODE; // type: boolean | undefined
When validation fails, you get actionable error messages:

```
❌ Environment Validation Failed
Environment validation failed.
The application cannot start due to invalid environment variables.

Missing required variables:
• DATABASE_URL
Expected: string
Fix: Add DATABASE_URL to your environment

Invalid values:
• PORT
Error: Expected number, received nan
Fix: Check the value and validation rules
Action: Define these variables in your environment.
```

## API Reference
Expand Down
35 changes: 13 additions & 22 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,55 @@
/**
* Custom error class for environment validation failures
*
* WHY: Better error messages than raw Zod errors
* HOW: Formats Zod validation errors into readable format
* WHY: Provide clear, actionable feedback when env validation fails
* HOW: Formats Zod validation errors into a readable, developer-friendly message
*/

import { z, ZodError } from 'zod';

export class EnvValidationError extends Error {
constructor(zodError: ZodError) {
const message = EnvValidationError.formatError(zodError);
super(message);
super(EnvValidationError.formatError(zodError));
this.name = 'EnvValidationError';
}

/**
* Format ZodError into a friendly, readable message
*/
private static formatError(zodError: ZodError): string {
const issues = zodError.issues;
const missingVars: z.ZodIssue[] = [];
const invalidVars: z.ZodIssue[] = [];

// Group issues by type
// Use z.core.$ZodIssue instead of deprecated ZodIssue
const missingVars: z.core.$ZodIssue[] = [];
const invalidVars: z.core.$ZodIssue[] = [];

for (const issue of issues) {
if (issue.code === 'invalid_type' && issue.input === 'undefined') {
for (const issue of zodError.issues) {
// A variable is considered "missing" only when it is truly undefined
if (issue.code === 'invalid_type' && issue.input === undefined) {
missingVars.push(issue);
} else {
invalidVars.push(issue);
}
}

// Build the error message
let message = '❌ Environment Validation Failed\n\n';
let message =
'Environment validation failed.\n' +
'The application cannot start due to invalid environment variables.\n\n';

// Missing variables section
if (missingVars.length > 0) {
message += 'Missing required variables:\n';
for (const issue of missingVars) {
const varName = issue.path.join('.');
message += ` • ${varName}\n`;
// Type guard to safely access 'expected' property
if ('expected' in issue) {
message += ` Expected: ${issue.expected}\n`;
}
message += ` Fix: Add ${varName} to your environment\n\n`;
}
message += 'Action: Define these variables in your environment.\n\n';
}

// Invalid values section
if (invalidVars.length > 0) {
message += 'Invalid values:\n';
for (const issue of invalidVars) {
const varName = issue.path.join('.');
message += ` • ${varName}\n`;
message += ` Error: ${issue.message}\n`;
message += ` Fix: Check the value and validation rules\n\n`;
}
message += 'Action: Verify the values match the expected format.\n\n';
}

return message.trim();
Expand Down
Loading