Skip to content

Commit

Permalink
feat: simple validation and some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
R11manish committed Oct 2, 2024
1 parent 93ddf15 commit febd13c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/modules/account/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ export class Wallet {
}

/**
* This async function adds a wallet address to the user's account and returns a response.
* @param {string} wallet - The `add` function is an asynchronous function that takes a `wallet`
* parameter of type string. This function sends a POST request to create a new wallet for the current
* account. The wallet address is provided in the `wallet` parameter and is sent in the request body.
* The `add` function in TypeScript adds a new wallet address to the user's account asynchronously.
* @param {string} address - The `add` function in the code snippet you provided is an asynchronous
* function that takes a `string` parameter called `address`. The function first checks if the
* `address` string is empty using a validation service method `isEmptyString(address)`. Then, it makes
* a POST request to a specific endpoint
* @returns The `add` function is returning a `Promise` that resolves to a `MyAccountResponse` object.
*/
async add(wallet: string): Promise<MyAccountResponse> {

async add(address: string): Promise<MyAccountResponse> {
this.validationService.isEmptyString(address);
const { data, error, response } = await this.client.POST(
'/accounts/me/wallets',
{
body: { address: wallet },
body: { address: address },
},
);

Expand All @@ -46,6 +49,8 @@ export class Wallet {
* object.
*/
async remove(address: string): Promise<MyAccountResponse> {
this.validationService.isEmptyString(address);

const { data, error, response } = await this.client.DELETE(
'/accounts/me/wallets/{address}',
{
Expand Down
19 changes: 19 additions & 0 deletions src/services/validator-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ export class ValidationService {
return true;
}

/**
* The function checks if a string is empty or contains only whitespace.
* @param {string} value - The `value` parameter in the `isEmptyString` function is a string that you
* want to check for emptiness or containing only whitespace characters. The function will return
* `true` if the string is not empty or does not contain only whitespace characters, otherwise it will
* throw an error indicating that the string
* @returns The `isEmptyString` function is returning a boolean value, either `true` if the provided
* string is not empty or contains only whitespace, or it will throw an error if the string is empty or
* contains only whitespace.
*/
public isEmptyString(value: string): boolean {
if (value.trim().length === 0) {
throw new Error(
'The provided string is empty or contains only whitespace',
);
}
return true;
}

/**
* The function `validateString` checks if a given string meets a minimum length requirement and
* returns a boolean value accordingly.
Expand Down
4 changes: 2 additions & 2 deletions test/data-model.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ValidationService } from '../src/services/validator-service';
import { GTWError } from '../src/helpers/custom-error';
import { DataModel } from '../src/modules/data-model/data-model';
import { Config, DataModelRequest } from '../src/common/types';
import { mockClient, mockGet, mockPost, mockPut } from './stubs/common.stub';
import { DataModelRequest } from '../src/common/types';
import { mockClient, mockGet, mockPost } from './stubs/common.stub';
import { routes } from '../src/common/routes';

const mockValidationService = {} as ValidationService;
Expand Down
11 changes: 11 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('UTILS VALIDATORS TESTING', () => {
).toThrow('f17ac10b-58cc-4372-a567 is not valid');
});

it('isEmptyString validator', () => {
const result = validationService.isEmptyString('test pda');
expect(result).toBeDefined();
expect(() => validationService.isEmptyString('')).toThrow(
'The provided string is empty or contains only whitespace',
);
expect(() => validationService.isEmptyString(' ')).toThrow(
'The provided string is empty or contains only whitespace',
);
});

it('string validator', () => {
const result = validationService.validateString('test pda');
expect(result).toBeDefined();
Expand Down

0 comments on commit febd13c

Please sign in to comment.