Skip to content

Added (basic) support for "patternProperties" attribute #8

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

Merged
merged 1 commit into from
May 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -195,4 +195,4 @@ public function isValid()
{
return !$this->getErrors();
}
}
}
26 changes: 22 additions & 4 deletions src/JsonSchema/Constraints/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -97,4 +115,4 @@ protected function getProperty($element, $property, $fallback = null)

return $fallback;
}
}
}
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public function check($element, $schema = null, $path = null, $i = null)
throw new \InvalidArgumentException('no schema found to verify against');
}
}
}
}
12 changes: 9 additions & 3 deletions src/JsonSchema/Constraints/Undefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,4 +110,4 @@ protected function validateCommonProperties($value, $schema = null, $path = null
}
}
}
}
}
72 changes: 72 additions & 0 deletions tests/JsonSchema/Tests/PatternPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace JsonSchema\Tests;

class PatternPropertiesTest extends BaseTestCase
{
public function getInvalidTests()
{
return array(
// matches pattern but invalid schema for object
array(
json_encode(array(
'someobject' => 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'),
),
),
)
))
),
);
}
}