diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index a6f57aef..7975d663 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -100,10 +100,10 @@ protected function checkArray($value, $schema = null, $path = null, $i = null) * @param mixed $path * @param mixed $i */ - protected function checkObject($value, $schema = null, $path = null, $i = null) + protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null) { $validator = new Object($this->checkMode); - $validator->check($value, $schema, $path, $i); + $validator->check($value, $schema, $path, $i, $patternProperties); $this->addErrors($validator->getErrors()); } @@ -195,4 +195,4 @@ public function isValid() { return !$this->getErrors(); } -} \ No newline at end of file +} diff --git a/src/JsonSchema/Constraints/Object.php b/src/JsonSchema/Constraints/Object.php index 9e7ff043..2f77bcd3 100644 --- a/src/JsonSchema/Constraints/Object.php +++ b/src/JsonSchema/Constraints/Object.php @@ -13,15 +13,33 @@ class Object extends Constraint /** * {inheritDoc} */ - function check($element, $definition = null, $path = null, $additionalProp = null) + function check($element, $definition = null, $path = null, $additionalProp = null, $patternProperties = null) { - // validate the definition properties - $this->validateDefinition($element, $definition, $path); + if ($patternProperties) { + $this->validatePatternProperties($element, $path, $additionalProp, $patternProperties); + } + + if ($definition) { + // validate the definition properties + $this->validateDefinition($element, $definition, $path); + } // additional the element properties $this->validateElement($element, $definition, $path, $additionalProp); } + public function validatePatternProperties($element, $path, $additionalProp, $patternProperties) + { + foreach ($patternProperties as $pregex => $schema) { + + foreach ($element as $i => $value) { + if (preg_match('/'.$pregex.'/', $i)) { + $this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i); + } + } + } + } + /** * validates the element properties * @@ -97,4 +115,4 @@ protected function getProperty($element, $property, $fallback = null) return $fallback; } -} \ No newline at end of file +} diff --git a/src/JsonSchema/Constraints/Schema.php b/src/JsonSchema/Constraints/Schema.php index 0f52b383..da459754 100644 --- a/src/JsonSchema/Constraints/Schema.php +++ b/src/JsonSchema/Constraints/Schema.php @@ -25,4 +25,4 @@ public function check($element, $schema = null, $path = null, $i = null) throw new \InvalidArgumentException('no schema found to verify against'); } } -} \ No newline at end of file +} diff --git a/src/JsonSchema/Constraints/Undefined.php b/src/JsonSchema/Constraints/Undefined.php index 524bf830..70f1980f 100644 --- a/src/JsonSchema/Constraints/Undefined.php +++ b/src/JsonSchema/Constraints/Undefined.php @@ -46,8 +46,14 @@ public function validateTypes($value, $schema = null, $path = null, $i = null) } // check object - if (is_object($value) && isset($schema->properties)) { - $this->checkObject($value, $schema->properties, $path, isset($schema->additionalProperties) ? $schema->additionalProperties : null); + if (is_object($value) && (isset($schema->properties) || isset($schema->patternProperties))) { + $this->checkObject( + $value, + isset($schema->properties) ? $schema->properties : null, + $path, + isset($schema->additionalProperties) ? $schema->additionalProperties : null, + isset($schema->patternProperties) ? $schema->patternProperties : null + ); } // check string @@ -104,4 +110,4 @@ protected function validateCommonProperties($value, $schema = null, $path = null } } } -} \ No newline at end of file +} diff --git a/tests/JsonSchema/Tests/PatternPropertiesTest.php b/tests/JsonSchema/Tests/PatternPropertiesTest.php new file mode 100644 index 00000000..8bb344a2 --- /dev/null +++ b/tests/JsonSchema/Tests/PatternPropertiesTest.php @@ -0,0 +1,72 @@ + array( + 'foobar' => 'foo', + 'barfoo' => 'bar', + ) + )), + json_encode(array( + 'type' => 'object', + 'patternProperties' => array( + '^someobject$' => array( + 'type' => 'object', + 'additionalProperties' => false, + 'properties' => array( + 'barfoo' => array( + 'type' => 'string', + ), + ) + ) + ) + )) + ), + ); + } + + public function getValidTests() + { + return array( + array( + // validates pattern schema + json_encode(array( + 'someobject' => array( + 'foobar' => 'foo', + 'barfoo' => 'bar', + ), + 'someotherobject' => array( + 'foobar' => 1234, + ) + )), + json_encode(array( + 'type' => 'object', + 'patternProperties' => array( + '^someobject$' => array( + 'type' => 'object', + 'properties' => array( + 'foobar' => array('type' => 'string'), + 'barfoo' => array('type' => 'string'), + ), + ), + '^someotherobject$' => array( + 'type' => 'object', + 'properties' => array( + 'foobar' => array('type' => 'number'), + ), + ), + ) + )) + ), + ); + } +} +