scenv-zod 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -0
- package/package.json +13 -21
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# scenv-zod
|
|
2
|
+
|
|
3
|
+
Zod-based validator for [scenv](https://www.npmjs.com/package/scenv). Use Zod schemas to validate and coerce variable values (env/context/CLI are strings; use `z.coerce.number()`, etc.).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add scenv scenv-zod zod
|
|
9
|
+
# or
|
|
10
|
+
npm install scenv scenv-zod zod
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Peer dependencies:** `scenv`, `zod` (^3.22.0).
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Pass `validator(schema)` as the `validator` option to `scenv()`. The schema can coerce strings to numbers, booleans, etc.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { scenv } from "scenv";
|
|
21
|
+
import { validator } from "scenv-zod";
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
|
|
24
|
+
const port = scenv("Port", {
|
|
25
|
+
key: "port",
|
|
26
|
+
env: "PORT",
|
|
27
|
+
default: 3000,
|
|
28
|
+
validator: validator(z.coerce.number().min(1).max(65535)),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const debug = scenv("Debug", {
|
|
32
|
+
key: "debug",
|
|
33
|
+
default: false,
|
|
34
|
+
validator: validator(
|
|
35
|
+
z.union([z.boolean(), z.literal("true"), z.literal("false")])
|
|
36
|
+
.transform((v) => v === true || v === "true")
|
|
37
|
+
),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const portNum = await port.get(); // number
|
|
41
|
+
const isDebug = await debug.get(); // boolean
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Validation runs during `.get()` / `.safeGet()`. On failure, `.get()` throws; `.safeGet()` returns `{ success: false, error }` with the Zod error.
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
| Export | Description |
|
|
49
|
+
|--------|-------------|
|
|
50
|
+
| `validator(schema)` | Returns a function `(val: unknown) => { success: true, data } \| { success: false, error }` compatible with scenv’s `validator` option. |
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scenv-zod",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Zod validator for scenv variables",
|
|
5
|
-
"repository": {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"scenv",
|
|
11
|
-
"zod",
|
|
12
|
-
"validation"
|
|
13
|
-
],
|
|
5
|
+
"repository": { "type": "git", "url": "https://github.com/PKWadsy/scenv" },
|
|
6
|
+
"publishConfig": { "access": "public", "provenance": true },
|
|
7
|
+
"keywords": ["scenv", "zod", "validation"],
|
|
14
8
|
"type": "module",
|
|
15
9
|
"main": "dist/index.cjs",
|
|
16
10
|
"module": "dist/index.js",
|
|
@@ -22,20 +16,18 @@
|
|
|
22
16
|
"types": "./dist/index.d.ts"
|
|
23
17
|
}
|
|
24
18
|
},
|
|
25
|
-
"files": [
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
"files": ["dist"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean"
|
|
22
|
+
},
|
|
28
23
|
"peerDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
24
|
+
"scenv": "workspace:*",
|
|
25
|
+
"zod": "^3.22.0"
|
|
31
26
|
},
|
|
32
27
|
"devDependencies": {
|
|
28
|
+
"scenv": "workspace:*",
|
|
33
29
|
"zod": "^3.22.0",
|
|
34
30
|
"tsup": "^8.0.0",
|
|
35
|
-
"typescript": "^5.3.0"
|
|
36
|
-
"scenv": "0.3.0"
|
|
37
|
-
},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --clean"
|
|
31
|
+
"typescript": "^5.3.0"
|
|
40
32
|
}
|
|
41
|
-
}
|
|
33
|
+
}
|