From 13ba07b7d2928ac5772cb637cb8b4279be737142 Mon Sep 17 00:00:00 2001 From: Dan Hensby Date: Wed, 18 Nov 2026 23:10:26 +0000 Subject: [PATCH] fix: trim a value before boolean coercion The parser keeps the surrounding whitespace of a quoted value. Boolean coercion then compared the value with `false`, `no` and `0` without trimming it. So `TrustServerCertificate={ no }` and `Encrypt=" false "` gave true. Boolean coercion now trims the value first. SqlClient also trims the value of its boolean keywords, such as TrustServerCertificate. A quoted value of only whitespace now gives false, as an empty value does. Values that are not recognised still give true. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 ++ src/connection-string.ts | 7 +++++-- test/connection-string.test.ts | 10 ++++++++++ test/parser/fixture/sql-strings.json | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 50e8155..ae0d008 100644 --- a/README.md +++ b/README.md @@ -120,3 +120,5 @@ console.log(parsed.get('a number', 'number')); // 987 // coersion will be permissive in its type coersion console.log(parsed.get('a number', 'boolean')); // true ``` + +`get(key, 'boolean')` gives `false` for `false`, `no` and `0`, in any case and with any surrounding whitespace, and for an empty value. Any other value gives `true`. `get(key, 'number')` uses `parseInt()`. For an empty value, `toSchema()` uses the schema default instead (see [Custom schemas](#custom-schemas)). diff --git a/src/connection-string.ts b/src/connection-string.ts index 1b28328..77342bc 100644 --- a/src/connection-string.ts +++ b/src/connection-string.ts @@ -50,8 +50,11 @@ export class ConnectionString implements ReadonlyMap { return val as CoerceTypeMap[T]; } switch (actualType) { - case 'boolean': - return (['false', 'no', '0'].includes(val.toLowerCase()) ? false : !!val) as CoerceTypeMap[T]; + case 'boolean': { + // a quoted value keeps its surrounding whitespace, so trim it before the match, as SqlClient does + const bool = val.trim().toLowerCase(); + return (['false', 'no', '0'].includes(bool) ? false : !!bool) as CoerceTypeMap[T]; + } case 'number': return parseInt(val, 10) as CoerceTypeMap[T]; default: diff --git a/test/connection-string.test.ts b/test/connection-string.test.ts index d44dd53..a754e6e 100644 --- a/test/connection-string.test.ts +++ b/test/connection-string.test.ts @@ -45,6 +45,16 @@ describe('connection-string', () => { assert.equal(parsed.get('is0', 'boolean'), false); assert.equal(parsed.get('is1', 'boolean'), true); }); + it('trims a boolean value before coercing it', () => { + const parsed = new ConnectionString('a={ false };b=" no ";c=\' 0\';d={\tFALSE\t};e={ true };f=" yes ";g={ }'); + assert.equal(parsed.get('a', 'boolean'), false); + assert.equal(parsed.get('b', 'boolean'), false); + assert.equal(parsed.get('c', 'boolean'), false); + assert.equal(parsed.get('d', 'boolean'), false); + assert.equal(parsed.get('e', 'boolean'), true); + assert.equal(parsed.get('f', 'boolean'), true); + assert.equal(parsed.get('g', 'boolean'), false); + }); it('returns undefined for missing value', () => { const parsed = new ConnectionString(connectionString); assert.equal(parsed.get('missing'), undefined); diff --git a/test/parser/fixture/sql-strings.json b/test/parser/fixture/sql-strings.json index d08ad45..8595fad 100644 --- a/test/parser/fixture/sql-strings.json +++ b/test/parser/fixture/sql-strings.json @@ -27,6 +27,16 @@ } } }, + { + "name": "trims quoted boolean values", + "connection_string": "Server=h;TrustServerCertificate={ no };MultiSubnetFailover=\" false \";Encrypt=' yes '", + "expected": { + "data source": "h", + "trustservercertificate": false, + "multisubnetfailover": false, + "encrypt": true + } + }, { "name": "uses the default for an empty boolean or number value", "connection_string": "Server=h;TrustServerCertificate=;Connect Timeout=;Database=d",