Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Extractors/JsonExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Units/JsonExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]'],
];
}

Expand All @@ -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'],
];
}

Expand Down