@@ -33,6 +33,13 @@ class SchemaObject
3333 */
3434 public function __construct (array $ properties = [])
3535 {
36+ foreach ($ properties as $ name => $ property ) {
37+ if (! $ this ->validateProperty ($ name , $ property )) {
38+ throw new \InvalidArgumentException (
39+ 'Invalid type ' .var_export ($ property ['type ' ], true )." for property ' $ name'. Must be one of: " .implode (', ' , self ::getValidTypes ())
40+ );
41+ }
42+ }
3643 $ this ->properties = $ properties ;
3744 }
3845
@@ -64,7 +71,7 @@ public function getProperty(string $name): ?array
6471 */
6572 public function addProperty (string $ name , array $ property ): self
6673 {
67- if (! isset ( $ property [ ' type ' ]) || ! in_array ( $ property [ ' type ' ], self :: getValidTypes (), true )) {
74+ if (! $ this -> validateProperty ( $ name , $ property )) {
6875 throw new \InvalidArgumentException (
6976 'Invalid type ' .var_export ($ property ['type ' ], true )." for property ' $ name'. Must be one of: " .implode (', ' , self ::getValidTypes ())
7077 );
@@ -89,6 +96,23 @@ public function getNames(): array
8996 return array_keys ($ this ->properties );
9097 }
9198
99+ /**
100+ * Validate a property
101+ *
102+ * @param string $name - name of the property
103+ * @param array<string, mixed> $property - property definition (must be defined in JSON Schema format)
104+ *
105+ * @return bool
106+ */
107+ public function validateProperty (string $ name , array $ property ): bool
108+ {
109+ if (! isset ($ property ['type ' ]) || ! in_array ($ property ['type ' ], self ::getValidTypes (), true )) {
110+ return false ;
111+ }
112+
113+ return true ;
114+ }
115+
92116 /**
93117 * @return array<int, string>
94118 */
0 commit comments