@safe-hand/safe-env-check 1.0.1 → 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/LICENSE +21 -0
- package/README.md +95 -19
- package/package.json +2 -2
|
@@ -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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shakhawat Hossain
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,19 +1,51 @@
|
|
|
1
1
|
# safe-env-check
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
4
6
|
|
|
5
|
-
|
|
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
|
|
6
21
|
|
|
7
22
|
```bash
|
|
8
|
-
npm install safe-env-check
|
|
23
|
+
npm install @safe-hand/safe-env-check
|
|
9
24
|
```
|
|
25
|
+
or
|
|
26
|
+
```bash
|
|
27
|
+
yarn add @safe-hand/safe-env-check
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- Validate process.env using a schema
|
|
10
33
|
|
|
11
|
-
|
|
34
|
+
- Strongly typed output (TypeScript)
|
|
12
35
|
|
|
13
|
-
|
|
14
|
-
import { validateEnv } from "safe-env-check";
|
|
36
|
+
- Prevents app startup with invalid configuration
|
|
15
37
|
|
|
16
|
-
|
|
38
|
+
- Supports CLI for CI/CD and deployment checks
|
|
39
|
+
|
|
40
|
+
- Customizable error messages
|
|
41
|
+
|
|
42
|
+
- Optional strict mode to disallow unknown variables
|
|
43
|
+
|
|
44
|
+
## Basic Usage
|
|
45
|
+
|
|
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,33 +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
|
|
42
102
|
|
|
43
|
-
|
|
103
|
+
By default, the library loads .env automatically using dotenv.
|
|
44
104
|
|
|
45
|
-
|
|
105
|
+
Example .env file:
|
|
106
|
+
```bash
|
|
107
|
+
PORT=3000
|
|
108
|
+
JWT_SECRET=supersecret
|
|
109
|
+
NODE_ENV=development
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## CLI Usage
|
|
113
|
+
|
|
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
|
```
|
|
121
|
+
|
|
122
|
+
Run validation:
|
|
51
123
|
```bash
|
|
52
|
-
|
|
53
|
-
```
|
|
124
|
+
npx safe-env-check env.schema.js
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT © Shakhawat Hossain
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@safe-hand/safe-env-check",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"bin": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"lint": "tsc --noEmit"
|
|
14
14
|
},
|
|
15
15
|
"author": "",
|
|
16
|
-
"license": "
|
|
16
|
+
"license": "MIT",
|
|
17
17
|
"description": "",
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@types/jest": "^30.0.0",
|