Improve Reimbursement.add validation
This commit is contained in:
parent
7ad2515b67
commit
15b47dbc42
@ -25,10 +25,21 @@ class Reimbursement extends Record {
|
|||||||
const token = attrs.token;
|
const token = attrs.token;
|
||||||
const contributorId = attrs.contributorId;
|
const contributorId = attrs.contributorId;
|
||||||
const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) );
|
const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) );
|
||||||
|
let errorMessage;
|
||||||
|
|
||||||
if (!amount > 0 || !token || token === '' || !contributorId || contributorId === '' || !expenses.length > 0) {
|
if (typeof amount !== 'number' || amount <= 0) {
|
||||||
return Promise.reject(new Error('Invalid data. amount, token, expenses is required.'));
|
errorMessage = 'Invalid data: amount must be a positive number.';
|
||||||
}
|
}
|
||||||
|
if (!token || token === '') {
|
||||||
|
errorMessage = 'Invalid data: token must be a token address.';
|
||||||
|
}
|
||||||
|
if (!contributorId || contributorId === '') {
|
||||||
|
errorMessage = 'Invalid data: contributorId is required.';
|
||||||
|
}
|
||||||
|
if (expenses.length === 0) {
|
||||||
|
errorMessage = 'Invalid data: at least one expense item is required.';
|
||||||
|
}
|
||||||
|
if (errorMessage) { return Promise.reject(new Error(errorMessage)); }
|
||||||
|
|
||||||
return Promise.all(expenses.map(e => e.validate()))
|
return Promise.all(expenses.map(e => e.validate()))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
@ -39,7 +50,7 @@ class Reimbursement extends Record {
|
|||||||
const reimbursement = [
|
const reimbursement = [
|
||||||
amount,
|
amount,
|
||||||
token,
|
token,
|
||||||
contributorId,
|
parseInt(contributorId),
|
||||||
ipfsHashAttr.hashDigest,
|
ipfsHashAttr.hashDigest,
|
||||||
ipfsHashAttr.hashFunction,
|
ipfsHashAttr.hashFunction,
|
||||||
ipfsHashAttr.hashSize,
|
ipfsHashAttr.hashSize,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user