-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,956 additions
and
1,701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ const nodeEnv: Validator<"development" | "test" | "production"> = (value) => { | |
} | ||
}; | ||
|
||
const email: Validator<string> = (value = "") => { | ||
if (/.+@.+\..+/.test(value)) { | ||
const hex: Validator<string> = (value = "") => { | ||
if (/^[A-F\d]+$/i.test(value)) { | ||
return value; | ||
} | ||
}; | ||
|
@@ -19,95 +19,81 @@ const url: Validator<URL> = (value = "") => { | |
} catch {} // eslint-disable-line no-empty | ||
}; | ||
|
||
const port: Validator<number> = (value = "") => { | ||
const number = Number.parseInt(value); | ||
|
||
if (number > 0 && number < 65536) { | ||
return number; | ||
} | ||
}; | ||
|
||
test("with valid input", () => { | ||
const input = { | ||
NODE_ENV: "test", | ||
USER_EMAIL: "[email protected]", | ||
COOKIE_KEY: "aba4a6fb2222ef28d81e4be445a51fba", | ||
SERVER_URL: "https://github.com", | ||
SERVER_PORT: "3000", | ||
}; | ||
|
||
const output = validate({ | ||
env: input, | ||
validators: { | ||
NODE_ENV: nodeEnv, | ||
USER_EMAIL: email, | ||
COOKIE_KEY: hex, | ||
SERVER_URL: url, | ||
SERVER_PORT: port, | ||
}, | ||
}); | ||
|
||
expect(output).toStrictEqual({ | ||
NODE_ENV: "test", | ||
USER_EMAIL: "[email protected]", | ||
COOKIE_KEY: "aba4a6fb2222ef28d81e4be445a51fba", | ||
SERVER_URL: new URL("https://github.com"), | ||
SERVER_PORT: 3000, | ||
}); | ||
}); | ||
|
||
test("with invalid input", () => { | ||
const input = { | ||
NODE_ENV: "staging", | ||
USER_EMAIL: "mathieu", | ||
SERVER_URL: "youtube", | ||
SERVER_PORT: "three thousand", | ||
COOKIE_KEY: "invalid hex", | ||
}; | ||
|
||
try { | ||
validate({ | ||
env: input, | ||
validators: { | ||
NODE_ENV: nodeEnv, | ||
USER_EMAIL: email, | ||
SERVER_URL: url, | ||
SERVER_PORT: port, | ||
COOKIE_KEY: hex, | ||
}, | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(EnvValidationError); | ||
|
||
expect((error as EnvValidationError).message).toBe( | ||
"Some environment variables cannot be validated: NODE_ENV, USER_EMAIL, SERVER_URL, SERVER_PORT", | ||
"Some environment variables cannot be validated: NODE_ENV, SERVER_URL, COOKIE_KEY", | ||
); | ||
|
||
expect((error as EnvValidationError).variables).toStrictEqual([ | ||
"NODE_ENV", | ||
"USER_EMAIL", | ||
"SERVER_URL", | ||
"SERVER_PORT", | ||
"COOKIE_KEY", | ||
]); | ||
} | ||
}); | ||
|
||
test("with invalid overrides", () => { | ||
// overrides are not validated, it will not throw | ||
const input = { | ||
COOKIE_KEY: "aba4a6fb2222ef28d81e4be445a51fba", | ||
SERVER_URL: "https://github.com", | ||
SERVER_PORT: "3000", | ||
}; | ||
|
||
const output = validate({ | ||
env: input, | ||
validators: { | ||
COOKIE_KEY: hex, | ||
SERVER_URL: url, | ||
SERVER_PORT: port, | ||
}, | ||
overrides: { | ||
COOKIE_KEY: "invalid hex", | ||
SERVER_URL: new URL("https://github.com"), | ||
SERVER_PORT: 0, | ||
}, | ||
}); | ||
|
||
expect(output).toStrictEqual({ | ||
COOKIE_KEY: "invalid hex", | ||
SERVER_URL: new URL("https://github.com"), | ||
SERVER_PORT: 0, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,13 @@ import { expect, test } from "vitest"; | |
import { | ||
EnvValidationError, | ||
boolean, | ||
email, | ||
number, | ||
oneOf, | ||
optional, | ||
port, | ||
string, | ||
url, | ||
validate, | ||
} from "../src"; | ||
|
||
|
@@ -15,6 +18,9 @@ test("with valid input", () => { | |
BAR: "42", | ||
BAZ: "true", | ||
QUX: "a", | ||
QUUX: "https://swan.io", | ||
FRED: "8080", | ||
THUD: "[email protected]", | ||
}; | ||
|
||
const output = validate({ | ||
|
@@ -24,6 +30,9 @@ test("with valid input", () => { | |
BAR: number, | ||
BAZ: boolean, | ||
QUX: oneOf("a", "b"), | ||
QUUX: url, | ||
FRED: port, | ||
THUD: email, | ||
}, | ||
}); | ||
|
||
|
@@ -32,6 +41,9 @@ test("with valid input", () => { | |
BAR: 42, | ||
BAZ: true, | ||
QUX: "a", | ||
QUUX: "https://swan.io", | ||
FRED: 8080, | ||
THUD: "[email protected]", | ||
}); | ||
}); | ||
|
||
|
@@ -60,6 +72,9 @@ test("with invalid env variables", () => { | |
BAR: "bar", | ||
BAZ: "baz", | ||
QUX: "c", | ||
QUUX: "swan.io", | ||
FRED: 72000, | ||
THUD: "john-doe.com", | ||
}; | ||
|
||
try { | ||
|
@@ -70,12 +85,23 @@ test("with invalid env variables", () => { | |
BAR: number, | ||
BAZ: boolean, | ||
QUX: oneOf("a", "b"), | ||
QUUX: url, | ||
FRED: port, | ||
THUD: email, | ||
}, | ||
}); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(EnvValidationError); | ||
const error = e as EnvValidationError; | ||
expect(error.variables).toStrictEqual(["BAR", "BAZ", "QUX"]); | ||
|
||
expect(error.variables).toStrictEqual([ | ||
"BAR", | ||
"BAZ", | ||
"QUX", | ||
"QUUX", | ||
"FRED", | ||
"THUD", | ||
]); | ||
} | ||
}); | ||
|
||
|
@@ -130,7 +156,7 @@ test("with overrides", () => { | |
const input = { | ||
FOO: "foo", | ||
BAR: "42", | ||
BAZ: "true", | ||
BAZ: "1", | ||
}; | ||
|
||
const output = validate({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "valienv", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"license": "MIT", | ||
"description": "A simple environment variables validator for Node.js, web browsers and React Native", | ||
"author": "Mathieu Acthernoene <[email protected]>", | ||
|
@@ -41,17 +41,19 @@ | |
"not dead" | ||
], | ||
"prettier": { | ||
"trailingComma": "all" | ||
"plugins": [ | ||
"prettier-plugin-organize-imports" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.2.5", | ||
"@typescript-eslint/eslint-plugin": "^5.59.9", | ||
"@typescript-eslint/parser": "^5.59.9", | ||
"eslint": "^8.42.0", | ||
"@types/node": "^20.11.20", | ||
"@typescript-eslint/eslint-plugin": "^7.0.2", | ||
"@typescript-eslint/parser": "^7.0.2", | ||
"eslint": "^8.56.0", | ||
"microbundle": "^0.15.1", | ||
"prettier": "^2.8.8", | ||
"prettier-plugin-organize-imports": "^3.2.2", | ||
"typescript": "^5.1.3", | ||
"vitest": "^0.32.0" | ||
"prettier": "^3.2.5", | ||
"prettier-plugin-organize-imports": "^3.2.4", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.