-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added validators to check objects
- Loading branch information
1 parent
7cb6634
commit 09d1ac0
Showing
5 changed files
with
47 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters