diff --git a/JsonSchema.php b/JsonSchema.php index d64431b..d5e0671 100644 --- a/JsonSchema.php +++ b/JsonSchema.php @@ -317,7 +317,7 @@ class JsonSchema { return array( array( 'property'=>$path, - 'message'=>gettype($value)." value found, but a ".$type." is required " + 'message'=>gettype($value)." value found, but a ".$type." is required" ) ); } diff --git a/tests/AdditionalPropertiesTest.php b/tests/AdditionalPropertiesTest.php index ecc3cd0..1c8374a 100644 --- a/tests/AdditionalPropertiesTest.php +++ b/tests/AdditionalPropertiesTest.php @@ -18,6 +18,20 @@ class AdditionalPropertiesTest extends BaseTestCase "additionalProperties": false }' ), + array( + '{ + "prop":"1", + "additionalProp":"2" + }', + '{ + "type":"object", + "properties":{ + "prop":{"type":"string"} + }, + "additionalProperties": false + }', + JsonSchema::CHECK_MODE_TYPE_CAST + ), array( '{ "prop":"1", @@ -30,6 +44,20 @@ class AdditionalPropertiesTest extends BaseTestCase }, "additionalProperties": {"type":"string"} }' + ), + array( + '{ + "prop":"1", + "additionalProp":2 + }', + '{ + "type":"object", + "properties":{ + "prop":{"type":"string"} + }, + "additionalProperties": {"type":"string"} + }', + JsonSchema::CHECK_MODE_TYPE_CAST ) ); } @@ -49,6 +77,19 @@ class AdditionalPropertiesTest extends BaseTestCase } }' ), + array( + '{ + "prop":"1", + "additionalProp":"2" + }', + '{ + "type":"object", + "properties":{ + "prop":{"type":"string"} + } + }', + JsonSchema::CHECK_MODE_TYPE_CAST + ), array( '{ "prop":"1", diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 8b01a73..e7a771b 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -5,11 +5,17 @@ abstract class BaseTestCase extends PHPUnit_Framework_TestCase /** * @dataProvider getInvalidTests */ - public function testInvalidCases($input, $schema, $errors = array()) + public function testInvalidCases($input, $schema, $checkMode = null, $errors = array()) { + if (null === $checkMode) { + $checkMode = JsonSchema::CHECK_MODE_NORMAL; + } + + JsonSchema::$checkMode = $checkMode; + $result = JsonSchema::validate(json_decode($input), json_decode($schema)); if (array() !== $errors) { - $this->assertEquals($errors, $result->errors); + $this->assertEquals($errors, $result->errors, var_export($result, true)); } $this->assertFalse($result->valid, var_export($result, true)); } @@ -22,7 +28,9 @@ abstract class BaseTestCase extends PHPUnit_Framework_TestCase if (null === $checkMode) { $checkMode = JsonSchema::CHECK_MODE_NORMAL; } + JsonSchema::$checkMode = $checkMode; + $result = JsonSchema::validate(json_decode($input), json_decode($schema)); $this->assertTrue($result->valid, var_export($result, true)); } diff --git a/tests/PhpTypeCastModeTest.php b/tests/PhpTypeCastModeTest.php index d2ca822..8c58484 100644 --- a/tests/PhpTypeCastModeTest.php +++ b/tests/PhpTypeCastModeTest.php @@ -14,18 +14,14 @@ class PhpTypeCastModeTest extends BaseTestCase "properties":{ "a":{"type":"number"} } - }' - ), - array( - '{ - "a":"9" }', - '{ - "type":"object", - "properties":{ - "a":{"type":"number"} - } - }' + null, + array( + array( + 'property' => 'a', + 'message' => 'string value found, but a number is required' + ) + ) ), array( '{ @@ -55,7 +51,19 @@ class PhpTypeCastModeTest extends BaseTestCase } }', JsonSchema::CHECK_MODE_TYPE_CAST - ) + ), + array( + '{ + "a":"9" + }', + '{ + "type":"object", + "properties":{ + "a":{"type":"number"} + } + }', + JsonSchema::CHECK_MODE_TYPE_CAST + ), ); } }