Skip to content

Commit

Permalink
MINOR: Improved range of accepted JSONPath expressions in JSONText::i…
Browse files Browse the repository at this point in the history
…sValidExpression()

including test cases.
  • Loading branch information
phptek committed Jul 15, 2016
1 parent c121e31 commit 25699d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion code/models/fieldtypes/JSONText.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public function isValidOperator($operator)
*/
public function isValidExpression($expression)
{
return (bool) preg_match("#^\\$\.#", $expression);
return (bool) preg_match("#^(\\*|\[\d:\d:\d\]|\\$\.+[^\d]+)#", $expression);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/JSONTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ class JSONTextTest extends SapphireTest
{
/**
* @todo There are a ton more permutations of a JSONPath regex
* See the walk() method in JSONStore
* See the trace() method in JSONPath for more examples to work from
*/
public function testIsValidExpression()
{
$field = JSONText::create('MyJSON');

$this->assertTrue($field->isValidExpression('$..'));
$this->assertTrue($field->isValidExpression('*'));
$this->assertTrue($field->isValidExpression('$.[2]'));
$this->assertTrue($field->isValidExpression('$.cars.american[*]'));
$this->assertTrue($field->isValidExpression('[0:1:1]'));
$this->assertFalse($field->isValidExpression('[0:1:]'));
$this->assertFalse($field->isValidExpression('[0:1:1'));
$this->assertFalse($field->isValidExpression(''));
$this->assertFalse($field->isValidExpression('$.1.cars.american[*]'));
$this->assertFalse($field->isValidExpression('$'));
$this->assertFalse($field->isValidExpression('$[2]'));
}
Expand Down

0 comments on commit 25699d5

Please sign in to comment.