@yash53/envscope 0.0.0-stage → 1.0.0
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/LICENSE +17 -0
- package/README.md +90 -2
- package/bin/envscope.js +4 -0
- package/package.json +34 -4
- package/src/analyzer.js +69 -0
- package/src/cli.js +195 -0
- package/src/index.js +8 -0
- package/src/parser.js +47 -0
- package/src/validator.js +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 envscope contributors
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# envscope
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A zero-dependency environment configuration validator, security auditor and comparison CLI for Node.js.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Parse `.env` files
|
|
8
|
+
- Validate required variables, types, URLs, enums and limits
|
|
9
|
+
- Audit missing and placeholder secrets
|
|
10
|
+
- Compare two environment files
|
|
11
|
+
- Generate `.env.example` files from a schema
|
|
12
|
+
- Redact sensitive values before sharing logs
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
Node.js 18 or newer.
|
|
17
|
+
|
|
18
|
+
## CLI
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npx envscope help
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
When working from this repository:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
node bin/envscope.js doctor --file examples/.env.example --config envscope.config.json
|
|
28
|
+
node bin/envscope.js validate --file examples/.env.example
|
|
29
|
+
node bin/envscope.js audit --file examples/.env.example
|
|
30
|
+
node bin/envscope.js redact --file examples/.env.example
|
|
31
|
+
node bin/envscope.js compare examples/.env.example examples/.env.production.example
|
|
32
|
+
node bin/envscope.js example --out generated.env
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## JavaScript API
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import {
|
|
39
|
+
parseEnv,
|
|
40
|
+
readEnvFile,
|
|
41
|
+
validateEnv,
|
|
42
|
+
auditEnv,
|
|
43
|
+
compareEnv,
|
|
44
|
+
generateExample,
|
|
45
|
+
redactEnv
|
|
46
|
+
} from 'envscope';
|
|
47
|
+
|
|
48
|
+
const env = parseEnv('PORT=3000');
|
|
49
|
+
|
|
50
|
+
console.log(env);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Schema
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"properties": {
|
|
58
|
+
"PORT": {
|
|
59
|
+
"type": "number",
|
|
60
|
+
"required": true,
|
|
61
|
+
"min": 1,
|
|
62
|
+
"max": 65535,
|
|
63
|
+
"example": "3000"
|
|
64
|
+
},
|
|
65
|
+
"API_TOKEN": {
|
|
66
|
+
"type": "string",
|
|
67
|
+
"required": true,
|
|
68
|
+
"secret": true,
|
|
69
|
+
"minLength": 32
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Supported types: `string`, `number`, `boolean`, `url`, and `enum`.
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm install
|
|
81
|
+
npm test
|
|
82
|
+
npm run check
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Security note
|
|
86
|
+
|
|
87
|
+
Do not commit real `.env` files or share unredacted secrets. The audit is a heuristic helper, not a guarantee that a secret is secure. Review findings before deploying.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
package/bin/envscope.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yash53/envscope",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-dependency environment configuration validator, security auditor and comparison CLI for Node.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"envscope": "bin/envscope.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"bin/",
|
|
15
|
+
"src/",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test",
|
|
21
|
+
"check": "node --check bin/envscope.js && node --check src/index.js && node --check src/parser.js && node --check src/validator.js && node --check src/analyzer.js && node --check src/cli.js"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"env",
|
|
25
|
+
"environment",
|
|
26
|
+
"configuration",
|
|
27
|
+
"validator",
|
|
28
|
+
"security",
|
|
29
|
+
"developer-tools",
|
|
30
|
+
"cli"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT"
|
|
36
|
+
}
|
package/src/analyzer.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const SECRET_NAME = /(SECRET|TOKEN|PASSWORD|PASSWD|API_KEY|PRIVATE_KEY|CREDENTIAL)/i;
|
|
2
|
+
const PLACEHOLDER = /^(change.?me|your.?|replace.?me|example|placeholder|xxx+|todo|dummy|test)$/i;
|
|
3
|
+
|
|
4
|
+
export function auditEnv(env, schema = {}) {
|
|
5
|
+
const findings = [];
|
|
6
|
+
const properties = schema.properties ?? {};
|
|
7
|
+
|
|
8
|
+
for (const [key, value] of Object.entries(env)) {
|
|
9
|
+
const secret = properties[key]?.secret ?? SECRET_NAME.test(key);
|
|
10
|
+
|
|
11
|
+
if (!secret) continue;
|
|
12
|
+
|
|
13
|
+
if (!value) {
|
|
14
|
+
findings.push({ key, severity: 'error', message: `${key} is an empty secret` });
|
|
15
|
+
} else if (PLACEHOLDER.test(value.trim()) || value.includes('your-secret-here')) {
|
|
16
|
+
findings.push({ key, severity: 'warning', message: `${key} appears to contain a placeholder secret` });
|
|
17
|
+
} else if (value.length < 12) {
|
|
18
|
+
findings.push({ key, severity: 'warning', message: `${key} looks unusually short for a secret` });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (const [key, rules] of Object.entries(properties)) {
|
|
23
|
+
if (rules.secret && !env[key]) {
|
|
24
|
+
if (!findings.some((finding) => finding.key === key)) {
|
|
25
|
+
findings.push({ key, severity: 'error', message: `${key} is a missing secret` });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return findings;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function compareEnv(first, second) {
|
|
34
|
+
const firstKeys = new Set(Object.keys(first));
|
|
35
|
+
const secondKeys = new Set(Object.keys(second));
|
|
36
|
+
|
|
37
|
+
const onlyInFirst = [...firstKeys].filter((key) => !secondKeys.has(key)).sort();
|
|
38
|
+
const onlyInSecond = [...secondKeys].filter((key) => !firstKeys.has(key)).sort();
|
|
39
|
+
const different = [...firstKeys]
|
|
40
|
+
.filter((key) => secondKeys.has(key) && first[key] !== second[key])
|
|
41
|
+
.sort();
|
|
42
|
+
|
|
43
|
+
return { onlyInFirst, onlyInSecond, different };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function generateExample(schema = {}) {
|
|
47
|
+
const lines = ['# Generated by envscope', ''];
|
|
48
|
+
const properties = schema.properties ?? {};
|
|
49
|
+
|
|
50
|
+
for (const [key, rules] of Object.entries(properties)) {
|
|
51
|
+
if (rules.description) lines.push(`# ${rules.description}`);
|
|
52
|
+
lines.push(`${key}=${rules.example ?? ''}`);
|
|
53
|
+
lines.push('');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return lines.join('\n');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function redactEnv(env, schema = {}) {
|
|
60
|
+
const properties = schema.properties ?? {};
|
|
61
|
+
const redacted = {};
|
|
62
|
+
|
|
63
|
+
for (const [key, value] of Object.entries(env)) {
|
|
64
|
+
const secret = properties[key]?.secret ?? SECRET_NAME.test(key);
|
|
65
|
+
redacted[key] = secret && value ? '[REDACTED]' : value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return redacted;
|
|
69
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import {
|
|
3
|
+
readEnvFile,
|
|
4
|
+
validateEnv,
|
|
5
|
+
auditEnv,
|
|
6
|
+
compareEnv,
|
|
7
|
+
generateExample,
|
|
8
|
+
redactEnv
|
|
9
|
+
} from './index.js';
|
|
10
|
+
|
|
11
|
+
const starterSchema = {
|
|
12
|
+
properties: {
|
|
13
|
+
PORT: {
|
|
14
|
+
type: 'number',
|
|
15
|
+
required: true,
|
|
16
|
+
min: 1,
|
|
17
|
+
max: 65535,
|
|
18
|
+
example: '3000',
|
|
19
|
+
description: 'Application port'
|
|
20
|
+
},
|
|
21
|
+
DATABASE_URL: {
|
|
22
|
+
type: 'url',
|
|
23
|
+
required: true,
|
|
24
|
+
secret: true,
|
|
25
|
+
example: 'https://database.example.com',
|
|
26
|
+
description: 'Database connection URL'
|
|
27
|
+
},
|
|
28
|
+
JWT_SECRET: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
required: true,
|
|
31
|
+
secret: true,
|
|
32
|
+
minLength: 32,
|
|
33
|
+
example: 'replace-with-a-long-random-secret',
|
|
34
|
+
description: 'JWT signing secret'
|
|
35
|
+
},
|
|
36
|
+
NODE_ENV: {
|
|
37
|
+
type: 'enum',
|
|
38
|
+
required: true,
|
|
39
|
+
values: ['development', 'production', 'test'],
|
|
40
|
+
example: 'development',
|
|
41
|
+
description: 'Application environment'
|
|
42
|
+
},
|
|
43
|
+
DEBUG: {
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
required: false,
|
|
46
|
+
example: 'false',
|
|
47
|
+
description: 'Enable debug logging'
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function readJson(filePath) {
|
|
53
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function option(args, name, fallback) {
|
|
57
|
+
const index = args.indexOf(name);
|
|
58
|
+
return index === -1 ? fallback : args[index + 1];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function printIssues(items, prefix) {
|
|
62
|
+
for (const item of items) {
|
|
63
|
+
console.log(`${prefix} ${item.key}: ${item.message}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function usage() {
|
|
68
|
+
console.log(`
|
|
69
|
+
envscope - Environment configuration doctor
|
|
70
|
+
|
|
71
|
+
Usage:
|
|
72
|
+
envscope init [--config envscope.config.json] [--force]
|
|
73
|
+
envscope doctor [--file .env] [--config envscope.config.json]
|
|
74
|
+
envscope validate [--file .env] [--config envscope.config.json]
|
|
75
|
+
envscope audit [--file .env] [--config envscope.config.json]
|
|
76
|
+
envscope redact [--file .env] [--config envscope.config.json]
|
|
77
|
+
envscope compare <file1> <file2>
|
|
78
|
+
envscope example [--config envscope.config.json] [--out .env.example]
|
|
79
|
+
envscope help
|
|
80
|
+
|
|
81
|
+
Commands:
|
|
82
|
+
init Create a starter configuration file
|
|
83
|
+
doctor Validate configuration and audit secrets
|
|
84
|
+
validate Check environment variables against a schema
|
|
85
|
+
audit Look for suspicious or missing secrets
|
|
86
|
+
redact Print environment values with secrets masked
|
|
87
|
+
compare Compare keys and values in two env files
|
|
88
|
+
example Generate an example env file from a schema
|
|
89
|
+
|
|
90
|
+
Options:
|
|
91
|
+
--force Overwrite an existing configuration file when using init
|
|
92
|
+
`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function runCLI(args = process.argv.slice(2)) {
|
|
96
|
+
const [command, ...rest] = args;
|
|
97
|
+
const file = option(rest, '--file', '.env');
|
|
98
|
+
const configFile = option(rest, '--config', 'envscope.config.json');
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
if (!command || command === 'help' || command === '--help') {
|
|
102
|
+
usage();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (command === 'init') {
|
|
107
|
+
const force = rest.includes('--force');
|
|
108
|
+
|
|
109
|
+
if (fs.existsSync(configFile) && !force) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`${configFile} already exists. Use "envscope init --force" to overwrite it.`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fs.writeFileSync(
|
|
116
|
+
configFile,
|
|
117
|
+
`${JSON.stringify(starterSchema, null, 2)}\n`,
|
|
118
|
+
{ flag: force ? 'w' : 'wx' }
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
console.log(`Configuration created: ${configFile}`);
|
|
122
|
+
console.log('Next steps:');
|
|
123
|
+
console.log(' 1. Edit the configuration to match your application.');
|
|
124
|
+
console.log(' 2. Add your environment variables to .env.');
|
|
125
|
+
console.log(' 3. Run: envscope validate');
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (command === 'compare') {
|
|
130
|
+
const files = rest.filter((arg) => !arg.startsWith('--'));
|
|
131
|
+
|
|
132
|
+
if (files.length < 2) {
|
|
133
|
+
throw new Error('Usage: envscope compare <file1> <file2>');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const result = compareEnv(readEnvFile(files[0]), readEnvFile(files[1]));
|
|
137
|
+
console.log('Only in first file:', result.onlyInFirst.join(', ') || 'None');
|
|
138
|
+
console.log('Only in second file:', result.onlyInSecond.join(', ') || 'None');
|
|
139
|
+
console.log('Different values:', result.different.join(', ') || 'None');
|
|
140
|
+
|
|
141
|
+
if (
|
|
142
|
+
result.onlyInFirst.length ||
|
|
143
|
+
result.onlyInSecond.length ||
|
|
144
|
+
result.different.length
|
|
145
|
+
) {
|
|
146
|
+
process.exitCode = 1;
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (command === 'example') {
|
|
152
|
+
const schema = readJson(configFile);
|
|
153
|
+
const output = option(rest, '--out', '.env.example');
|
|
154
|
+
fs.writeFileSync(output, generateExample(schema));
|
|
155
|
+
console.log(`Example environment file written to ${output}`);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!['doctor', 'validate', 'audit', 'redact'].includes(command)) {
|
|
160
|
+
throw new Error(`Unknown command: ${command}. Run "envscope help".`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const env = readEnvFile(file);
|
|
164
|
+
const schema = fs.existsSync(configFile) ? readJson(configFile) : {};
|
|
165
|
+
|
|
166
|
+
if (command === 'redact') {
|
|
167
|
+
console.log(JSON.stringify(redactEnv(env, schema), null, 2));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (command === 'validate' || command === 'doctor') {
|
|
172
|
+
const result = validateEnv(env, schema);
|
|
173
|
+
console.log(`Validation: ${result.valid ? 'PASSED' : 'FAILED'}`);
|
|
174
|
+
printIssues(result.errors, 'ERROR');
|
|
175
|
+
printIssues(result.warnings, 'WARNING');
|
|
176
|
+
|
|
177
|
+
if (!result.valid) process.exitCode = 1;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (command === 'audit' || command === 'doctor') {
|
|
181
|
+
const findings = auditEnv(env, schema);
|
|
182
|
+
console.log(
|
|
183
|
+
`Security audit: ${findings.length ? 'FINDINGS DETECTED' : 'PASSED'}`
|
|
184
|
+
);
|
|
185
|
+
printIssues(findings, 'SECURITY');
|
|
186
|
+
|
|
187
|
+
if (findings.some((item) => item.severity === 'error')) {
|
|
188
|
+
process.exitCode = 1;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error(`envscope: ${error.message}`);
|
|
193
|
+
process.exitCode = 1;
|
|
194
|
+
}
|
|
195
|
+
}
|
package/src/index.js
ADDED
package/src/parser.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
|
|
3
|
+
export function parseEnv(source) {
|
|
4
|
+
const result = {};
|
|
5
|
+
const lines = source.replace(/^\uFEFF/, '').split(/\r?\n/);
|
|
6
|
+
|
|
7
|
+
for (let index = 0; index < lines.length; index++) {
|
|
8
|
+
let line = lines[index].trim();
|
|
9
|
+
|
|
10
|
+
if (!line || line.startsWith('#')) continue;
|
|
11
|
+
|
|
12
|
+
line = line.replace(/^export\s+/, '');
|
|
13
|
+
|
|
14
|
+
const match = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
15
|
+
if (!match) continue;
|
|
16
|
+
|
|
17
|
+
const key = match[1];
|
|
18
|
+
let value = match[2].trim();
|
|
19
|
+
|
|
20
|
+
if (
|
|
21
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
22
|
+
(value.startsWith("'") && value.endsWith("'")) ||
|
|
23
|
+
(value.startsWith('`') && value.endsWith('`'))
|
|
24
|
+
) {
|
|
25
|
+
const quote = value[0];
|
|
26
|
+
value = value.slice(1, -1);
|
|
27
|
+
|
|
28
|
+
if (quote === '"') {
|
|
29
|
+
value = value.replace(/\\n/g, '\n').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
value = value.replace(/\s+#.*$/, '').trim();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
result[key] = value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function readEnvFile(filePath = '.env') {
|
|
42
|
+
if (!fs.existsSync(filePath)) {
|
|
43
|
+
throw new Error(`Environment file not found: ${filePath}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return parseEnv(fs.readFileSync(filePath, 'utf8'));
|
|
47
|
+
}
|
package/src/validator.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const isEmpty = (value) => value === undefined || value === null || value === '';
|
|
2
|
+
|
|
3
|
+
export function validateEnv(env, schema = {}) {
|
|
4
|
+
const errors = [];
|
|
5
|
+
const warnings = [];
|
|
6
|
+
const properties = schema.properties ?? {};
|
|
7
|
+
|
|
8
|
+
for (const [key, rules] of Object.entries(properties)) {
|
|
9
|
+
const value = env[key];
|
|
10
|
+
|
|
11
|
+
if (isEmpty(value)) {
|
|
12
|
+
if (rules.required) {
|
|
13
|
+
errors.push({ key, message: `${key} is required` });
|
|
14
|
+
}
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const type = rules.type ?? 'string';
|
|
19
|
+
|
|
20
|
+
if (type === 'number') {
|
|
21
|
+
const number = Number(value);
|
|
22
|
+
if (!Number.isFinite(number)) {
|
|
23
|
+
errors.push({ key, message: `${key} must be a valid number` });
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (rules.min !== undefined && number < rules.min) {
|
|
27
|
+
errors.push({ key, message: `${key} must be at least ${rules.min}` });
|
|
28
|
+
}
|
|
29
|
+
if (rules.max !== undefined && number > rules.max) {
|
|
30
|
+
errors.push({ key, message: `${key} must be at most ${rules.max}` });
|
|
31
|
+
}
|
|
32
|
+
} else if (type === 'boolean') {
|
|
33
|
+
if (!['true', 'false'].includes(String(value).toLowerCase())) {
|
|
34
|
+
errors.push({ key, message: `${key} must be true or false` });
|
|
35
|
+
}
|
|
36
|
+
} else if (type === 'url') {
|
|
37
|
+
try {
|
|
38
|
+
const parsed = new URL(value);
|
|
39
|
+
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
|
40
|
+
throw new Error('Unsupported URL protocol');
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
errors.push({ key, message: `${key} must be a valid HTTP or HTTPS URL` });
|
|
44
|
+
}
|
|
45
|
+
} else if (type === 'enum') {
|
|
46
|
+
if (!Array.isArray(rules.values) || !rules.values.includes(value)) {
|
|
47
|
+
errors.push({
|
|
48
|
+
key,
|
|
49
|
+
message: `${key} must be one of: ${(rules.values ?? []).join(', ')}`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
} else if (type === 'string') {
|
|
53
|
+
if (rules.minLength !== undefined && value.length < rules.minLength) {
|
|
54
|
+
errors.push({ key, message: `${key} must contain at least ${rules.minLength} characters` });
|
|
55
|
+
}
|
|
56
|
+
if (rules.maxLength !== undefined && value.length > rules.maxLength) {
|
|
57
|
+
errors.push({ key, message: `${key} must contain at most ${rules.maxLength} characters` });
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
errors.push({ key, message: `${key} uses unsupported schema type: ${type}` });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (Array.isArray(rules.values) && type !== 'enum' && !rules.values.includes(value)) {
|
|
64
|
+
errors.push({ key, message: `${key} must be one of: ${rules.values.join(', ')}` });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const key of Object.keys(env)) {
|
|
69
|
+
if (!(key in properties)) {
|
|
70
|
+
warnings.push({ key, message: `${key} is not defined in the schema` });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
valid: errors.length === 0,
|
|
76
|
+
errors,
|
|
77
|
+
warnings
|
|
78
|
+
};
|
|
79
|
+
}
|