-
Notifications
You must be signed in to change notification settings - Fork 356
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
Allow the schema to be an associative array #389
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
465a947
Allow the schema to be an associative array
erayd 869d003
Use json_decode(json_encode()) for array -> object cast
erayd 627fbc9
Skip exception check on PHP versions < 5.5.0
erayd 665e974
Skip test on HHVM, as it's happy to encode resources
erayd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace JsonSchema\Tests; | ||
|
||
use JsonSchema\Constraints\Constraint; | ||
use JsonSchema\Validator; | ||
|
||
class ValidatorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testValidateWithAssocSchema() | ||
{ | ||
$schema = json_decode('{"properties":{"propertyOne":{"type":"array","items":[{"type":"string"}]}}}', true); | ||
$data = json_decode('{"propertyOne":[42]}', true); | ||
|
||
$validator = new Validator(); | ||
$validator->validate($data, $schema); | ||
|
||
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.'); | ||
} | ||
|
||
public function testBadAssocSchemaInput() | ||
{ | ||
if (version_compare(phpversion(), '5.5.0', '<')) { | ||
$this->markTestSkipped('PHP versions < 5.5.0 trigger an error on json_encode issues'); | ||
} | ||
if (defined('HHVM_VERSION')) { | ||
$this->markTestSkipped('HHVM has no problem with encoding resources'); | ||
} | ||
$schema = array('propertyOne' => fopen('php://stdout', 'w')); | ||
$data = json_decode('{"propertyOne":[42]}', true); | ||
|
||
$validator = new Validator(); | ||
|
||
$this->setExpectedException('\JsonSchema\Exception\InvalidArgumentException'); | ||
$validator->validate($data, $schema); | ||
} | ||
|
||
public function testCheck() | ||
{ | ||
$schema = json_decode('{"type":"string"}'); | ||
$data = json_decode('42'); | ||
|
||
$validator = new Validator(); | ||
$validator->check($data, $schema); | ||
|
||
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.'); | ||
} | ||
|
||
public function testCoerce() | ||
{ | ||
$schema = json_decode('{"type":"integer"}'); | ||
$data = json_decode('"42"'); | ||
|
||
$validator = new Validator(); | ||
$validator->coerce($data, $schema); | ||
|
||
$this->assertTrue($validator->isValid(), 'Validation failed, but should have succeeded.'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this any faster than just doing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't benchmarked it. Do you have an opinion on which is better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think my version is faster, because you're letting all the work happen in C, and you don't have the overhead of the recursive PHP function calls. Time it for yourself though and see what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You make a good point 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is worth considering the performance in this case. Perhaps a continuous means of measuring performance can be added in another PR to facilitate a continuous improvement and scrutiny?