forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidations.ts
66 lines (63 loc) · 2.57 KB
/
validations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as Yup from 'yup';
const regexChecks = {
address_details: {
address_city: /^\p{L}[\p{L}\s'.-]{0,99}$/u,
address_line_1: /^[\p{L}\p{Nd}\s'.,:;()\u00b0@#/-]{1,70}$/u,
address_line_2: /^[\p{L}\p{Nd}\s'.,:;()\u00b0@#/-]{0,70}$/u,
address_postcode: /^[a-zA-Z0-9\s-]{0,20}$/,
address_state: /^[\w\s\W'.;,-]{0,99}$/,
non_jersey_postcode: /^(?!\s*je.*)[a-zA-Z0-9\s-]*/i,
},
};
const addressPermittedSpecialCharactersMessage = ". , ' : ; ( ) ° @ # / -";
export const addressDetailValidations = (countryCode: string, isSvg: boolean) => ({
addressCity: Yup.string()
.required('City is required')
.max(99, 'Only 99 characters, please.')
.matches(
regexChecks.address_details.address_city,
'Only letters, periods, hyphens, apostrophes, and spaces, please.'
),
addressLine1: Yup.string()
.required('First line of address is required')
.max(70, 'Only 70 characters, please.')
.matches(
regexChecks.address_details.address_line_1,
`Use only the following special characters: ${addressPermittedSpecialCharactersMessage}`
)
.when({
is: () => isSvg,
then: Yup.string().test(
'po_box',
'P.O. Box is not accepted in address',
value => !/p[.\s]+o[.\s]+box/i.test(value ?? '')
),
}),
addressLine2: Yup.string()
.max(70, 'Only 70 characters, please.')
.matches(
regexChecks.address_details.address_line_2,
`Use only the following special characters: ${addressPermittedSpecialCharactersMessage}`
)
.when({
is: () => isSvg,
then: Yup.string().test(
'po_box',
'P.O. Box is not accepted in address',
value => !/p[.\s]+o[.\s]+box/i.test(value ?? '')
),
}),
addressPostcode: Yup.string()
.max(20, 'Please enter a postal/ZIP code under 20 characters.')
.matches(regexChecks.address_details.address_postcode, 'Letters, numbers, spaces, hyphens only')
.when({
is: () => countryCode === 'gb',
then: Yup.string().matches(
regexChecks.address_details.non_jersey_postcode,
'Our accounts and services are unavailable for the Jersey postal code.'
),
}),
addressState: Yup.string()
.required('State is required')
.matches(regexChecks.address_details.address_state, 'State is not in a proper format'),
});