Skip to content

Commit

Permalink
fix: replaced expect-runtime calls with Joi validation
Browse files Browse the repository at this point in the history
  • Loading branch information
xianglupeng authored and joonaun93 committed Sep 16, 2023
1 parent 60e9726 commit 1320bf3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 44 deletions.
31 changes: 18 additions & 13 deletions server/models/Trust.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ class Trust {
});
const [trustRelationship] = trustRelationships;

if(!trustRelationship){
if (!trustRelationship) {
throw new HttpError(
404,
'No such trust relationship exists or it is not associated with the current wallet.'
)
404,
'No such trust relationship exists or it is not associated with the current wallet.',
);
}

if (trustRelationship?.originator_wallet_id !== walletId) {
Expand All @@ -369,10 +369,12 @@ class Trust {
* target wallet
*/
async hasTrust(walletLoginId, trustType, senderWallet, receiveWallet) {

Joi.assert(trustType,
Joi.string()
.valid(...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE)));
Joi.assert(
trustType,
Joi.string().valid(
...Object.values(TrustRelationshipEnums.ENTITY_TRUST_REQUEST_TYPE),
),
);

const trustRelationships = await this.getTrustRelationshipsTrusted(
walletLoginId,
Expand Down Expand Up @@ -408,7 +410,7 @@ class Trust {
return false;
}

async getTrustRelationshipById({ walletId, trustRelationshipId}) {
async getTrustRelationshipById({ walletId, trustRelationshipId }) {
const filter = {
and: [
{
Expand All @@ -424,13 +426,16 @@ class Trust {
],
};

const [trustRelationship] = await this._trustRepository.getByFilter(filter)
const [trustRelationship] = await this._trustRepository.getByFilter(filter);

if(!trustRelationship){
throw new HttpError(404, 'No such trust relationship exists or it is not associated with the current wallet.')
if (!trustRelationship) {
throw new HttpError(
404,
'No such trust relationship exists or it is not associated with the current wallet.',
);
}

return trustRelationship
return trustRelationship;
}

// NOT YET IN USE
Expand Down
25 changes: 14 additions & 11 deletions server/repositories/BaseRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class BaseRepository {
const filterObjectCopy = { ...object };
const beforeFilter = object.before;
if (object.before) {
result.whereRaw(`cast(${Object.keys(beforeFilter)[0]} as date) <= ?`, [Object.values(beforeFilter)[0]])
result.whereRaw(`cast(${Object.keys(beforeFilter)[0]} as date) <= ?`, [
Object.values(beforeFilter)[0],
]);
delete filterObjectCopy.before;
}
const afterFilter = object.after;
Expand Down Expand Up @@ -97,13 +99,16 @@ class BaseRepository {
.count()
.table(this._tableName)
.where(filter);

Joi.assert(result, Joi.array().items(
Joi.object({
count: Joi.string().required()
})
));


Joi.assert(
result,
Joi.array().items(
Joi.object({
count: Joi.string().required(),
}),
),
);

return parseInt(result[0].count);
}

Expand Down Expand Up @@ -151,9 +156,7 @@ class BaseRepository {
.getDB()
.batchInsert(this._tableName, objects)
.returning('id');
Joi.assert(result, Joi.array().items(
Joi.string()
));
Joi.assert(result, Joi.array().items(Joi.string()));
return result;
}
}
Expand Down
45 changes: 30 additions & 15 deletions server/repositories/TokenRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@ class TokenRepository extends BaseRepository {
this._session = session;
}

// async getById(id) {
// const result = await this._session
// .getDB()(this._tableName)
// .where('id', id)
// .first();
// expect(
// result,
// () => new HttpError(404, `can not found token by id:${id}`),
// ).match({
// id: expect.any(String),
// });
// return result;
// }

async getById(id) {

Joi.assert(id, Joi.string().uuid());

const result = await this._session
.getDB()(this._tableName)
.where('id', id)
.first();
.getDB()(this._tableName)
.where('id', id)
.first();

try {
Joi.assert(result,
Joi.object({ id: Joi.string().required() })
.unknown()
.required());
Joi.assert(
result,
Joi.object({ id: Joi.string().required() }).unknown().required(),
);
} catch (error) {
throw new HttpError(404, `can not found token by id:${id}`);
throw new HttpError(404, `can not found token by id:${id}`);
}

return result;
Expand All @@ -34,12 +47,14 @@ class TokenRepository extends BaseRepository {
* select transaction table by transfer id, return matched tokens
*/
async getByTransferId(transferId, limit, offset = 0) {
return this._session.getDB().select('*')
.from('token')
.join('transaction', 'token.id', 'transaction.token_id')
.where('transaction.transfer_id', transferId)
.limit(limit)
.offset(offset);
return this._session
.getDB()
.select('*')
.from('token')
.join('transaction', 'token.id', 'transaction.token_id')
.where('transaction.transfer_id', transferId)
.limit(limit)
.offset(offset);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/repositories/TransferRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class TransferRepository extends BaseRepository {
'destination_wallet.id',
)
.where((builder) => this.whereBuilder(filter, builder));

let order = 'desc';
let column = 'transfer.created_at';

Expand Down
16 changes: 12 additions & 4 deletions server/repositories/WalletRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ class WalletRepository extends BaseRepository {
}

async getByName(wallet) {
Joi.assert(wallet, Joi.string().pattern(/^\S+$/).messages({
'string.pattern.base': `Invalid wallet name: "${wallet}"`
}) );
Joi.assert(
wallet,
Joi.string()
.pattern(/^\S+$/)
.messages({
'string.pattern.base': `Invalid wallet name: "${wallet}"`,
}),
);

const list = await this._session
.getDB()
Expand All @@ -27,7 +32,10 @@ class WalletRepository extends BaseRepository {
try {
Joi.assert(list, Joi.array().required().length(1));
} catch (error) {
throw new HttpError(404, `Could not find entity by wallet name: ${wallet}`);
throw new HttpError(
404,
`Could not find entity by wallet name: ${wallet}`,
);
}
return list[0];
}
Expand Down

0 comments on commit 1320bf3

Please sign in to comment.