Skip to content

Commit 71b48af

Browse files
committed
spec compliance
Tests: 275, Assertions: 259, Errors: 16, Failures: 20.
1 parent cd3d176 commit 71b48af

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

src/Schema.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Schema extends MagicMap
2828
public $required;
2929
/** @var string[][]|Schema[] */
3030
public $dependencies;
31+
/** @var int */
32+
public $minProperties;
33+
/** @var int */
34+
public $maxProperties;
3135

3236
// Array
3337
/** @var Schema|Schema[] */
@@ -36,6 +40,10 @@ class Schema extends MagicMap
3640
public $additionalItems;
3741
/** @var bool */
3842
public $uniqueItems;
43+
/** @var int */
44+
public $minItems;
45+
/** @var int */
46+
public $maxItems;
3947

4048
// Reference
4149
/** @var Ref */
@@ -46,14 +54,25 @@ class Schema extends MagicMap
4654
public $enum;
4755

4856
// Number
57+
/** @var int */
4958
public $maximum;
59+
/** @var bool */
5060
public $exclusiveMaximum;
61+
/** @var int */
5162
public $minimum;
63+
/** @var bool */
5264
public $exclusiveMinimum;
65+
/** @var float|int */
66+
public $multipleOf;
5367

5468

5569
// String
70+
/** @var string */
5671
public $pattern;
72+
/** @var int */
73+
public $minLength;
74+
/** @var int */
75+
public $maxLength;
5776

5877
public function import($data)
5978
{
@@ -82,14 +101,31 @@ public function import($data)
82101
}
83102

84103
if (is_string($data)) {
85-
if ($this->pattern) {
104+
if ($this->minLength !== null) {
105+
if (mb_strlen($data) < $this->minLength) {
106+
$this->fail('String is too short');
107+
}
108+
}
109+
if ($this->maxLength !== null) {
110+
if (mb_strlen($data) > $this->maxLength) {
111+
$this->fail('String is too long');
112+
}
113+
}
114+
if ($this->pattern !== null) {
86115
if (0 === preg_match($this->pattern, $data)) {
87116
$this->fail('Does not match to ' . $this->pattern);
88117
}
89118
}
90119
}
91120

92121
if (is_int($data) || is_float($data)) {
122+
if ($this->multipleOf !== null) {
123+
$div = $data / $this->multipleOf;
124+
if ($div != (int)$div) {
125+
$this->fail($data . ' is not multiple of ' . $this->multipleOf);
126+
}
127+
}
128+
93129
if ($this->maximum !== null) {
94130
if ($this->exclusiveMaximum === true) {
95131
if ($data >= $this->maximum) {
@@ -135,7 +171,14 @@ public function import($data)
135171
$properties = &$this->properties->toArray();
136172
}
137173

138-
foreach ((array)$data as $key => $value) {
174+
$array = (array)$data;
175+
if ($this->minProperties !== null && count($array) < $this->minProperties) {
176+
$this->fail("Not enough properties");
177+
}
178+
if ($this->maxProperties !== null && count($array) > $this->maxProperties) {
179+
$this->fail("Too many properties");
180+
}
181+
foreach ($array as $key => $value) {
139182
$found = false;
140183
if (isset($this->dependencies[$key])) {
141184
$dependencies = $this->dependencies[$key];
@@ -155,7 +198,7 @@ public function import($data)
155198
$value = $properties[$key]->import($value);
156199
}
157200

158-
if (!$found && $this->patternProperties !== null) {
201+
if ($this->patternProperties !== null) {
159202
foreach ($this->patternProperties as $pattern => $propertySchema) {
160203
if (preg_match($pattern, $key)) {
161204
$found = true;
@@ -178,6 +221,14 @@ public function import($data)
178221

179222
if (is_array($data)) {
180223

224+
if ($this->minItems !== null && count($data) < $this->minItems) {
225+
$this->fail("Not enough items in array");
226+
}
227+
228+
if ($this->maxItems !== null && count($data) > $this->maxItems) {
229+
$this->fail("Too many items in array");
230+
}
231+
181232
if ($this->items instanceof Schema) {
182233
$items = array();
183234
$additionalItems = $this->items;

src/SchemaLoader.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@ class SchemaLoader extends Base
1515
const ADDITIONAL_PROPERTIES = 'additionalProperties';
1616
const REQUIRED = 'required';
1717
const DEPENDENCIES = 'dependencies';
18+
const MIN_PROPERTIES = 'minProperties';
19+
const MAX_PROPERTIES = 'maxProperties';
1820

1921
const REF = '$ref';
2022

2123
const ITEMS = 'items';
2224
const ADDITIONAL_ITEMS = 'additionalItems';
2325
const UNIQUE_ITEMS = 'uniqueItems';
26+
const MIN_ITEMS = 'minItems';
27+
const MAX_ITEMS = 'maxItems';
2428

2529
const ENUM = 'enum';
2630

2731
const MINIMUM = 'minimum';
2832
const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
2933
const MAXIMUM = 'maximum';
3034
const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
35+
const MULTIPLE_OF = 'multipleOf';
3136

3237
const PATTERN = 'pattern';
38+
const MIN_LENGTH = 'minLength';
39+
const MAX_LENGTH = 'maxLength';
3340

3441
/** @var Schema */
3542
private $rootSchema;
@@ -102,6 +109,12 @@ protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
102109
}
103110
}
104111

112+
if (isset($schemaArray[self::MIN_PROPERTIES])) {
113+
$schema->minProperties = $schemaArray[self::MIN_PROPERTIES];
114+
}
115+
if (isset($schemaArray[self::MAX_PROPERTIES])) {
116+
$schema->maxProperties = $schemaArray[self::MAX_PROPERTIES];
117+
}
105118

106119

107120
// Array
@@ -131,6 +144,14 @@ protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
131144
$schema->uniqueItems = true;
132145
}
133146

147+
if (isset($schemaArray[self::MIN_ITEMS])) {
148+
$schema->minItems = $schemaArray[self::MIN_ITEMS];
149+
}
150+
151+
if (isset($schemaArray[self::MAX_ITEMS])) {
152+
$schema->maxItems = $schemaArray[self::MAX_ITEMS];
153+
}
154+
134155

135156

136157
// Number
@@ -146,13 +167,21 @@ protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
146167
if (isset($schemaArray[self::EXCLUSIVE_MAXIMUM])) {
147168
$schema->exclusiveMaximum = $schemaArray[self::EXCLUSIVE_MAXIMUM];
148169
}
170+
if (isset($schemaArray[self::MULTIPLE_OF])) {
171+
$schema->multipleOf = $schemaArray[self::MULTIPLE_OF];
172+
}
149173

150174

151175
// String
152176
if (isset($schemaArray[self::PATTERN])) {
153177
$schema->pattern = Helper::toPregPattern($schemaArray[self::PATTERN]);
154178
}
155-
179+
if (isset($schemaArray[self::MIN_LENGTH])) {
180+
$schema->minLength = $schemaArray[self::MIN_LENGTH];
181+
}
182+
if (isset($schemaArray[self::MAX_LENGTH])) {
183+
$schema->maxLength = $schemaArray[self::MAX_LENGTH];
184+
}
156185

157186

158187
// Misc

0 commit comments

Comments
 (0)