From 2c49b2b711c1c1ef9104d513577d0d593b64fdc3 Mon Sep 17 00:00:00 2001 From: Dan Hensby Date: Wed, 18 Nov 2026 20:16:10 +0000 Subject: [PATCH] fix: use the schema default for an empty boolean or number value toSchema() coerced an empty value as if it were set. An empty boolean value became false, and an empty number value became NaN. SqlClient reads an empty number value, such as `Connect Timeout=`, as not set, and uses the default. toSchema() now gives the schema default for an empty, or whitespace only, value of a boolean or number key. For example, with MSSQL_SCHEMA, `Connect Timeout=` now gives 15. The key stays in the result, with the value `undefined` if the schema has no default. An empty value of a string key does not change, so an empty password is still an empty string. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +++ src/connection-string.ts | 7 ++++- test/connection-string.test.ts | 45 +++++++++++++++++++++++++++- test/parser/fixture/sql-strings.json | 37 +++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5bbdd39..50e8155 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,10 @@ Output: } ``` +Keys that are not in the connection string are left out. To include them with their `default` value, use `toSchema(schema, { includeMissing: true })`. + +An empty value for a `boolean` or `number` key, such as `a number=;`, means that the value is not set. The key is included with its `default` value (or `undefined` if it has no default). An empty value for a `string` key stays an empty string. + ## Accessing properties The parsed connection string object is a readonly `Map` with an overloadded `get()` method allowing diff --git a/src/connection-string.ts b/src/connection-string.ts index 064713b..1b28328 100644 --- a/src/connection-string.ts +++ b/src/connection-string.ts @@ -90,13 +90,18 @@ export class ConnectionString implements ReadonlyMap { }); } + // an empty boolean or number value is not set, so it gets the schema default + #isSet(key: string, type?: CoerceType): boolean { + return (type !== 'boolean' && type !== 'number') || !!this.#parsed.get(key.toLowerCase())?.trim(); + } + // a way to extract a formatted object from the connection string toSchema(schema: T, { includeMissing }: { includeMissing?: boolean } = {}): InferSchema { return Object.fromEntries(Object.entries(schema).reduce((props, [key, { type, default: defaultValue, aliases }]) => { // try to find the property const prop = [key, ...aliases ?? []].find((k) => this.has(k)); if (prop || includeMissing) { - props.push([key, prop ? this.get(prop.toLowerCase(), type) : defaultValue] as [keyof T, CoerceType]); + props.push([key, prop && this.#isSet(prop, type as CoerceType | undefined) ? this.get(prop.toLowerCase(), type) : defaultValue] as [keyof T, CoerceType]); } return props; }, [] as [keyof T, CoerceType][])) as InferSchema; diff --git a/test/connection-string.test.ts b/test/connection-string.test.ts index b01e588..d44dd53 100644 --- a/test/connection-string.test.ts +++ b/test/connection-string.test.ts @@ -1,6 +1,6 @@ import * as assert from 'node:assert'; import { describe, it } from 'node:test'; -import { ConnectionString } from '../src/connection-string'; +import { ConnectionString, type SchemaDefinition } from '../src/connection-string'; describe('connection-string', () => { const connectionString = 'keyword=value;isTrue=true;isYes=yes;isFalse=false;isNo=no;is0=0;is1=1'; @@ -98,6 +98,49 @@ describe('connection-string', () => { ]); }); }); + describe('.toSchema()', () => { + const schema = { + 'a boolean': { type: 'boolean' as const, default: true }, + 'a number': { type: 'number' as const, default: 15 }, + 'a string': { type: 'string' as const, default: 'default' }, + 'no default': { type: 'boolean' as const }, + }; + it('coerces values that are set', () => { + const parsed = new ConnectionString('a boolean=false;a number=30;a string=value;no default=yes'); + assert.deepEqual(parsed.toSchema(schema), { + 'a boolean': false, + 'a number': 30, + 'a string': 'value', + 'no default': true, + }); + }); + it('uses the default for an empty boolean or number value, and keeps the key', () => { + const parsed = new ConnectionString('a boolean=;a number= ;a string=;no default='); + const result = parsed.toSchema(schema); + assert.deepEqual(result, { + 'a boolean': true, + 'a number': 15, + 'a string': '', + 'no default': undefined, + }); + assert.ok('no default' in result); + }); + it('still throws for an empty value with an unsupported type', () => { + const parsed = new ConnectionString('when='); + const badSchema = { when: { type: 'date', default: 'D' } } as unknown as SchemaDefinition; + assert.throws(() => parsed.toSchema(badSchema), { message: 'Coerce type not supported' }); + }); + it('includes missing keys with their defaults when asked', () => { + const parsed = new ConnectionString('a number=30'); + assert.deepEqual(parsed.toSchema(schema), { 'a number': 30 }); + assert.deepEqual(parsed.toSchema(schema, { includeMissing: true }), { + 'a boolean': true, + 'a number': 30, + 'a string': 'default', + 'no default': undefined, + }); + }); + }); describe('.toString()', () => { it('returns the connection string', () => { const parsed = new ConnectionString(connectionString); diff --git a/test/parser/fixture/sql-strings.json b/test/parser/fixture/sql-strings.json index 8f93951..d08ad45 100644 --- a/test/parser/fixture/sql-strings.json +++ b/test/parser/fixture/sql-strings.json @@ -27,6 +27,43 @@ } } }, + { + "name": "uses the default for an empty boolean or number value", + "connection_string": "Server=h;TrustServerCertificate=;Connect Timeout=;Database=d", + "expected": { + "data source": "h", + "trustservercertificate": false, + "connection timeout": 15, + "initial catalog": "d" + } + }, + { + "name": "uses the default for a boolean or number value of only whitespace", + "connection_string": "Server=h;TrustServerCertificate={ };MultiSubnetFailover={ };Packet Size=' '", + "expected": { + "data source": "h", + "trustservercertificate": false, + "multisubnetfailover": false, + "packet size": 8000 + } + }, + { + "name": "keeps an empty string value", + "connection_string": "Server=h;Password=;Application Name={ }", + "expected": { + "data source": "h", + "password": "", + "application name": " " + } + }, + { + "name": "uses the default for an empty value given by an alias", + "connection_string": "Server=h;Timeout=", + "expected": { + "data source": "h", + "connection timeout": 15 + } + }, { "name": "parses a string value that starts with numbers", "connection_string": "client id=1e92dd5a-091f-4b2b-bc1b-8461ced6a177;client secret=55a4ef49-bef5-4669-818b-ff148c0c0db9;tenant id=5dc09092-ee09-4e5c-9515-ba937415e9ff;",