1
0
Fork 0
mirror of synced 2024-06-30 04:00:34 +12:00

Set min length to 0 for default value when creating or updating a string attribute

This commit is contained in:
Jake Barnby 2023-05-30 14:52:59 +12:00
parent f45905e545
commit c61e54f69b
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
2 changed files with 29 additions and 9 deletions

View file

@ -1103,7 +1103,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string
->param('key', '', new Key(), 'Attribute Key.')
->param('size', null, new Range(1, APP_DATABASE_ATTRIBUTE_STRING_MAX_LENGTH, Range::TYPE_INTEGER), 'Attribute size for text attributes, in number of characters.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('default', null, new Text(0, 0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true)
->param('array', false, new Boolean(), 'Is attribute an array?', true)
->inject('response')
->inject('dbForProject')
@ -1112,7 +1112,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string
->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $database, Event $events) {
// Ensure attribute default is within required size
$validator = new Text($size);
$validator = new Text($size, 0);
if (!is_null($default) && !$validator->isValid($default)) {
throw new Exception(Exception::ATTRIBUTE_VALUE_INVALID, $validator->getDescription());
}
@ -1777,7 +1777,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/strin
->param('collectionId', '', new UID(), 'Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection).')
->param('key', '', new Key(), 'Attribute Key.')
->param('required', null, new Boolean(), 'Is attribute required?')
->param('default', null, new Nullable(new Text(0)), 'Default value for attribute when not provided. Cannot be set when attribute is required.')
->param('default', null, new Nullable(new Text(0, 0)), 'Default value for attribute when not provided. Cannot be set when attribute is required.')
->inject('response')
->inject('dbForProject')
->inject('events')

View file

@ -157,6 +157,7 @@ trait DatabasesBase
public function testCreateAttributes(array $data): array
{
$databaseId = $data['databaseId'];
$title = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -167,6 +168,17 @@ trait DatabasesBase
'required' => true,
]);
$description = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'description',
'size' => 256,
'required' => false,
'default' => '',
]);
$releaseYear = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -226,6 +238,13 @@ trait DatabasesBase
$this->assertEquals($title['body']['size'], 256);
$this->assertEquals($title['body']['required'], true);
$this->assertEquals(202, $description['headers']['status-code']);
$this->assertEquals($description['body']['key'], 'description');
$this->assertEquals($description['body']['type'], 'string');
$this->assertEquals($description['body']['size'], 256);
$this->assertEquals($description['body']['required'], false);
$this->assertEquals($description['body']['default'], '');
$this->assertEquals(202, $releaseYear['headers']['status-code']);
$this->assertEquals($releaseYear['body']['key'], 'releaseYear');
$this->assertEquals($releaseYear['body']['type'], 'integer');
@ -266,13 +285,14 @@ trait DatabasesBase
]), []);
$this->assertIsArray($movies['body']['attributes']);
$this->assertCount(6, $movies['body']['attributes']);
$this->assertCount(7, $movies['body']['attributes']);
$this->assertEquals($movies['body']['attributes'][0]['key'], $title['body']['key']);
$this->assertEquals($movies['body']['attributes'][1]['key'], $releaseYear['body']['key']);
$this->assertEquals($movies['body']['attributes'][2]['key'], $duration['body']['key']);
$this->assertEquals($movies['body']['attributes'][3]['key'], $actors['body']['key']);
$this->assertEquals($movies['body']['attributes'][4]['key'], $datetime['body']['key']);
$this->assertEquals($movies['body']['attributes'][5]['key'], $relationship['body']['key']);
$this->assertEquals($movies['body']['attributes'][1]['key'], $description['body']['key']);
$this->assertEquals($movies['body']['attributes'][2]['key'], $releaseYear['body']['key']);
$this->assertEquals($movies['body']['attributes'][3]['key'], $duration['body']['key']);
$this->assertEquals($movies['body']['attributes'][4]['key'], $actors['body']['key']);
$this->assertEquals($movies['body']['attributes'][5]['key'], $datetime['body']['key']);
$this->assertEquals($movies['body']['attributes'][6]['key'], $relationship['body']['key']);
return $data;
}