diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index bc608f6d..951f2fd2 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -10,7 +10,9 @@ namespace JsonSchema\Constraints; use JsonSchema\ConstraintError; +use JsonSchema\Constraints\TypeCheck\LooseTypeCheck; use JsonSchema\Entity\JsonPointer; +use JsonSchema\Exception\InvalidArgumentException; use JsonSchema\Exception\ValidationException; /** @@ -89,4 +91,25 @@ public function reset() { $this->errors = array(); } + + /** + * Recursively cast an associative array to an object + * + * @param array $array + * + * @return object + */ + public static function arrayToObjectRecursive($array) + { + $json = json_encode($array); + if (json_last_error() !== \JSON_ERROR_NONE) { + $message = 'Unable to encode schema array as JSON'; + if (version_compare(phpversion(), '5.5.0', '>=')) { + $message .= ': ' . json_last_error_msg(); + } + throw new InvalidArgumentException($message); + } + + return json_decode($json); + } } diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index d479103c..0a9a8418 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -40,6 +40,11 @@ public function validate(&$value, $schema = null, $checkMode = null) // reset errors prior to validation $this->reset(); + // make sure $schema is an object + if (is_array($schema)) { + $schema = self::arrayToObjectRecursive($schema); + } + // set checkMode $initialCheckMode = $this->factory->getConfig(); if ($checkMode !== null) { diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php new file mode 100644 index 00000000..73688537 --- /dev/null +++ b/tests/ValidatorTest.php @@ -0,0 +1,59 @@ +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.'); + } +}