Skip to content

Commit 74b638d

Browse files
authored
Merge pull request #2 from danimydev/v1.0.0
Make it SSv1 compatible and runtime independent.
2 parents 311d79e + 6413f49 commit 74b638d

27 files changed

+736
-1114
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.zed
2+
coverage

README.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
# @danimydev/env
22

3-
A TypeScript-first, runtime-safe environment variable reader. Works in Node.js,
4-
Deno and Bun. If you want you can just copy the source code or install it via
5-
[jsr](https://jsr.io/@danimydev/env).
3+
A TypeScript-first, runtime-safe object validator for environment-like records.
4+
It works with any schema implementing the StandardSchemaV1 spec, including
5+
Valibot, Zod, or custom schemas.
66

77
## Usage
88

99
```typescript
10-
import env, { boolean, number, optional, string } from "@danimydev/env";
11-
12-
const config = env({
13-
NODE_ENV: string(), // string,
14-
PORT: number(), // number,
15-
DEBUG: optional(boolean()), // boolean | undefined
16-
});
10+
import { env } from "@danimydev/env";
11+
import { boolean, enum as enum_, number } from "@zod/zod";
12+
13+
const typeSafeEnv = await env(
14+
{
15+
NODE_ENV: enum_(["development", "production"]),
16+
PORT: number(),
17+
IS_ENABLED: boolean(),
18+
},
19+
Deno.env.toObject(), // or process.env in Node.js
20+
);
21+
22+
console.log(typeSafeEnv.NODE_ENV); // "development" | "production"
23+
console.log(typeSafeEnv.PORT); // number
24+
console.log(typeSafeEnv.IS_ENABLED); // boolean
1725
```
1826

19-
## Highlights
20-
21-
- ✅ TypeScript-first: fully typed environment variable schemas.
22-
- 🌍 Cross-runtime: works in Node.js, Deno and Bun.
23-
- 💡 Flexible: supports optional variables.
24-
- ⚡ Lightweight: zero dependencies, minimal overhead.
25-
2627
## License
2728

28-
This project is released into the public domain under
29-
[The Unlicense](https://unlicense.org).
30-
31-
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
32-
software for any purpose.
33-
34-
If you like the project, feel free to ⭐
35-
[@danimydev/env on GitHub](https://github.com/danimydev/env)!
29+
Released under The Unlicense — free to use, copy, modify, publish, or
30+
distribute. ⭐ Star on GitHub if you like it!

deno.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
{
22
"name": "@danimydev/env",
3-
"version": "0.1.1",
3+
"version": "1.0.0",
44
"license": "MIT",
5-
"exports": "./src/mod.ts",
5+
"exports": {
6+
".": "./src/mod.ts",
7+
"./schemas/valibot": "./src/schemas/valibot/mod.ts",
8+
"./schemas/zod": "./src/schemas/zod/mod.ts"
9+
},
610
"tasks": {
711
"dev": "deno test"
812
},
913
"publish": {
1014
"exclude": ["src/**/*_test.ts"]
1115
},
1216
"imports": {
13-
"@std/assert": "jsr:@std/assert@1"
17+
"@std/assert": "jsr:@std/assert@1",
18+
"@valibot/valibot": "jsr:@valibot/valibot@^1.1.0",
19+
"@zod/zod": "jsr:@zod/zod@^4.1.12"
1420
}
1521
}

deno.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/boolean.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/boolean_test.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/env.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ export interface StandardSchemaV1<Input = unknown, Output = Input> {
77
export declare namespace StandardSchemaV1 {
88
/** The Standard Schema properties interface. */
99
export interface Props<Input = unknown, Output = Input> {
10+
/** The version number of the standard. */
11+
readonly version: 1;
12+
/** The vendor name of the schema library. */
13+
readonly vendor: string;
1014
/** Validates unknown input values. */
1115
readonly validate: (
1216
value: unknown,
13-
) => Result<Output>;
17+
) => Result<Output> | Promise<Result<Output>>;
1418
/** Inferred types associated with the schema. */
1519
readonly types?: Types<Input, Output> | undefined;
1620
}

src/lib/standard-schema/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { StandardSchemaV1 } from "./spec.ts";
2+
3+
/**
4+
* A schema error with useful information.
5+
*/
6+
export class SchemaError extends Error {
7+
/**
8+
* The schema issues.
9+
*/
10+
public readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
11+
12+
/**
13+
* Creates a schema error with useful information.
14+
*
15+
* @param issues The schema issues.
16+
*/
17+
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>) {
18+
super(issues[0].message);
19+
this.name = "SchemaError";
20+
this.issues = issues;
21+
}
22+
}

0 commit comments

Comments
 (0)