Skip to content

Commit

Permalink
Validate document on nulls in fields before we insert, update or repl…
Browse files Browse the repository at this point in the history
…ace (#188)

* Validate document on nulls in fields before we insert, update or replace

* Updated the document check sequence

* Fixed codestyle

---------

Co-authored-by: Nick Sergeev <[email protected]>
  • Loading branch information
donhardman and Nick-S-2018 authored Jan 17, 2024
1 parent da726dc commit 0b08765
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Manticoresearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function addDocument($data, $id = 0)
} elseif (is_string($data)) {
$data = json_decode($data, true);
}
static::checkDocument($data);
$params = [
'body' => [
'index' => $this->index,
Expand All @@ -106,6 +107,7 @@ public function addDocuments($documents)
if (isset($document['id'])) {
$id = $document['id'];
static::checkDocumentId($id);
static::checkDocument($document);
unset($document['id']);
} else {
$id = 0;
Expand Down Expand Up @@ -177,6 +179,7 @@ public function deleteDocuments($query)
public function updateDocument($data, $id)
{
static::checkDocumentId($id);
static::checkDocument($data);
$params = [
'body' => [
'index' => $this->index,
Expand Down Expand Up @@ -211,6 +214,7 @@ public function updateDocuments($data, $query)
public function replaceDocument($data, $id)
{
static::checkDocumentId($id);
static::checkDocument($data);
if (is_object($data)) {
$data = (array) $data;
} elseif (is_string($data)) {
Expand Down Expand Up @@ -240,6 +244,7 @@ public function replaceDocuments($documents)
}
$id = $document['id'];
static::checkDocumentId($id);
static::checkDocument($document);
unset($document['id']);
$replace = [
'index' => $this->index,
Expand Down Expand Up @@ -451,7 +456,28 @@ protected static function checkDocumentId(&$id)
}
$id = (int)$id;
}



/**
* Validate the document and ensure that there is null passed
* or display better error to identify the issue when manticore failed
* to insert value that contains null
* @param array $data
* @param ?int $index
* @return void
*/
protected static function checkDocument(array $data, ?int $index = null)
{
foreach ($data as $key => $value) {
if ($value === null) {
if ($index !== null) {
$key = "[$index][$key]";
}
throw new RuntimeException("Error: The key '{$key}' in document has a null value.\n");
}
}
}

protected static function checkIfList(array &$ids)
{
if ($ids && (array_keys($ids) !== range(0, count($ids) - 1))) {
Expand Down

0 comments on commit 0b08765

Please sign in to comment.