@safe-hand/safe-env-check 1.0.2 → 1.0.3
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/.github/workflows/release.yml +34 -0
- package/CHANGELOG.md +39 -0
- package/README.md +89 -18
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*.*.*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
release:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v3
|
|
13
|
+
|
|
14
|
+
- name: Set up Node.js
|
|
15
|
+
uses: actions/setup-node@v3
|
|
16
|
+
with:
|
|
17
|
+
node-version: '20'
|
|
18
|
+
registry-url: 'https://registry.npmjs.org'
|
|
19
|
+
|
|
20
|
+
- name: Install dependencies
|
|
21
|
+
run: npm install
|
|
22
|
+
|
|
23
|
+
- name: Build
|
|
24
|
+
run: npm run build
|
|
25
|
+
|
|
26
|
+
- name: Publish to npm
|
|
27
|
+
env:
|
|
28
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
29
|
+
run: npm publish --access public
|
|
30
|
+
|
|
31
|
+
- name: Create GitHub Release
|
|
32
|
+
uses: ncipollo/release-action@v1
|
|
33
|
+
with:
|
|
34
|
+
tag: ${{ github.ref_name }}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
|
+
and [Semantic Versioning](https://semver.org/).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## [Unreleased]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Placeholder for upcoming changes
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## [1.0.3] - 2026-02-18
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
|
|
22
|
+
- Initial CHANGELOG.md for tracking
|
|
23
|
+
- Release workflow setup
|
|
24
|
+
|
|
25
|
+
## [1.0.2] - 2026-02-18
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- Initial npm release (version already existed)
|
|
30
|
+
- Schema-based environment validation
|
|
31
|
+
- CLI support
|
|
32
|
+
- Strict mode
|
|
33
|
+
- TypeScript support
|
|
34
|
+
- Support for types: `string`, `number`, `boolean`, `enum`
|
|
35
|
+
- Required and default value support
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- None yet
|
package/README.md
CHANGED
|
@@ -1,19 +1,51 @@
|
|
|
1
1
|
# safe-env-check
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
4
6
|
|
|
5
|
-
## Install
|
|
6
7
|
|
|
8
|
+
A tiny TypeScript library to validate environment variables using a schema with support for:
|
|
9
|
+
|
|
10
|
+
- ✅ Type validation
|
|
11
|
+
- ✅ Required & default values
|
|
12
|
+
- ✅ Enum values
|
|
13
|
+
- ✅ Strict mode (no extra env vars)
|
|
14
|
+
- ✅ dotenv integration
|
|
15
|
+
- ✅ Custom error formatting
|
|
16
|
+
- ✅ CLI support
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @safe-hand/safe-env-check
|
|
24
|
+
```
|
|
25
|
+
or
|
|
7
26
|
```bash
|
|
8
|
-
|
|
27
|
+
yarn add @safe-hand/safe-env-check
|
|
9
28
|
```
|
|
10
29
|
|
|
11
|
-
##
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- Validate process.env using a schema
|
|
33
|
+
|
|
34
|
+
- Strongly typed output (TypeScript)
|
|
35
|
+
|
|
36
|
+
- Prevents app startup with invalid configuration
|
|
37
|
+
|
|
38
|
+
- Supports CLI for CI/CD and deployment checks
|
|
39
|
+
|
|
40
|
+
- Customizable error messages
|
|
41
|
+
|
|
42
|
+
- Optional strict mode to disallow unknown variables
|
|
12
43
|
|
|
13
|
-
|
|
14
|
-
import { validateEnv } from "safe-env-check";
|
|
44
|
+
## Basic Usage
|
|
15
45
|
|
|
16
|
-
|
|
46
|
+
### Define a schema
|
|
47
|
+
```ts
|
|
48
|
+
const schema = {
|
|
17
49
|
PORT: { type: "number", required: true },
|
|
18
50
|
JWT_SECRET: { type: "string", required: true },
|
|
19
51
|
NODE_ENV: {
|
|
@@ -21,38 +53,77 @@ const env = validateEnv({
|
|
|
21
53
|
values: ["development", "production"],
|
|
22
54
|
default: "development",
|
|
23
55
|
},
|
|
24
|
-
}
|
|
56
|
+
};
|
|
25
57
|
```
|
|
26
58
|
|
|
27
|
-
|
|
59
|
+
### Validate environment variables
|
|
60
|
+
```ts
|
|
61
|
+
import { validateEnv } from "@safe-hand/safe-env-check";
|
|
28
62
|
|
|
29
|
-
|
|
30
|
-
|
|
63
|
+
const env = validateEnv(schema);
|
|
64
|
+
|
|
65
|
+
console.log(env.PORT); // number
|
|
66
|
+
console.log(env.NODE_ENV); // "development" | "production"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Schema Options
|
|
70
|
+
Each environment variables supports the following options:
|
|
71
|
+
|
|
72
|
+
| Field | Type | Description | | | |
|
|
73
|
+
| ---------- | ---------- | -------------------------------- | --------- | ------- | ------------------- |
|
|
74
|
+
| `type` | `"string" or "number" or "boolean" or "enum"` | Expected value type |
|
|
75
|
+
| `required` | `boolean` | Whether the variable is required | | | |
|
|
76
|
+
| `default` | `any` | Default value if not provided | | | |
|
|
77
|
+
| `values` | `string[]` | Required for `enum` type | | | |
|
|
78
|
+
|
|
79
|
+
## Example
|
|
80
|
+
```ts
|
|
81
|
+
DATABASE_URL: { type: "string", required: true },
|
|
82
|
+
DEBUG: { type: "boolean", default: false },
|
|
83
|
+
MODE: { type: "enum", values: ["dev", "prod"] }
|
|
31
84
|
```
|
|
32
85
|
|
|
33
|
-
|
|
86
|
+
## Strict Mode
|
|
87
|
+
Disallow environment variables that are not defined in the schema.
|
|
88
|
+
```ts
|
|
89
|
+
validateEnv(schema, { strict: true });
|
|
90
|
+
```
|
|
91
|
+
If extra variables are found, validation will fail.
|
|
34
92
|
|
|
35
|
-
|
|
93
|
+
## Custom Error Formatter
|
|
94
|
+
You can control how errors are displayed:
|
|
95
|
+
```ts
|
|
36
96
|
validateEnv(schema, {
|
|
37
97
|
formatError: (errors) => `Config error:\n${errors.join("\n")}`,
|
|
38
98
|
});
|
|
39
99
|
```
|
|
40
100
|
|
|
41
|
-
##
|
|
101
|
+
## Dotenv Support
|
|
102
|
+
|
|
103
|
+
By default, the library loads .env automatically using dotenv.
|
|
104
|
+
|
|
105
|
+
Example .env file:
|
|
106
|
+
```bash
|
|
107
|
+
PORT=3000
|
|
108
|
+
JWT_SECRET=supersecret
|
|
109
|
+
NODE_ENV=development
|
|
110
|
+
```
|
|
42
111
|
|
|
43
|
-
|
|
112
|
+
## CLI Usage
|
|
44
113
|
|
|
45
|
-
|
|
114
|
+
Create a schema file called env.schema.js:
|
|
115
|
+
```ts
|
|
46
116
|
module.exports = {
|
|
47
117
|
PORT: { type: "number", required: true },
|
|
48
|
-
NODE_ENV: { type: "enum", values: ["dev", "prod"] },
|
|
118
|
+
NODE_ENV: { type: "enum", values: ["dev", "prod"], default: "dev" },
|
|
49
119
|
};
|
|
50
120
|
```
|
|
51
121
|
|
|
122
|
+
Run validation:
|
|
52
123
|
```bash
|
|
53
124
|
npx safe-env-check env.schema.js
|
|
54
125
|
```
|
|
55
126
|
|
|
56
127
|
## License
|
|
57
128
|
|
|
58
|
-
MIT © Shakhawat Hossain
|
|
129
|
+
MIT © Shakhawat Hossain
|