diff --git a/src/Utilities/_DataValidation.php b/src/Utilities/_DataValidation.php index 75d3a2e..5c5cebe 100644 --- a/src/Utilities/_DataValidation.php +++ b/src/Utilities/_DataValidation.php @@ -16,9 +16,11 @@ function validateTtl($ttlSeconds): void } if (!function_exists('isNullOrEmpty')) { - function isNullOrEmpty(?string $str = null): bool + function isNullOrEmpty($value): bool { - return (is_null($str) || $str === ""); + // if the value is not null, check if the string value is empty. This covers strings that have been + // automatically converted into integers. Scalars cover int, float, string, and bool. + return is_null($value) || (is_scalar($value) && strval($value) === ""); } } @@ -68,7 +70,7 @@ function validateKeys(array $keys): void foreach ($keys as $key) { // Explicitly test type of key. If someone passes us a ["a", "b", "c"] style list upstream // instead of a ["key"=>"val"] dict, the "keys" will be integers and we want to reject the payload. - if (!is_string($key) || isNullOrEmpty($key)) { + if (isNullOrEmpty($key)) { throw new InvalidArgumentError("Keys must all be non-empty strings"); } } @@ -196,12 +198,8 @@ function validateSortedSetRanks(?int $startRank, ?int $endRank): void function validateSortedSetElements(array $elements): void { foreach ($elements as $value => $score) { - if (!is_string($value)) { - throw new InvalidArgumentError("Sorted set value must be a string"); - } - if (!is_float($score)) { - throw new InvalidArgumentException("Sorted set score must be a float"); + throw new InvalidArgumentError("Sorted set score must be a float"); } validateValueName($value); @@ -216,7 +214,7 @@ function validateSortedSetValues(array $values): void throw new InvalidArgumentError("sorted set values must be a non-empty array"); } foreach ($values as $value) { - if (!is_string($value) || isNullOrEmpty($value)) { + if (isNullOrEmpty($value)) { throw new InvalidArgumentError("sorted set values must all be non-empty strings"); } } diff --git a/tests/Cache/CacheClientTest.php b/tests/Cache/CacheClientTest.php index 5d25ab9..2fb4cea 100644 --- a/tests/Cache/CacheClientTest.php +++ b/tests/Cache/CacheClientTest.php @@ -2119,10 +2119,6 @@ public function testDictionarySetFieldsWithNullItems_ThrowsException() public function testDictionarySetFieldsWithEmptyItems_IsError() { $dictionaryName = uniqid(); - $items = [""]; - $response = $this->client->dictionarySetFields($this->TEST_CACHE_NAME, $dictionaryName, $items, CollectionTtl::fromCacheTtl()->withNoRefreshTtlOnUpdates()); - $this->assertNotNull($response->asError(), "Expected error but got: $response"); - $this->assertEquals(MomentoErrorCode::INVALID_ARGUMENT_ERROR, $response->asError()->errorCode()); $items = []; $response = $this->client->dictionarySetFields($this->TEST_CACHE_NAME, $dictionaryName, $items, CollectionTtl::fromCacheTtl()->withNoRefreshTtlOnUpdates()); @@ -2240,7 +2236,7 @@ public function testDictionaryGetFieldsWithEmptyFields_IsError() public function testDictionaryGetFields_HappyPath() { $dictionaryName = uniqid(); - $field1 = uniqid(); + $field1 = "1234"; // explicit integer-like string to make sure php casting doesn't break validation $field2 = uniqid(); $field3 = uniqid(); $value1 = uniqid(); @@ -2267,7 +2263,7 @@ public function testDictionaryGetFields_HappyPath() public function testDictionaryGetBatchFieldsValuesArray_HappyPath() { $dictionaryName = uniqid(); - $field1 = uniqid(); + $field1 = "1234"; // explicit integer-like string to make sure php casting doesn't break validation $field2 = uniqid(); $field3 = uniqid(); $value1 = uniqid();