Merge pull request #2503 from appwrite/feat-strict-enums
fix: strict enum attribute
This commit is contained in:
commit
7f82661801
|
@ -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,
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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']);
|
||||
|
||||
|
|
Loading…
Reference in a new issue