1
0
Fork 0
mirror of synced 2024-06-01 10:29:48 +12:00

Encode format as json string

This commit is contained in:
kodumbeats 2021-07-27 14:19:37 -04:00
parent b9fea89e73
commit 1eae5490df
3 changed files with 35 additions and 14 deletions

View file

@ -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);
});

View file

@ -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

View file

@ -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'],