From 008173a892b678aad153617f0a2457a4ffd0f1e1 Mon Sep 17 00:00:00 2001 From: Aleksey Kuznetsov Date: Mon, 22 Jul 2024 11:36:58 +0300 Subject: [PATCH 1/4] feat: handle "yes"/"no" as valid boolean type --- src/validators.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/validators.ts b/src/validators.ts index f2d6977..437bcc1 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -34,11 +34,13 @@ export const bool = makeExactValidator((input: string | boolean) => { case true: case 'true': case 't': + case 'yes': case '1': return true case false: case 'false': case 'f': + case 'no': case '0': return false default: From 93f8c5658f01793308c003abeebfa5407fa568ca Mon Sep 17 00:00:00 2001 From: Aleksey Kuznetsov Date: Mon, 22 Jul 2024 11:39:18 +0300 Subject: [PATCH 2/4] docs: update documentation for bool validator --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 06922f7..e34c3e5 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ URL, email address). To these ends, the following validation functions are avail - `str()` - Passes string values through, will ensure a value is present unless a `default` value is given. Note that an empty string is considered a valid value - if this is undesirable you can easily create your own validator (see below) -- `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f"` into booleans +- `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f", "yes", "no"` into booleans - `num()` - Parses an env var (eg. `"42", "0.23", "1e5"`) into a Number - `email()` - Ensures an env var is an email address - `host()` - Ensures an env var is either a domain name or an ip address (v4 or v6) From 598c5ad18a254314bdd3b19a7fcd29331d9bf5d9 Mon Sep 17 00:00:00 2001 From: Aleksey Kuznetsov Date: Wed, 24 Jul 2024 14:57:40 +0300 Subject: [PATCH 3/4] feat: add test for yes/no values for bool validator --- tests/validators.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/validators.test.ts b/tests/validators.test.ts index 2787d08..94c2d0d 100644 --- a/tests/validators.test.ts +++ b/tests/validators.test.ts @@ -28,6 +28,11 @@ test('bool() works with various formats', () => { const f = cleanEnv({ FOO: 'f' }, { FOO: bool() }) expect(f).toEqual({ FOO: false }) + const yes = cleanEnv({ FOO: 'yes'}, { FOO: bool() }) + expect(yes).toEqual({ FOO: true }) + const no = cleanEnv({ FOO: 'no' }, { FOO: bool() }) + expect(no).toEqual({ FOO: false }) + const defaultF = cleanEnv({}, { FOO: bool({ default: false }) }) expect(defaultF).toEqual({ FOO: false }) }) From e12b5de402c311383695dbb6d60e29d86b95017a Mon Sep 17 00:00:00 2001 From: Aleksey Kuznetsov Date: Wed, 31 Jul 2024 00:45:25 +0300 Subject: [PATCH 4/4] feat: handle "on"/"off" as valid boolean type --- README.md | 2 +- src/validators.ts | 2 ++ tests/validators.test.ts | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e34c3e5..274ce23 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ URL, email address). To these ends, the following validation functions are avail - `str()` - Passes string values through, will ensure a value is present unless a `default` value is given. Note that an empty string is considered a valid value - if this is undesirable you can easily create your own validator (see below) -- `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f", "yes", "no"` into booleans +- `bool()` - Parses env var strings `"1", "0", "true", "false", "t", "f", "yes", "no", "on", "off"` into booleans - `num()` - Parses an env var (eg. `"42", "0.23", "1e5"`) into a Number - `email()` - Ensures an env var is an email address - `host()` - Ensures an env var is either a domain name or an ip address (v4 or v6) diff --git a/src/validators.ts b/src/validators.ts index 437bcc1..3272bce 100644 --- a/src/validators.ts +++ b/src/validators.ts @@ -35,12 +35,14 @@ export const bool = makeExactValidator((input: string | boolean) => { case 'true': case 't': case 'yes': + case 'on': case '1': return true case false: case 'false': case 'f': case 'no': + case 'off': case '0': return false default: diff --git a/tests/validators.test.ts b/tests/validators.test.ts index 94c2d0d..aca82e1 100644 --- a/tests/validators.test.ts +++ b/tests/validators.test.ts @@ -33,6 +33,11 @@ test('bool() works with various formats', () => { const no = cleanEnv({ FOO: 'no' }, { FOO: bool() }) expect(no).toEqual({ FOO: false }) + const on = cleanEnv({ FOO: 'on'}, { FOO: bool() }) + expect(on).toEqual({ FOO: true }) + const off = cleanEnv({ FOO: 'off' }, { FOO: bool() }) + expect(off).toEqual({ FOO: false }) + const defaultF = cleanEnv({}, { FOO: bool({ default: false }) }) expect(defaultF).toEqual({ FOO: false }) })