Skip to content

Commit a4ed028

Browse files
committed
Merge pull request #8 from dantleech/pattern-properties
Added (basic) support for "patternProperties" attribute
2 parents bb6175a + 58adfda commit a4ed028

File tree

5 files changed

+107
-11
lines changed

5 files changed

+107
-11
lines changed

src/JsonSchema/Constraints/Constraint.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
100100
* @param mixed $path
101101
* @param mixed $i
102102
*/
103-
protected function checkObject($value, $schema = null, $path = null, $i = null)
103+
protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null)
104104
{
105105
$validator = new Object($this->checkMode);
106-
$validator->check($value, $schema, $path, $i);
106+
$validator->check($value, $schema, $path, $i, $patternProperties);
107107

108108
$this->addErrors($validator->getErrors());
109109
}
@@ -195,4 +195,4 @@ public function isValid()
195195
{
196196
return !$this->getErrors();
197197
}
198-
}
198+
}

src/JsonSchema/Constraints/Object.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,33 @@ class Object extends Constraint
1313
/**
1414
* {inheritDoc}
1515
*/
16-
function check($element, $definition = null, $path = null, $additionalProp = null)
16+
function check($element, $definition = null, $path = null, $additionalProp = null, $patternProperties = null)
1717
{
18-
// validate the definition properties
19-
$this->validateDefinition($element, $definition, $path);
18+
if ($patternProperties) {
19+
$this->validatePatternProperties($element, $path, $additionalProp, $patternProperties);
20+
}
21+
22+
if ($definition) {
23+
// validate the definition properties
24+
$this->validateDefinition($element, $definition, $path);
25+
}
2026

2127
// additional the element properties
2228
$this->validateElement($element, $definition, $path, $additionalProp);
2329
}
2430

31+
public function validatePatternProperties($element, $path, $additionalProp, $patternProperties)
32+
{
33+
foreach ($patternProperties as $pregex => $schema) {
34+
35+
foreach ($element as $i => $value) {
36+
if (preg_match('/'.$pregex.'/', $i)) {
37+
$this->checkUndefined($value, $schema ? : new \stdClass(), $path, $i);
38+
}
39+
}
40+
}
41+
}
42+
2543
/**
2644
* validates the element properties
2745
*
@@ -97,4 +115,4 @@ protected function getProperty($element, $property, $fallback = null)
97115

98116
return $fallback;
99117
}
100-
}
118+
}

src/JsonSchema/Constraints/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ public function check($element, $schema = null, $path = null, $i = null)
2525
throw new \InvalidArgumentException('no schema found to verify against');
2626
}
2727
}
28-
}
28+
}

src/JsonSchema/Constraints/Undefined.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ public function validateTypes($value, $schema = null, $path = null, $i = null)
4646
}
4747

4848
// check object
49-
if (is_object($value) && isset($schema->properties)) {
50-
$this->checkObject($value, $schema->properties, $path, isset($schema->additionalProperties) ? $schema->additionalProperties : null);
49+
if (is_object($value) && (isset($schema->properties) || isset($schema->patternProperties))) {
50+
$this->checkObject(
51+
$value,
52+
isset($schema->properties) ? $schema->properties : null,
53+
$path,
54+
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
55+
isset($schema->patternProperties) ? $schema->patternProperties : null
56+
);
5157
}
5258

5359
// check string
@@ -104,4 +110,4 @@ protected function validateCommonProperties($value, $schema = null, $path = null
104110
}
105111
}
106112
}
107-
}
113+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace JsonSchema\Tests;
4+
5+
class PatternPropertiesTest extends BaseTestCase
6+
{
7+
public function getInvalidTests()
8+
{
9+
return array(
10+
// matches pattern but invalid schema for object
11+
array(
12+
json_encode(array(
13+
'someobject' => array(
14+
'foobar' => 'foo',
15+
'barfoo' => 'bar',
16+
)
17+
)),
18+
json_encode(array(
19+
'type' => 'object',
20+
'patternProperties' => array(
21+
'^someobject$' => array(
22+
'type' => 'object',
23+
'additionalProperties' => false,
24+
'properties' => array(
25+
'barfoo' => array(
26+
'type' => 'string',
27+
),
28+
)
29+
)
30+
)
31+
))
32+
),
33+
);
34+
}
35+
36+
public function getValidTests()
37+
{
38+
return array(
39+
array(
40+
// validates pattern schema
41+
json_encode(array(
42+
'someobject' => array(
43+
'foobar' => 'foo',
44+
'barfoo' => 'bar',
45+
),
46+
'someotherobject' => array(
47+
'foobar' => 1234,
48+
)
49+
)),
50+
json_encode(array(
51+
'type' => 'object',
52+
'patternProperties' => array(
53+
'^someobject$' => array(
54+
'type' => 'object',
55+
'properties' => array(
56+
'foobar' => array('type' => 'string'),
57+
'barfoo' => array('type' => 'string'),
58+
),
59+
),
60+
'^someotherobject$' => array(
61+
'type' => 'object',
62+
'properties' => array(
63+
'foobar' => array('type' => 'number'),
64+
),
65+
),
66+
)
67+
))
68+
),
69+
);
70+
}
71+
}
72+

0 commit comments

Comments
 (0)