1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00

Merge pull request #2503 from appwrite/feat-strict-enums

fix: strict enum attribute
This commit is contained in:
Torsten Dittmann 2021-12-17 13:51:42 +01:00 committed by GitHub
commit 7f82661801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View file

@ -839,6 +839,10 @@ App::post('/v1/database/collections/:collectionId/attributes/enum')
$size = ($length > $size) ? $length : $size;
}
if (!is_null($default) && !in_array($default, $elements)) {
throw new Exception('Default value not found in elements', 400);
}
$attribute = createAttribute($collectionId, new Document([
'key' => $key,
'type' => Database::VAR_STRING,

View file

@ -311,7 +311,7 @@ Structure::addFormat(APP_DATABASE_ATTRIBUTE_EMAIL, function() {
Structure::addFormat(APP_DATABASE_ATTRIBUTE_ENUM, function($attribute) {
$elements = $attribute['formatOptions']['elements'];
return new WhiteList($elements);
return new WhiteList($elements, true);
}, Database::VAR_STRING);
Structure::addFormat(APP_DATABASE_ATTRIBUTE_IP, function() {

View file

@ -1419,6 +1419,36 @@ trait DatabaseBase
'array' => true,
]);
$defaultRequired = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'defaultRequired',
'required' => true,
'default' => 12
]);
$enumDefault = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/enum', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'enumDefault',
'elements' => ['north', 'west'],
'default' => 'south'
]);
$enumDefaultStrict = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/enum', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'enumDefault',
'elements' => ['north', 'west'],
'default' => 'NORTH'
]);
$this->assertEquals(201, $email['headers']['status-code']);
$this->assertEquals(201, $ip['headers']['status-code']);
$this->assertEquals(201, $url['headers']['status-code']);
@ -1427,8 +1457,12 @@ trait DatabaseBase
$this->assertEquals(201, $probability['headers']['status-code']);
$this->assertEquals(201, $upperBound['headers']['status-code']);
$this->assertEquals(201, $lowerBound['headers']['status-code']);
$this->assertEquals(201, $enum['headers']['status-code']);
$this->assertEquals(400, $invalidRange['headers']['status-code']);
$this->assertEquals(400, $defaultArray['headers']['status-code']);
$this->assertEquals(400, $defaultRequired['headers']['status-code']);
$this->assertEquals(400, $enumDefault['headers']['status-code']);
$this->assertEquals(400, $enumDefaultStrict['headers']['status-code']);
$this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
$this->assertEquals('Cannot set default value for array attributes', $defaultArray['body']['message']);