Skip to content

Commit 1920156

Browse files
committed
feat: #2 allow any payload type for validateArray()
1 parent 523ac64 commit 1920156

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/HttpApiValidationTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function validateJsonString(string $payload, array|object $schema, ?in
6161
/**
6262
* Validate a JSON object with given schema.
6363
*
64-
* @param array<mixed> $payload
64+
* @param mixed $payload
6565
* The JSON object to validate.
6666
* @param array<string, mixed>|object $schema
6767
* The JSON schema to validate against.
@@ -71,7 +71,7 @@ protected function validateJsonString(string $payload, array|object $schema, ?in
7171
* @return \Wunderwerk\HttpApiUtils\Validation\ValidationResult
7272
* The validation result.
7373
*/
74-
protected function validateArray(array $payload, array|object $schema, ?int $checkMode = NULL) {
74+
protected function validateArray(mixed $payload, array|object $schema, ?int $checkMode = NULL) {
7575
$data = $this->getValidator()->arrayToObjectRecursive($payload);
7676

7777
return $this->validateDataStructure($data, $schema, $checkMode);

tests/HttpApiValidationTraitTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ public function canValidateArray(): void {
120120
$this->assertEquals('name', $result->getErrors()[0]['property']);
121121
}
122122

123+
#[Test]
124+
public function canHandleNonArrayValidation(): void {
125+
$result = $this->validateArray("non-array", $this->schema);
126+
$this->assertFalse($result->isValid());
127+
$this->assertEquals('name', $result->getErrors()[0]['property']);
128+
129+
$result = $this->validateArray(NULL, $this->schema);
130+
$this->assertFalse($result->isValid());
131+
$this->assertEquals('name', $result->getErrors()[0]['property']);
132+
133+
$result = $this->validateArray(1, $this->schema);
134+
$this->assertFalse($result->isValid());
135+
$this->assertEquals('name', $result->getErrors()[0]['property']);
136+
137+
$result = $this->validateArray(FALSE, $this->schema);
138+
$this->assertFalse($result->isValid());
139+
$this->assertEquals('name', $result->getErrors()[0]['property']);
140+
141+
$result = $this->validateArray(new stdClass(), $this->schema);
142+
$this->assertFalse($result->isValid());
143+
$this->assertEquals('name', $result->getErrors()[0]['property']);
144+
}
145+
123146
#[Test]
124147
public function canReturnErrorResponse(): void {
125148
$validInput = (object) [

0 commit comments

Comments
 (0)