Skip to content

Commit 6fe8cd6

Browse files
committed
chore: fix test and propety validation
1 parent 6d229c5 commit 6fe8cd6

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

src/Agents/Schema.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function toJson(): array
9191
{
9292
$json = [];
9393
foreach ($this->object->getProperties() as $property => $value) {
94-
$json[$property] = $value['description'].' ('.$value['type'].')';
94+
$description = $value['description'] ?? '';
95+
$type = $value['type'] ?? '';
96+
$json[$property] = $description.' ('.$type.')';
9597
}
9698

9799
return $json;

src/Agents/Schema/SchemaObject.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
*/

tests/Agents/SchemaTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ class SchemaTest extends TestCase
2424
protected function setUp(): void
2525
{
2626
$this->object = new SchemaObject([
27-
'id' => ['type' => SchemaObject::TYPE_STRING],
28-
'name' => ['type' => SchemaObject::TYPE_STRING],
29-
'age' => ['type' => SchemaObject::TYPE_INTEGER],
27+
'id' => [
28+
'type' => SchemaObject::TYPE_STRING,
29+
'description' => 'The ID of the user',
30+
],
31+
'name' => [
32+
'type' => SchemaObject::TYPE_STRING,
33+
'description' => 'The name of the user',
34+
],
35+
'age' => [
36+
'type' => SchemaObject::TYPE_INTEGER,
37+
'description' => 'The age of the user',
38+
],
3039
]);
3140
$this->schema = new Schema(
3241
$this->name,

0 commit comments

Comments
 (0)