Skip to content
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

[feat] add Json::assertMatchesSchema() #102

Merged
merged 1 commit into from
Aug 26, 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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"dbrekelmans/bdi": "^1.0",
"justinrainbow/json-schema": "^5.2",
nikophil marked this conversation as resolved.
Show resolved Hide resolved
"mtdowling/jmespath.php": "^2.6",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5",
Expand All @@ -33,6 +34,10 @@
"symfony/security-bundle": "^5.4|^6.0",
"zenstruck/foundry": "^1.17"
},
"suggest": {
"justinrainbow/json-schema": "Json schema validator. Needed to use Json::assertMatchesSchema().",
"mtdowling/jmespath.php": "PHP implementation for JMESPath. Needed to use Json assertions."
},
"config": {
"preferred-install": "dist",
"sort-packages": true
Expand Down
23 changes: 22 additions & 1 deletion src/Browser/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zenstruck\Browser;

use JsonSchema\Validator;
use Zenstruck\Assert;
use Zenstruck\Assert\Expectation;

Expand Down Expand Up @@ -97,7 +98,7 @@ public function assertThat(string $selector, callable $assert): self
/**
* @param callable(Json):mixed $assert
*/
public function assertThatEach(string $selector, callable $assert): void
public function assertThatEach(string $selector, callable $assert): self
{
$value = $this->search($selector);

Expand All @@ -110,6 +111,26 @@ public function assertThatEach(string $selector, callable $assert): void
foreach ($value as $item) {
$assert(self::encode($item));
}

return $this;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops 😅

}

public function assertMatchesSchema(string $jsonSchema): self
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not require a file name here, because it would prevent users to provide json "hard coded"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

{
if (!\class_exists(Validator::class)) {
throw new \LogicException('"justinrainbow/json-schema" is required to check JSON schema (composer require --dev justinrainbow/json-schema".');
}

$validator = new Validator();
$decoded = \json_decode($this->source, null, 512, \JSON_THROW_ON_ERROR);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

justinrainbow/json-schema actually requests a \stdclass

$validator->validate(
$decoded,
\json_decode($jsonSchema, true, 512, \JSON_THROW_ON_ERROR)
);

Assert::that($validator->isValid())->is(true, (string) \json_encode($validator->getErrors()));

return $this;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,29 @@ public function invalidArrayChildAssertionProvider(): iterable
yield ['{"foo": "bar"}', 'foo', static function(Json $json) {}];
yield ['{"foo": []}', 'foo', static function(Json $json) {}];
}

/** @test */
public function can_match_json_schema(): void
{
(new Json('{"foo1": "bar", "foo2": [1, 2], "foo3": {"bar": "baz"}, "foo4": [{"bar": "baz"}]}'))->assertMatchesSchema(
<<<'JSON'
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"foo1": { "type": "string" },
"foo2": { "type": "array", "items": [ { "type": "integer" }, { "type": "integer" } ] },
"foo3": { "type": "object", "properties": { "bar": { "type": "string" } }, "required": [ "bar" ] },
"foo4": { "type": "array", "items": [ { "type": "object", "properties": { "bar": { "type": "string" } }, "required": [ "bar" ] } ] }
},
"required": [
"foo1",
"foo2",
"foo3",
"foo4"
]
}
JSON
);
}
}