-
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
Add option to apply default values from the schema #349
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Tests\Constraints; | ||
|
||
use JsonSchema\Constraints\Constraint; | ||
use JsonSchema\Constraints\Factory; | ||
use JsonSchema\SchemaStorage; | ||
use JsonSchema\Validator; | ||
|
||
class DefaultPropertiesTest extends VeryBaseTestCase | ||
{ | ||
public function getValidTests() | ||
{ | ||
return array( | ||
array(// default value for entire object | ||
'', | ||
'{"default":"valueOne"}', | ||
'"valueOne"' | ||
), | ||
array(// default value in an empty object | ||
'{}', | ||
'{"properties":{"propertyOne":{"default":"valueOne"}}}', | ||
'{"propertyOne":"valueOne"}' | ||
), | ||
array(// default value for top-level property | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":"valueTwo"}}}', | ||
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' | ||
), | ||
array(// default value for sub-property | ||
'{"propertyOne":{}}', | ||
'{"properties":{"propertyOne":{"properties":{"propertyTwo":{"default":"valueTwo"}}}}}', | ||
'{"propertyOne":{"propertyTwo":"valueTwo"}}' | ||
), | ||
array(// default value for sub-property with sibling | ||
'{"propertyOne":{"propertyTwo":"valueTwo"}}', | ||
'{"properties":{"propertyOne":{"properties":{"propertyThree":{"default":"valueThree"}}}}}', | ||
'{"propertyOne":{"propertyTwo":"valueTwo","propertyThree":"valueThree"}}' | ||
), | ||
array(// default value for top-level property with type check | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":"valueTwo","type":"string"}}}', | ||
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' | ||
), | ||
array(// default value for top-level property with v3 required check | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":"valueTwo","required":"true"}}}', | ||
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' | ||
), | ||
array(// default value for top-level property with v4 required check | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":"valueTwo"}},"required":["propertyTwo"]}', | ||
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' | ||
), | ||
array(//default value for an already set property | ||
'{"propertyOne":"alreadySetValueOne"}', | ||
'{"properties":{"propertyOne":{"default":"valueOne"}}}', | ||
'{"propertyOne":"alreadySetValueOne"}' | ||
), | ||
array(//default item value for an array | ||
'["valueOne"]', | ||
'{"type":"array","items":[{},{"type":"string","default":"valueTwo"}]}', | ||
'["valueOne","valueTwo"]' | ||
), | ||
array(//default item value for an empty array | ||
'[]', | ||
'{"type":"array","items":[{"type":"string","default":"valueOne"}]}', | ||
'["valueOne"]' | ||
), | ||
array(//property without a default available | ||
'{"propertyOne":"alreadySetValueOne"}', | ||
'{"properties":{"propertyOne":{"type":"string"}}}', | ||
'{"propertyOne":"alreadySetValueOne"}' | ||
), | ||
array(// default property value is an object | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":{}}}}', | ||
'{"propertyOne":"valueOne","propertyTwo":{}}' | ||
), | ||
array(// default item value is an object | ||
'[]', | ||
'{"type":"array","items":[{"default":{}}]}', | ||
'[{}]' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidTests | ||
*/ | ||
public function testValidCases($input, $schema, $expectOutput = null, $validator = null) | ||
{ | ||
if (is_string($input)) { | ||
$inputDecoded = json_decode($input); | ||
} else { | ||
$inputDecoded = $input; | ||
} | ||
|
||
if ($validator === null) { | ||
$factory = new Factory(null, null, Constraint::CHECK_MODE_APPLY_DEFAULTS); | ||
$validator = new Validator($factory); | ||
} | ||
$validator->validate($inputDecoded, json_decode($schema)); | ||
|
||
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true)); | ||
|
||
if ($expectOutput !== null) { | ||
$this->assertEquals($expectOutput, json_encode($inputDecoded)); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider getValidTests | ||
*/ | ||
public function testValidCasesUsingAssoc($input, $schema, $expectOutput = null) | ||
{ | ||
$input = json_decode($input, true); | ||
|
||
$factory = new Factory(null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_APPLY_DEFAULTS); | ||
self::testValidCases($input, $schema, $expectOutput, new Validator($factory)); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidTests | ||
*/ | ||
public function testValidCasesUsingAssocWithoutTypeCast($input, $schema, $expectOutput = null) | ||
{ | ||
$input = json_decode($input, true); | ||
$factory = new Factory(null, null, Constraint::CHECK_MODE_APPLY_DEFAULTS); | ||
self::testValidCases($input, $schema, $expectOutput, new Validator($factory)); | ||
} | ||
|
||
public function testNoModificationViaReferences() | ||
{ | ||
$input = json_decode(''); | ||
$schema = json_decode('{"default":{"propertyOne":"valueOne"}}'); | ||
|
||
$validator = new Validator(); | ||
$validator->validate($input, $schema, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_APPLY_DEFAULTS); | ||
|
||
$this->assertEquals('{"propertyOne":"valueOne"}', json_encode($input)); | ||
|
||
$input->propertyOne = 'valueTwo'; | ||
$this->assertEquals('valueOne', $schema->default->propertyOne); | ||
} | ||
} |
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
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.
I think this could be refactored with https://github.com/justinrainbow/json-schema/pull/349/files#diff-9aeb900077027f2ca2e0a03af4ea6a2fR117 by using the
LooseTypeCheck
instead of$this->getTypeCheck()