From 9e85b942dd0e64741d48dc5121dc6aff9b2ca6d9 Mon Sep 17 00:00:00 2001 From: elwafa Date: Wed, 12 Feb 2025 21:33:16 +0200 Subject: [PATCH 1/2] feat(validation): Support array indexing in JSON path validation - Updated the JSON path validation regex to allow array indexing (e.g., `$.data[0].attribute`). - Ensured compatibility with dot notation for nested objects (e.g., `$.data.object.attribute`). - Improved validation to support mixed array and object paths (e.g., `$.data[0].nested[2].value`). - Added stricter checks to prevent malformed paths. --- src/Extractors/JsonExtractor.php | 5 +++-- tests/Units/JsonExtractorTest.php | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Extractors/JsonExtractor.php b/src/Extractors/JsonExtractor.php index c7c6fb2..7a1cb17 100644 --- a/src/Extractors/JsonExtractor.php +++ b/src/Extractors/JsonExtractor.php @@ -44,10 +44,11 @@ public function validate(): bool if (empty($this->selector) || $this->selector === '$.') { throw new InvalidJsonPathException('JSON path cannot be empty'); } - // Validate the selector ex: $.meta.token + // Validate the selector ex: $.meta.token or $.data[0].name // Validate the selector follows proper JSON path format // Should start with $ followed by dot and valid path segments - if (! preg_match('/^\$(\.[a-zA-Z0-9_]+)*$/', $this->selector)) { + $pattern = '/^\$(\.[a-zA-Z0-9_]+|\[[0-9]+\])*$/'; + if (! preg_match($pattern, $this->selector)) { throw new InvalidJsonPathException('Invalid JSON path'); } diff --git a/tests/Units/JsonExtractorTest.php b/tests/Units/JsonExtractorTest.php index b16c437..1e70778 100644 --- a/tests/Units/JsonExtractorTest.php +++ b/tests/Units/JsonExtractorTest.php @@ -46,6 +46,9 @@ public static function validJsonPathProvider(): array ['with_underscore'], ['with.numbers.123'], ['mixed.path_with.numbers123'], + ['mixed[0].path'], + ['mixed[0].path[1]'], + ['mixed[0].path[1].with[2].numbers[3]'], ]; } @@ -54,7 +57,7 @@ public function testInvalidJsonPaths(string $jsonPath): void { $this->expectException(InvalidJsonPathException::class); $extractor = new JsonExtractor('testVar', $jsonPath); - $extractor->validate(); + $extractor->validate(true); } public static function invalidJsonPathProvider(): array @@ -67,6 +70,12 @@ public static function invalidJsonPathProvider(): array ['$invalid.start'], ['invalid$.middle'], ['path.with.$'], + ['$.data[abc]'], + ['$.data[0].name[abc]'], + ['$.data[0].name[0].'], + ['$.data[0].name[0].[1]'], + ['$.data[0].name[0].[1].'], + ['$.data[0].name[0].[1].name'], ]; } From dbb382dae96151f0d19d83a0c1adf3f99d96c0d8 Mon Sep 17 00:00:00 2001 From: elwafa Date: Wed, 12 Feb 2025 21:35:04 +0200 Subject: [PATCH 2/2] remove unused code --- tests/Units/JsonExtractorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Units/JsonExtractorTest.php b/tests/Units/JsonExtractorTest.php index 1e70778..6a7dcfb 100644 --- a/tests/Units/JsonExtractorTest.php +++ b/tests/Units/JsonExtractorTest.php @@ -57,7 +57,7 @@ public function testInvalidJsonPaths(string $jsonPath): void { $this->expectException(InvalidJsonPathException::class); $extractor = new JsonExtractor('testVar', $jsonPath); - $extractor->validate(true); + $extractor->validate(); } public static function invalidJsonPathProvider(): array