Skip to content

Commit

Permalink
feat: added validators to check objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Dec 11, 2023
1 parent 7cb6634 commit 09d1ac0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ jobs:
run: pnpm lint
- name: Build Graphql queries
run: pnpm mesh build
- name: Format code
run: pnpm format
- name: Test project
run: pnpm test
env:
DATAMODEL_ID: ${{ secrets.DATAMODEL_ID }}
BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }}
API_KEY: ${{ secrets.API_KEY }}
ORGAINZATION_ID: ${{ secrets.ORGAINZATION_ID }}
5 changes: 5 additions & 0 deletions src/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../.mesh';
import { PDAFilter, PDAStatus } from '../types';
import { errorHandler } from '../utils/errorHandler';
import { isUUIDValid, validateObjectProperties } from '../utils/validators';

export class PDA {
public sdk: Sdk;
Expand All @@ -23,6 +24,7 @@ export class PDA {
*/
async getPDA(id: string) {
try {
isUUIDValid(id);
return await this.sdk.PDA_query({ id });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down Expand Up @@ -112,6 +114,7 @@ export class PDA {
*/
async changePDAStatus({ id, status }: { id: string; status: PDAStatus }) {
try {
isUUIDValid(id);
return await this.sdk.changePDAStatus_mutation({ input: { id, status } });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -126,6 +129,7 @@ export class PDA {
*/
async createPDA(pdaInput: CreatePDAInput) {
try {
validateObjectProperties(pdaInput);
return await this.sdk.createPDA_mutation({ input: pdaInput });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -143,6 +147,7 @@ export class PDA {
*/
async updatePDA(updatedPDA: UpdatePDAInput) {
try {
validateObjectProperties(updatedPDA);
return await this.sdk.updatePDA_mutation({ input: updatedPDA });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const STRING_VALIDATION_LENGTH = 3;
export const TEST_DEFAULT_TIMEOUT = 10000;
40 changes: 37 additions & 3 deletions src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import { STRING_VALIDATION_LENGTH } from './constants';

export const isEmailValid = (email: string): boolean => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) throw new Error(`${email} is not valid`);
return true;
};

export const isStringValid = (value: string): boolean => {
return value.length > 3;
if (!(value.length > STRING_VALIDATION_LENGTH))
throw new Error(
`${value} should be atleast ${STRING_VALIDATION_LENGTH} length`,
);
return true;
};

export const isUUIDValid = (uuid: string): boolean => {
const uuidRegex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
return uuidRegex.test(uuid);
if (!uuidRegex.test(uuid)) throw new Error(`${uuid} is not valid`);
return true;
};

export const isWalletAddressvalid = (address: string): boolean => {
const ethereumAddressRegex = /^(0x)?[0-9a-fA-F]{40}$/;
return ethereumAddressRegex.test(address);
if (!ethereumAddressRegex.test(address))
throw new Error(`${address} is not valid`);
return true;
};

export const isDateValid = (date: string): boolean => {
const parsedDate = new Date(date);
if (isNaN(parsedDate.getTime())) {
throw new Error(`${date} is not valid`);
}
return true;
};

export const validateObjectProperties = (obj: Record<string, any>): void => {
for (const key in obj) {
if (typeof obj[key] === 'string') {
try {
if (key.toLocaleLowerCase().includes('id')) {
isUUIDValid(obj[key]);
}
if (key.toLocaleLowerCase().includes('date')) {
isDateValid(obj[key]);
} else isStringValid(obj[key]);
} catch (error) {
throw error;
}
}
}
};
1 change: 1 addition & 0 deletions test/stubs/pda.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ export const pdaCreateStub = (): CreatePDAInput => ({
type: UserIdentifierType.GATEWAY_ID,
value: 'sid',
},
expirationDate: new Date('2021-01-01T12:00:0'),
});

0 comments on commit 09d1ac0

Please sign in to comment.