Expand API validation.

This commit is contained in:
James Cole 2018-02-20 18:03:02 +01:00
parent 9d457787f7
commit f16760d607
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 594 additions and 354 deletions

View File

@ -215,7 +215,7 @@ class TransactionController extends Controller
}
$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');
}

View File

@ -165,6 +165,7 @@ class TransactionRequest extends Request
$this->emptySplitDescriptions($validator);
$this->foreignCurrencyInformation($validator);
$this->validateAccountInformation($validator);
$this->validateSplitAccounts($validator);
}
);
}
@ -368,6 +369,9 @@ class TransactionRequest extends Request
{
$data = $validator->getData();
$transactions = $data['transactions'] ?? [];
if(!isset($data['type'])) {
return;
}
foreach ($transactions as $index => $transaction) {
$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);
break;
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;
}
}

View File

@ -29,6 +29,7 @@ return [
'file_already_attached' => 'Uploaded file ":name" is already attached to this object.',
'file_attached' => 'Succesfully uploaded file ":name".',
'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.',
'at_least_one_transaction' => 'Need at least one transaction.',
'require_currency_info' => 'The content of this field is invalid without currency information.',

File diff suppressed because it is too large Load Diff