diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index bd38b3b..c6d7447 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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 }} diff --git a/src/pda/pda.ts b/src/pda/pda.ts index ae2922d..d2670cb 100644 --- a/src/pda/pda.ts +++ b/src/pda/pda.ts @@ -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; @@ -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)); @@ -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)); @@ -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)); @@ -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)); diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..01ab5a4 --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,2 @@ +export const STRING_VALIDATION_LENGTH = 3; +export const TEST_DEFAULT_TIMEOUT = 10000; diff --git a/src/utils/validators.ts b/src/utils/validators.ts index 855859f..21a36a8 100644 --- a/src/utils/validators.ts +++ b/src/utils/validators.ts @@ -1,3 +1,5 @@ +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`); @@ -5,16 +7,48 @@ export const isEmailValid = (email: string): boolean => { }; 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): 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; + } + } + } }; diff --git a/test/stubs/pda.stub.ts b/test/stubs/pda.stub.ts index 7a8641f..3229ca9 100644 --- a/test/stubs/pda.stub.ts +++ b/test/stubs/pda.stub.ts @@ -33,4 +33,5 @@ export const pdaCreateStub = (): CreatePDAInput => ({ type: UserIdentifierType.GATEWAY_ID, value: 'sid', }, + expirationDate: new Date('2021-01-01T12:00:0'), });