Skip to content
Open
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
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
}
],
"require": {
"ext-json": "*",
"galbar/jsonpath": "^3.0",
"opis/json-schema": "^1.0.8"
"opis/json-schema": "^2.1",
"galbar/jsonpath": "^1.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
Expand All @@ -26,7 +26,8 @@
},
"autoload": {
"psr-4": {
"RootedData\\": "src/"
"RootedData\\": "src/",
"RootedDataTest\\": "tests/"
}
},
"autoload-dev": {
Expand Down
6 changes: 4 additions & 2 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class ValidationException extends \InvalidArgumentException
{
/**
* Validation result report.
*
* @var \Opis\JsonSchema\ValidationResult
*/
private ValidationResult $validationResult;

/**
* @param string $message
* Exception message.
* @param ValidationResult $validationResult
* @param \Opis\JsonSchema\ValidationResult $validationResult
* Validation result report.
*/
public function __construct(string $message, ValidationResult $validationResult)
Expand All @@ -31,7 +33,7 @@ public function __construct(string $message, ValidationResult $validationResult)
/**
* Get the validation result object.
*
* @return ValidationResult
* @return \Opis\JsonSchema\ValidationResult
* Validation result report.
*/
public function getResult(): ValidationResult
Expand Down
37 changes: 19 additions & 18 deletions src/RootedJsonData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace RootedData;

use InvalidArgumentException;
use JsonPath\InvalidJsonException;
use Opis\JsonSchema\Schema;
use Opis\JsonSchema\Validator;
use JsonPath\JsonObject;
use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\ValidationResult;
use Opis\JsonSchema\Validator;
use RootedData\Exception\ValidationException;

/**
Expand All @@ -16,7 +14,6 @@
*/
class RootedJsonData
{

private ?string $schema = null;
private JsonObject $data;

Expand All @@ -27,13 +24,11 @@ class RootedJsonData
* String of JSON data.
* @param string $schema
* JSON schema document for validation.
* @throws InvalidJsonException
* @throws \JsonPath\InvalidJsonException
*/
public function __construct(string $json = "{}", string $schema = "{}")
{
if (Schema::fromJsonString($schema)) {
$this->schema = $schema;
}
$this->schema = $schema;

$result = self::validate($json, $this->schema);
if (!$result->isValid()) {
Expand All @@ -59,12 +54,16 @@ public static function validate(string $json, string $schema): ValidationResult
$decoded = json_decode($json);

if (!isset($decoded)) {
throw new InvalidArgumentException("Invalid JSON: " . json_last_error_msg());
throw new \InvalidArgumentException("Invalid JSON: " . json_last_error_msg());
}

$opiSchema = Schema::fromJsonString($schema);
$validator = new Validator();
return $validator->schemaValidation($decoded, $opiSchema);

$opiSchema = $validator
->loader()
->loadObjectSchema(json_decode($schema));

return $validator->validate($decoded, $opiSchema);
}

/**
Expand All @@ -76,7 +75,7 @@ public function __toString()
{
return $this->data->getJson();
}

/**
* Return pretty-formatted JSON string
*
Expand Down Expand Up @@ -122,7 +121,7 @@ public function __get(string $path)
* @param mixed $value
*
* @return JsonObject
* @throws InvalidJsonException
* @throws \JsonPath\InvalidJsonException
*/
public function set(string $path, $value)
{
Expand All @@ -132,8 +131,9 @@ public function set(string $path, $value)

$result = self::validate($validationJsonObject, $this->schema);
if (!$result->isValid()) {
$keywordArgs = $result->getFirstError()->keywordArgs();
$message = "{$path} expects a {$keywordArgs['expected']}";
$formatter = new ErrorFormatter();
$error = $formatter->format($result->error(), false);
$message = reset($error);
throw new ValidationException($message, $result);
}

Expand Down Expand Up @@ -245,9 +245,9 @@ public function getSchema(): ?string
* Key if adding key/value pair
*
* @return JsonObject
* @throws InvalidJsonException
* @throws \JsonPath\InvalidJsonException
*
* @see JsonPath\JsonObject::add()
* @see \JsonPath\JsonObject::add()
*/
public function add($path, $value, $field = null)
{
Expand All @@ -263,4 +263,5 @@ public function add($path, $value, $field = null)

return $this->data->add($path, $value, $field);
}

}
15 changes: 7 additions & 8 deletions tests/RootedJsonDatatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use PHPUnit\Framework\TestCase;
use RootedData\RootedJsonData;
use Opis\JsonSchema\Exception\InvalidSchemaException;
use Opis\JsonSchema\Exception\SchemaKeywordException;
use Opis\JsonSchema\Exceptions\SchemaException;
use RootedData\Exception\ValidationException;

class RootedJsonDataTest extends TestCase
Expand Down Expand Up @@ -61,7 +60,7 @@ public function testJsonIntegrityFailure()
new RootedJsonData($json, $schema);
} catch (ValidationException $e) {
$this->assertInstanceOf(ValidationException::class, $e);
$this->assertEquals("type", $e->getResult()->getFirstError()->keyword());
$this->assertEquals('properties', $e->getResult()->error()->keyword());
}
}

Expand All @@ -78,7 +77,7 @@ public function testSchemaIntegrity()
// Schema is not even valid JSON
public function testSchemaJsonIntegrity()
{
$this->expectException(InvalidSchemaException::class);
$this->expectException(\TypeError::class);
$json = '{"number":"hello"}';
// Missing a closing bracket
$schema = '{"type":"object","properties":{"number":{"type":"number"}}';
Expand All @@ -95,7 +94,7 @@ public function testJsonIntegrity()

public function testJsonIntegrityFailureAfterChange()
{
$this->expectExceptionMessage("\$.number expects a number");
$this->expectExceptionMessage('The data (string) must match the type: number');

$json = '{"number":51}';
$schema = '{"type":"object","properties": {"number":{ "type":"number"}}}';
Expand All @@ -109,7 +108,7 @@ public function testJsonIntegrityFailureAfterChange()
*/
public function testJsonIntegrityFailureMagicSetter()
{
$this->expectExceptionMessage("\$[number] expects a number");
$this->expectExceptionMessage('The data (string) must match the type: number');

$json = '{"number":51}';
$schema = '{"type":"object","properties": {"number":{ "type":"number"}}}';
Expand Down Expand Up @@ -151,14 +150,14 @@ public function testAddJsonData()
$data = new RootedJsonData($json, $schema);
$data->set("$.container", new RootedJsonData($subJson));
$this->assertEquals(51, $data->get("$.container.number"));

// If we add stdClass object, it should be work and be an array.
$data2 = new RootedJsonData($json, $schema);
$data2->set("$.container", json_decode($subJson));
$this->assertEquals(51, $data2->get("$.container.number"));
$this->assertIsArray($data2->get("$.container"));
}

/**
* getSchema() should return the same string that was provided to constructor.
*/
Expand Down