Skip to content

Add JSONPath validation #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased v2.x]

### Added

- Path validation for JSON fields

### Changed

- (dev) Update PHP-CS-Fixer to version `3.12.0`
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"php": "^7.2 || ^8.0",
"ext-intl": "*",
"composer/semver": "^3.2",
"respect/validation": "^2.0"
"respect/validation": "^2.0",
"softcreatr/jsonpath": "^0.7.6 || ^0.8.1"
},
"require-dev": {
"ext-mbstring": "*",
Expand Down
33 changes: 33 additions & 0 deletions src/Exception/InvalidJSONPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\Exception;

use Exception;
use InvalidArgumentException;

class InvalidJSONPathException extends InvalidArgumentException
{
public function __construct(string $path, Exception $previous = null)
{
parent::__construct(sprintf('The provided JSON Path is not valid. (path: "%s")', $path), 0, $previous);
}
}
28 changes: 28 additions & 0 deletions src/Redis/Command/CreateCommand/JSONFieldOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace MacFJA\RediSearch\Redis\Command\CreateCommand;

use function count;

use Flow\JSONPath\JSONPath;
use Flow\JSONPath\JSONPathException;
use MacFJA\RediSearch\Exception\InvalidJSONPathException;
use MacFJA\RediSearch\Redis\Command\Option\AbstractCommandOption;

class JSONFieldOption extends AbstractCommandOption implements CreateCommandJSONFieldOption
Expand All @@ -36,6 +41,9 @@ public function __construct(string $path, CreateCommandFieldOption $decorated)
parent::__construct('>=2.2.0');
$this->path = $path;
$this->decorated = $decorated;
if (!self::isPathValid($path)) {
throw new InvalidJSONPathException($path);
}
}

public function isValid(): bool
Expand Down Expand Up @@ -63,6 +71,26 @@ public function getJSONPath(): string
return $this->path;
}

/**
* @throws InvalidJSONPathException
*/
public static function isPathValid(string $path): bool
{
if (0 === strpos($path, '$')) {
$parser = new JSONPath();

try {
$result = $parser->parseTokens($path);

return count($result) > 0;
} catch (JSONPathException $e) {
throw new InvalidJSONPathException($path, $e);
}
}

return false;
}

protected function doRender(?string $version): array
{
return array_merge([$this->path, 'AS'], $this->decorated->render($version));
Expand Down
74 changes: 74 additions & 0 deletions tests/Redis/Command/CreateCommand/JSONFieldOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\Redis\Command\CreateCommand;

use MacFJA\RediSearch\Exception\InvalidJSONPathException;
use MacFJA\RediSearch\Redis\Command\CreateCommand\JSONFieldOption;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @covers \MacFJA\RediSearch\Exception\InvalidJSONPathException
* @covers \MacFJA\RediSearch\Redis\Command\CreateCommand\JSONFieldOption
*/
class JSONFieldOptionTest extends TestCase
{
/**
* @dataProvider dataProvider
*/
public function testIsPathValid(string $input, bool $expected): void
{
static::assertEquals($expected, JSONFieldOption::isPathValid($input));
}

/**
* @dataProvider dataProvider
*/
public function testIsPathValidException(string $input, bool $expected): void
{
$this->expectException(InvalidJSONPathException::class);
static::assertEquals($expected, JSONFieldOption::isPathValid($input));
}

/**
* @return array<string, array<bool|string>>
*/
public function dataProvider(string $testName): array
{
if ('testIsPathValid' === $testName) {
return [
'2nd element of root' => ['$.[1]', true],
'2nd element' => ['$[1]', true],
'multilevel' => ['$.basic.dict.new_child_1', true],
'empty 2' => [' ', false],
'empty' => ['', false],
'not close 2' => ['$[', false],
'missing dollar' => ['.basic', false],
];
}

return [
'not close 1' => ['$(', false],
];
}
}
11 changes: 9 additions & 2 deletions tests/Redis/Command/Option/JSONFieldOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace MacFJA\RediSearch\tests\Redis\Command\Option;

use MacFJA\RediSearch\Exception\InvalidJSONPathException;
use MacFJA\RediSearch\Redis\Command\CreateCommand\JSONFieldOption;
use MacFJA\RediSearch\Redis\Command\CreateCommand\TextFieldOption;
use PHPUnit\Framework\TestCase;
Expand All @@ -36,6 +37,7 @@
* @uses \MacFJA\RediSearch\Redis\Command\Option\GroupedOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamedOption
* @uses \MacFJA\RediSearch\Redis\Command\Option\NamelessOption
* @uses \MacFJA\RediSearch\Exception\InvalidJSONPathException
*
* @internal
*/
Expand All @@ -50,9 +52,14 @@ public function testIsValid(): void
static::assertFalse($json->isValid());

$option->setField('data');
$json = new JSONFieldOption('', $option);
static::assertTrue($option->isValid());
static::assertFalse($json->isValid());

try {
new JSONFieldOption('', $option);
static::fail();
} catch (InvalidJSONPathException $e) {
// Do nothing
}

$json = new JSONFieldOption('$.data', $option);
static::assertTrue($option->isValid());
Expand Down