Skip to content

Commit 3abec67

Browse files
committed
feat: add google-libphonenumber dependency and implement phone validation function
1 parent a964953 commit 3abec67

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@tippyjs/react": "^4.2.0",
4545
"@typeform/embed-react": "2.20.0",
4646
"@types/dompurify": "^3.0.5",
47+
"@types/google-libphonenumber": "^7.4.30",
4748
"@types/json-schema": "^7.0.15",
4849
"@types/react": "17.0.39",
4950
"@types/react-dom": "17.0.13",
@@ -115,6 +116,7 @@
115116
"dayjs": "^1.11.13",
116117
"fast-json-patch": "^3.1.1",
117118
"framer-motion": "^6.5.1",
119+
"google-libphonenumber": "^3.2.40",
118120
"jsonpath-plus": "^10.3.0",
119121
"marked": "^13.0.3",
120122
"nanoid": "^3.3.8",

src/Shared/validations.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { PhoneNumberUtil } from 'google-libphonenumber'
1718
import { getSanitizedIframe } from '@Common/Helper'
1819
import { customizeValidator } from '@rjsf/validator-ajv8'
1920
import { PATTERNS } from '@Common/Constants'
2021
import { parse } from 'yaml'
2122
import { URLProtocolType } from './types'
2223
import { SKIP_LABEL_KEY_VALIDATION_PREFIX } from './constants'
2324

25+
const phoneUtil = PhoneNumberUtil.getInstance()
26+
2427
export interface ValidationResponseType {
2528
isValid: boolean
2629
message?: string
@@ -495,3 +498,18 @@ export const validateYAML = (yamlString: string, isRequired?: boolean): Validati
495498
}
496499
}
497500
}
501+
502+
export const isPhoneValid = (phone: string): ValidationResponseType => {
503+
try {
504+
const isValid = phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone))
505+
return {
506+
isValid,
507+
message: isValid ? '' : 'Invalid phone number',
508+
}
509+
} catch (error) {
510+
return {
511+
isValid: false,
512+
message: error.message || 'Invalid phone number',
513+
}
514+
}
515+
}

0 commit comments

Comments
 (0)