@@ -28,6 +28,10 @@ class Schema extends MagicMap
28
28
public $ required ;
29
29
/** @var string[][]|Schema[] */
30
30
public $ dependencies ;
31
+ /** @var int */
32
+ public $ minProperties ;
33
+ /** @var int */
34
+ public $ maxProperties ;
31
35
32
36
// Array
33
37
/** @var Schema|Schema[] */
@@ -36,6 +40,10 @@ class Schema extends MagicMap
36
40
public $ additionalItems ;
37
41
/** @var bool */
38
42
public $ uniqueItems ;
43
+ /** @var int */
44
+ public $ minItems ;
45
+ /** @var int */
46
+ public $ maxItems ;
39
47
40
48
// Reference
41
49
/** @var Ref */
@@ -46,14 +54,25 @@ class Schema extends MagicMap
46
54
public $ enum ;
47
55
48
56
// Number
57
+ /** @var int */
49
58
public $ maximum ;
59
+ /** @var bool */
50
60
public $ exclusiveMaximum ;
61
+ /** @var int */
51
62
public $ minimum ;
63
+ /** @var bool */
52
64
public $ exclusiveMinimum ;
65
+ /** @var float|int */
66
+ public $ multipleOf ;
53
67
54
68
55
69
// String
70
+ /** @var string */
56
71
public $ pattern ;
72
+ /** @var int */
73
+ public $ minLength ;
74
+ /** @var int */
75
+ public $ maxLength ;
57
76
58
77
public function import ($ data )
59
78
{
@@ -82,14 +101,31 @@ public function import($data)
82
101
}
83
102
84
103
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 ) {
86
115
if (0 === preg_match ($ this ->pattern , $ data )) {
87
116
$ this ->fail ('Does not match to ' . $ this ->pattern );
88
117
}
89
118
}
90
119
}
91
120
92
121
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
+
93
129
if ($ this ->maximum !== null ) {
94
130
if ($ this ->exclusiveMaximum === true ) {
95
131
if ($ data >= $ this ->maximum ) {
@@ -135,7 +171,14 @@ public function import($data)
135
171
$ properties = &$ this ->properties ->toArray ();
136
172
}
137
173
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 ) {
139
182
$ found = false ;
140
183
if (isset ($ this ->dependencies [$ key ])) {
141
184
$ dependencies = $ this ->dependencies [$ key ];
@@ -155,7 +198,7 @@ public function import($data)
155
198
$ value = $ properties [$ key ]->import ($ value );
156
199
}
157
200
158
- if (! $ found && $ this ->patternProperties !== null ) {
201
+ if ($ this ->patternProperties !== null ) {
159
202
foreach ($ this ->patternProperties as $ pattern => $ propertySchema ) {
160
203
if (preg_match ($ pattern , $ key )) {
161
204
$ found = true ;
@@ -178,6 +221,14 @@ public function import($data)
178
221
179
222
if (is_array ($ data )) {
180
223
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
+
181
232
if ($ this ->items instanceof Schema) {
182
233
$ items = array ();
183
234
$ additionalItems = $ this ->items ;
0 commit comments