-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to apply default values from the schema
- Loading branch information
Showing
9 changed files
with
236 additions
and
1 deletion.
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
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,129 @@ | ||
<?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\SchemaStorage; | ||
use JsonSchema\Validator; | ||
use JsonSchema\Constraints\Constraint; | ||
use JsonSchema\Constraints\Factory; | ||
|
||
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 value is required | ||
'{"propertyOne":"valueOne"}', | ||
'{"properties":{"propertyTwo":{"default":"valueTwo","required":true}}}', | ||
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' | ||
), | ||
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"}' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getValidTests | ||
*/ | ||
public function testValidCases($input, $schema, $expectOutput = null) | ||
{ | ||
if (is_string($input)) { | ||
$assoc = false; | ||
$inputDecoded = json_decode($input); | ||
} else { | ||
$assoc = true; | ||
$inputDecoded = $input; | ||
} | ||
|
||
$validator = new Validator(); | ||
$validator->coerceDefault($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); | ||
self::testValidCases($input, $schema, $expectOutput); | ||
} | ||
|
||
public function testDontApplyDefaults() | ||
{ | ||
$f = new Factory(); | ||
$f->setApplyDefaults(false); | ||
|
||
$this->assertEquals(0, $f->getCheckMode() & Constraint::CHECK_MODE_APPLY_DEFAULTS); | ||
} | ||
|
||
} |