From 9e73ecc60a668f1606a769809766b48a445b9d8a Mon Sep 17 00:00:00 2001 From: kakasoo Date: Fri, 20 Feb 2026 14:17:58 +0900 Subject: [PATCH] fix: prevent `deepStrictAssert` crash on non-leaf object keys Add `rest.length > 0` guard before recursing into nested objects, consistent with the existing fix in `deepStrictPick`. Closes #19. Co-Authored-By: Claude Opus 4.6 --- src/functions/DeepStrictAssert.ts | 4 ++-- test/features/DeepStrictAssert.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/functions/DeepStrictAssert.ts b/src/functions/DeepStrictAssert.ts index 4fb8d92..3034f66 100644 --- a/src/functions/DeepStrictAssert.ts +++ b/src/functions/DeepStrictAssert.ts @@ -33,7 +33,7 @@ export const deepStrictAssert = if (input instanceof Array) { const elements = input.map((element) => { if (first in element) { - if (typeof element[first] === 'object' && element[first] !== null) { + if (typeof element[first] === 'object' && element[first] !== null && rest.length > 0) { return { [first]: traverse(element[first], rest) }; } @@ -46,7 +46,7 @@ export const deepStrictAssert = return elements; } else { if (first in input) { - if (typeof input[first] === 'object' && input[first] !== null) { + if (typeof input[first] === 'object' && input[first] !== null && rest.length > 0) { return { [first]: traverse(input[first], rest) }; } return { [first]: input[first] }; diff --git a/test/features/DeepStrictAssert.ts b/test/features/DeepStrictAssert.ts index fc9229d..094c387 100644 --- a/test/features/DeepStrictAssert.ts +++ b/test/features/DeepStrictAssert.ts @@ -1,3 +1,4 @@ +import { ok } from 'assert'; import typia, { tags } from 'typia'; import { deepStrictAssert } from '../../src'; @@ -130,3 +131,14 @@ export function test_functions_deepStrictAssert_accesses_multiple_nested_array_p typia.assertEquals(resultD); typia.assertEquals(resultE); } + +/** + * Tests that deepStrictAssert can access a non-leaf object key without crashing. + * This verifies the fix for GitHub issue #19. + */ +export function test_functions_deepStrictAssert_accesses_non_leaf_object_key() { + const data = { user: { name: 'Alice', age: 30 } }; + const result = deepStrictAssert(data)('user'); + ok(result.user.name === 'Alice'); + ok(result.user.age === 30); +}