Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Jsv4/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ private function includeSubResult($subResult, $dataPrefix, $schemaPrefix)
private function checkTypes()
{
if (isset($this->schema->type)) {
$types = $this->schema->type;
if (!is_array($types)) {
$types = [$types];
}
$types = (array) $this->schema->type;

foreach ($types as $type) {
if ($type == "object" && is_object($this->data)) {
return;
Expand Down Expand Up @@ -215,15 +213,15 @@ private function checkTypes()
return;
}
} else if ($type == "string") {
if (is_numeric($this->data)) {
$this->data = "" . $this->data;
return;
} else if (is_bool($this->data)) {
$this->data = ($this->data) ? "true" : "false";
if (is_bool($this->data)) {
$this->data = $this->data? "true" : "false";
return;
} else if (is_null($this->data)) {
$this->data = "";
return;
} else if (is_scalar($this->data)) {
$this->data = (string) $this->data;
return;
}
} else if ($type == "boolean") {
if (is_numeric($this->data)) {
Expand All @@ -239,6 +237,14 @@ private function checkTypes()
$this->data = FALSE;
return;
}
} else if ($type == "object") {
if (is_array($this->data)) {
$this->data = (object) $this->data;
return;
}
} else if ($type == "array") {
$this->data = (array) $this->data;
return;
}
}
}
Expand Down Expand Up @@ -274,7 +280,7 @@ private function checkObject()
}
if (isset($this->schema->required)) {
foreach ($this->schema->required as $index => $key) {
if (!array_key_exists($key, (array) $this->data)) {
if (!property_exists($this->data, $key)) {
if ($this->coerce && $this->createValueForProperty($key)) {
continue;
}
Expand All @@ -286,7 +292,7 @@ private function checkObject()
if (isset($this->schema->properties)) {
foreach ($this->schema->properties as $key => $subSchema) {
$checkedProperties[$key] = TRUE;
if (array_key_exists($key, (array) $this->data)) {
if (property_exists($this->data, $key)) {
$subResult = $this->subResult($this->data->$key, $subSchema);
$this->includeSubResult($subResult, self::pointerJoin(array($key)), self::pointerJoin(array("properties", $key)));
}
Expand Down Expand Up @@ -417,12 +423,12 @@ private function checkString()
return;
}
if (isset($this->schema->minLength)) {
if (strlen($this->data) < $this->schema->minLength) {
if (mb_strlen($this->data) < $this->schema->minLength) {
$this->fail(self::STRING_LENGTH_SHORT, "", "/minLength", "String must be at least {$this->schema->minLength} characters long");
}
}
if (isset($this->schema->maxLength)) {
if (strlen($this->data) > $this->schema->maxLength) {
if (mb_strlen($this->data) > $this->schema->maxLength) {
$this->fail(self::STRING_LENGTH_LONG, "", "/maxLength", "String must be at most {$this->schema->maxLength} characters long");
}
}
Expand Down