mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 04:34:00 +00:00
Expand API validation.
This commit is contained in:
@@ -215,7 +215,7 @@ class TransactionController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$transactions = $collector->getJournals();
|
$transactions = $collector->getJournals();
|
||||||
$resource = new Item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
|
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
|
||||||
|
|
||||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
}
|
}
|
||||||
|
@@ -165,6 +165,7 @@ class TransactionRequest extends Request
|
|||||||
$this->emptySplitDescriptions($validator);
|
$this->emptySplitDescriptions($validator);
|
||||||
$this->foreignCurrencyInformation($validator);
|
$this->foreignCurrencyInformation($validator);
|
||||||
$this->validateAccountInformation($validator);
|
$this->validateAccountInformation($validator);
|
||||||
|
$this->validateSplitAccounts($validator);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -368,6 +369,9 @@ class TransactionRequest extends Request
|
|||||||
{
|
{
|
||||||
$data = $validator->getData();
|
$data = $validator->getData();
|
||||||
$transactions = $data['transactions'] ?? [];
|
$transactions = $data['transactions'] ?? [];
|
||||||
|
if(!isset($data['type'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
foreach ($transactions as $index => $transaction) {
|
foreach ($transactions as $index => $transaction) {
|
||||||
|
|
||||||
$sourceId = isset($transaction['source_id']) ? intval($transaction['source_id']) : null;
|
$sourceId = isset($transaction['source_id']) ? intval($transaction['source_id']) : null;
|
||||||
@@ -404,10 +408,57 @@ class TransactionRequest extends Request
|
|||||||
$this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
|
$this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s".', $data['type']));
|
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s" in validateAccountInformation().', $data['type']));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Validator $validator
|
||||||
|
*
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
protected function validateSplitAccounts(Validator $validator)
|
||||||
|
{
|
||||||
|
$data = $validator->getData();
|
||||||
|
$count = isset($data['transactions']) ? count($data['transactions']) : 0;
|
||||||
|
if ($count < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// collect all source ID's and destination ID's, if present:
|
||||||
|
$sources = [];
|
||||||
|
$destinations = [];
|
||||||
|
|
||||||
|
foreach ($data['transactions'] as $transaction) {
|
||||||
|
$sources[] = isset($transaction['source_id']) ? intval($transaction['source_id']) : 0;
|
||||||
|
$destinations[] = isset($transaction['destination_id']) ? intval($transaction['destination_id']) : 0;
|
||||||
|
}
|
||||||
|
$destinations = array_unique($destinations);
|
||||||
|
$sources = array_unique($sources);
|
||||||
|
// switch on type:
|
||||||
|
switch ($data['type']) {
|
||||||
|
case 'withdrawal':
|
||||||
|
if (count($sources) > 1) {
|
||||||
|
$validator->errors()->add('transactions.0.source_id', trans('validation.all_accounts_equal'));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'deposit':
|
||||||
|
if (count($destinations) > 1) {
|
||||||
|
$validator->errors()->add('transactions.0.destination_id', trans('validation.all_accounts_equal'));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'transfer':
|
||||||
|
if (count($sources) > 1 || count($destinations) > 1) {
|
||||||
|
$validator->errors()->add('transactions.0.source_id', trans('validation.all_accounts_equal'));
|
||||||
|
$validator->errors()->add('transactions.0.destination_id', trans('validation.all_accounts_equal'));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new FireflyException(sprintf('The validator cannot handle transaction type "%s" in validateSplitAccounts().', $data['type']));
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -29,6 +29,7 @@ return [
|
|||||||
'file_already_attached' => 'Uploaded file ":name" is already attached to this object.',
|
'file_already_attached' => 'Uploaded file ":name" is already attached to this object.',
|
||||||
'file_attached' => 'Succesfully uploaded file ":name".',
|
'file_attached' => 'Succesfully uploaded file ":name".',
|
||||||
'must_exist' => 'The ID in field :attribute does not exist in the database.',
|
'must_exist' => 'The ID in field :attribute does not exist in the database.',
|
||||||
|
'all_accounts_equal' => 'All accounts in this field must be equal.',
|
||||||
'belongs_user' => 'This value is invalid for this field.',
|
'belongs_user' => 'This value is invalid for this field.',
|
||||||
'at_least_one_transaction' => 'Need at least one transaction.',
|
'at_least_one_transaction' => 'Need at least one transaction.',
|
||||||
'require_currency_info' => 'The content of this field is invalid without currency information.',
|
'require_currency_info' => 'The content of this field is invalid without currency information.',
|
||||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user