Skip to content

Commit

Permalink
📝 Update README and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuki0918 committed Jan 16, 2025
1 parent 2844a67 commit 54986b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ npm install dotenv-zod-validator
NODE_ENV="development"
PORT="3000"
BOOLEAN_FLAG="true"
__OTHER__="__other__"
```

code
Expand All @@ -30,14 +31,15 @@ import { zenv } from "dotenv-zod-validator";
const schema = zenv.object({
NODE_ENV: zenv.enum(["development", "production", "test"]),
PORT: zenv.number(),
OPTIONAL_VAR: zenv.string().optional(),
BOOLEAN_FLAG: zenv.boolean(),
OPTIONAL_VAR: zenv.string().optional(),
});

const ENV = zenv.validate(schema);
// NODE_ENV: "development"
// PORT: 3000
// BOOLEAN_FLAG: true
// OPTIONAL_VAR: undefined

// Cannot assign to 'NODE_ENV' because it is a read-only property.
// ENV.NODE_ENV = "production"
Expand Down
26 changes: 14 additions & 12 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@ import { zenv } from "../src";
describe("zenv.validate", () => {
it("should validate correct environment variables", () => {
const schema = zenv.object({
NODE_ENV: zenv.enum(["development", "production", "test"]),
PORT: zenv.number(),
DEBUG_MODE: zenv.boolean(),
APP_ENV: zenv.enum(["development", "production", "test"]),
BOOLEAN_FLAG: zenv.boolean(),
OPTIONAL_VAR: zenv.string().optional(),
});

const env = {
NODE_ENV: "development",
PORT: "3000",
DEBUG_MODE: "true",
APP_ENV: "development",
BOOLEAN_FLAG: "true",
__OTHER__: "__other__",
};

const result = zenv.validate(schema, env);

expect(result).toEqual({
NODE_ENV: "development",
PORT: 3000,
DEBUG_MODE: true,
APP_ENV: "development",
BOOLEAN_FLAG: true,
});
});

it("should not access environment variables if schema is not defined", () => {
const schema = zenv.object({});
const env = {
NODE_ENV: "development",
PORT: "3000",
DEBUG_MODE: "true",
APP_ENV: "development",
BOOLEAN_FLAG: "true",
};

const result = zenv.validate(schema, env);
Expand Down Expand Up @@ -61,15 +63,15 @@ describe("zenv.validate", () => {

it("should throw an error for invalid environment variables", () => {
const schema = zenv.object({
NODE_ENV: zenv.enum(["development", "production", "test"]),
PORT: zenv.number(),
DEBUG_MODE: zenv.boolean(),
APP_ENV: zenv.enum(["development", "production", "test"]),
BOOLEAN_FLAG: zenv.boolean(),
});

const invalidEnv = {
NODE_ENV: "invalid_env",
PORT: "not_a_number",
DEBUG_MODE: "invalid_boolean",
APP_ENV: "invalid_env",
BOOLEAN_FLAG: "invalid_boolean",
};

expect(() => zenv.validate(schema, invalidEnv)).toThrow(
Expand Down

0 comments on commit 54986b0

Please sign in to comment.