Validate format on non-strings

This commit is contained in:
Jake Barnby
2025-12-03 22:38:37 +13:00
parent dce4c6e9f1
commit 252a4e12ff
3 changed files with 23 additions and 2 deletions
@@ -19,7 +19,6 @@ use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Index as IndexException;
use Utopia\Database\Exception\Limit as LimitException;
use Utopia\Database\Exception\NotFound as NotFoundException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Validator\Authorization;
@@ -6,7 +6,6 @@ use Utopia\Database\Database;
use Utopia\Database\Validator\Datetime as DatetimeValidator;
use Utopia\Database\Validator\Key;
use Utopia\Validator;
use Utopia\Validator\Boolean as BooleanValidator;
use Utopia\Validator\Range;
use Utopia\Validator\Text;
@@ -135,6 +134,11 @@ class Attributes extends Validator
// Validate format if provided
if (isset($attribute['format']) && $attribute['format'] !== '') {
// Format is only allowed for string type
if ($attribute['type'] !== Database::VAR_STRING) {
$this->message = "Format is only allowed for string type for attribute '" . $attribute['key'] . "'";
return false;
}
if (!in_array($attribute['format'], $this->supportedFormats)) {
$this->message = "Invalid format for attribute '" . $attribute['key'] . "': " . $attribute['format'];
return false;
@@ -7454,6 +7454,24 @@ class DatabasesCustomServerTest extends Scope
]);
$this->assertEquals(400, $collection['headers']['status-code']);
// Test: Format on non-string type (format is only allowed for strings)
$collection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'Format On Integer',
'attributes' => [
[
'key' => 'count',
'type' => Database::VAR_INTEGER,
'format' => 'enum',
],
],
]);
$this->assertEquals(400, $collection['headers']['status-code']);
// Test: Valid integer with min/max range and default within range (should succeed)
$collection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',