From 1eae5490df73a96df0f3c7e5205da90ed7a1d4ee Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Jul 2021 14:19:37 -0400 Subject: [PATCH] Encode format as json string --- app/controllers/api/database.php | 28 +++++++++++++------- app/init.php | 20 +++++++++++--- tests/e2e/Services/Database/DatabaseBase.php | 1 + 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 071429b2dd..365c08455f 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -59,8 +59,11 @@ $attributesCallback = function ($attribute, $response, $dbForExternal, $database } } - if (!\is_null($format) && !Structure::hasFormat($format, $type)) { - throw new Exception("Format {$format} not available for {$type} attributes.", 400); + if (!\is_null($format)) { + $name = \json_decode($format, true)['name']; + if (!Structure::hasFormat($name, $type)) { + throw new Exception("Format {$name} not available for {$type} attributes.", 400); + } } if (!is_null($min) || !is_null($max)) { // Add range validator if either $min or $max is provided @@ -389,7 +392,7 @@ App::post('/v1/database/collections/:collectionId/attributes/email') 'required' => $required, 'default' => $default, 'array' => $array, - 'format' => 'email', + 'format' => \json_encode(['name'=>'email']), ]), $response, $dbForExternal, $database, $audits); }); @@ -428,7 +431,7 @@ App::post('/v1/database/collections/:collectionId/attributes/ip') 'required' => $required, 'default' => $default, 'array' => $array, - 'format' => 'ip', + 'format' => \json_encode(['name'=>'ip']), ]), $response, $dbForExternal, $database, $audits); }); @@ -468,7 +471,7 @@ App::post('/v1/database/collections/:collectionId/attributes/url') 'required' => $required, 'default' => $default, 'array' => $array, - 'format' => 'url', + 'format' => \json_encode(['name'=>'url']), ]), $response, $dbForExternal, $database, $audits); }); @@ -508,11 +511,13 @@ App::post('/v1/database/collections/:collectionId/attributes/integer') 'size' => 0, 'required' => $required, 'default' => $default, - 'min' => $min, - 'max' => $max, 'array' => $array, + 'format' => \json_encode([ + 'name'=>'int-range', + 'min' => $min, + 'max' => $max, + ]), ]), $response, $dbForExternal, $database, $audits); - }); App::post('/v1/database/collections/:collectionId/attributes/float') @@ -551,9 +556,12 @@ App::post('/v1/database/collections/:collectionId/attributes/float') 'required' => $required, 'size' => 0, 'default' => $default, - 'min' => $min, - 'max' => $max, 'array' => $array, + 'format' => \json_encode([ + 'name'=>'float-range', + 'min' => $min, + 'max' => $max, + ]), ]), $response, $dbForExternal, $database, $audits); }); diff --git a/app/init.php b/app/init.php index cbb8b792d3..8101ce810a 100644 --- a/app/init.php +++ b/app/init.php @@ -201,13 +201,25 @@ Structure::addFormat('url', function() { return new URL(); }, Database2::VAR_STRING); -Structure::addFormat('int-range', function($min, $max, $type) { +Structure::addFormat('int-range', function($attribute) { + // Format encoded as json string containing name and relevant options + // E.g. Range: $format = json_encode(['name'=>$name, 'min'=>$min, 'max'=>$max]); + $format = json_decode($attribute['format'], true); + $min = $format['min'] ?? -INF; + $max = $format['max'] ?? INF; + $type = $attribute['type']; return new Range($min, $max, $type); -}, Database2::VAR_INTEGER, ['min', 'max', 'type']); +}, Database2::VAR_INTEGER); -Structure::addFormat('float-range', function($min, $max, $type) { +Structure::addFormat('float-range', function($attribute) { + // Format encoded as json string containing name and relevant options + // E.g. Range: $format = json_encode(['name'=>$name, 'min'=>$min, 'max'=>$max]); + $format = json_decode($attribute['format'], true); + $min = $format['min'] ?? -INF; + $max = $format['max'] ?? INF; + $type = $attribute['type'] ?? ''; return new Range($min, $max, $type); -}, Database2::VAR_FLOAT, ['min', 'max', 'type']); +}, Database2::VAR_FLOAT); /* * Registry diff --git a/tests/e2e/Services/Database/DatabaseBase.php b/tests/e2e/Services/Database/DatabaseBase.php index 6fce95bf86..090d1978fc 100644 --- a/tests/e2e/Services/Database/DatabaseBase.php +++ b/tests/e2e/Services/Database/DatabaseBase.php @@ -568,6 +568,7 @@ trait DatabaseBase ]); // TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats + // TODO@kodumbeats min and max are rounded in error message $floatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],