Skip to content

Commit

Permalink
fix(condo): open-condo-software#3906 updated phone validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan committed May 2, 2024
1 parent 03d855f commit 042cd65
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions apps/condo/domains/common/hooks/useValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useCallback, useMemo } from 'react'
import { useIntl } from '@open-condo/next/intl'

import { SPECIAL_CHAR_REGEXP, MULTIPLE_EMAILS_REGEX } from '@condo/domains/common/constants/regexps'
import { normalizePhone } from '@condo/domains/common/utils/phone'
import { isValidTin } from '@condo/domains/organization/utils/tin.utils'


Expand Down Expand Up @@ -38,29 +37,24 @@ export const useValidations: UseValidations = (settings = {}) => {
const intl = useIntl()
const ThisFieldIsRequiredMessage = intl.formatMessage({ id: 'FieldIsRequired' })
const MobilePhoneIsNotValidMessage = intl.formatMessage({ id: 'global.input.error.wrongMobilePhone' })
const PhoneIsNotValidMessage = intl.formatMessage({ id: 'global.input.error.wrongPhone' })
const EmailErrorMessage = intl.formatMessage({ id: 'pages.auth.EmailIsNotValid' })
const FieldIsTooShortMessage = intl.formatMessage({ id: 'ValueIsTooShort' })
const FieldIsTooLongMessage = intl.formatMessage({ id: 'ValueIsTooLong' })
const NumberIsNotValidMessage = intl.formatMessage({ id: 'NumberIsNotValid' })
const TinValueIsInvalidMessage = intl.formatMessage({ id: 'pages.organizations.tin.InvalidValue' })
const EmailsAreInvalidMessage = intl.formatMessage({ id: 'global.input.error.wrongEmails' })

const { allowLandLine } = settings

const requiredValidator: Rule = useMemo(() => ({
required: true,
message: ThisFieldIsRequiredMessage,
}), [ThisFieldIsRequiredMessage])

const phoneValidator: Rule = useMemo(() => ({
validator: (_, value) => {
if (!value) return Promise.resolve()
const v = normalizePhone(value, allowLandLine)
if (!v) return Promise.reject(allowLandLine ? PhoneIsNotValidMessage : MobilePhoneIsNotValidMessage)
return Promise.resolve()
validator: (_, { valid }) => {
if (valid()) return Promise.resolve()
return Promise.reject(MobilePhoneIsNotValidMessage)
},
}), [MobilePhoneIsNotValidMessage, PhoneIsNotValidMessage, allowLandLine])
}), [MobilePhoneIsNotValidMessage])

const emailValidator: Rule = useMemo(() => ({
type: 'email',
Expand Down

0 comments on commit 042cd65

Please sign in to comment.