@digigov/form 1.1.2-fd2cea11 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -1
- package/cjs/inputs/Input/Input.stories/index.js +0 -12
- package/cjs/inputs/Input/__stories__/MobilePhone/index.js +0 -4
- package/cjs/inputs/Input/__stories__/PhoneNumber/index.js +1 -4
- package/cjs/validators/utils/phone/index.js +70 -94
- package/index.js +1 -1
- package/inputs/Input/Input.stories/index.js +0 -1
- package/inputs/Input/Input.stories.d.ts +0 -1
- package/inputs/Input/__stories__/MobilePhone/index.js +0 -4
- package/inputs/Input/__stories__/PhoneNumber/index.js +1 -4
- package/package.json +5 -4
- package/src/inputs/Input/Input.stories.js +0 -1
- package/src/inputs/Input/__stories__/MobilePhone.tsx +0 -4
- package/src/inputs/Input/__stories__/PhoneNumber.tsx +1 -4
- package/src/validators/utils/phone.ts +72 -152
- package/validators/utils/phone/index.js +66 -92
- package/validators/utils/phone.d.ts +2 -2
- package/cjs/inputs/Input/__stories__/LandlineNumber/index.js +0 -36
- package/inputs/Input/__stories__/LandlineNumber/index.js +0 -26
- package/inputs/Input/__stories__/LandlineNumber/package.json +0 -6
- package/inputs/Input/__stories__/LandlineNumber.d.ts +0 -3
- package/src/inputs/Input/__stories__/LandlineNumber.tsx +0 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# Change Log - @digigov/form
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Fri, 16 Feb 2024 09:57:32 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 1.1.2
|
|
6
|
+
Fri, 16 Feb 2024 09:57:32 GMT
|
|
7
|
+
|
|
8
|
+
_Version update only_
|
|
4
9
|
|
|
5
10
|
## 1.1.1
|
|
6
11
|
Mon, 29 Jan 2024 17:45:11 GMT
|
|
@@ -55,18 +55,6 @@ Object.keys(_PhoneNumber).forEach(function (key) {
|
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
|
-
var _LandlineNumber = require("@digigov/form/inputs/Input/__stories__/LandlineNumber");
|
|
59
|
-
Object.keys(_LandlineNumber).forEach(function (key) {
|
|
60
|
-
if (key === "default" || key === "__esModule") return;
|
|
61
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
62
|
-
if (key in exports && exports[key] === _LandlineNumber[key]) return;
|
|
63
|
-
Object.defineProperty(exports, key, {
|
|
64
|
-
enumerable: true,
|
|
65
|
-
get: function get() {
|
|
66
|
-
return _LandlineNumber[key];
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
58
|
var _MobilePhone = require("@digigov/form/inputs/Input/__stories__/MobilePhone");
|
|
71
59
|
Object.keys(_MobilePhone).forEach(function (key) {
|
|
72
60
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -24,10 +24,7 @@ var PhoneNumber = exports.PhoneNumber = function PhoneNumber() {
|
|
|
24
24
|
name: "phone-number",
|
|
25
25
|
type: "phone_number",
|
|
26
26
|
label: {
|
|
27
|
-
primary: '
|
|
28
|
-
},
|
|
29
|
-
extra: {
|
|
30
|
-
countries: ['gr']
|
|
27
|
+
primary: 'Σταθερό τηλέφωνο'
|
|
31
28
|
},
|
|
32
29
|
required: true
|
|
33
30
|
}), _ref);
|
|
@@ -1,111 +1,87 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.PHONE_NUMBER_VALIDATOR = exports.MOBILE_PHONE_VALIDATOR = void 0;
|
|
8
|
-
exports.
|
|
8
|
+
exports.matchTypeOfPhoneNumber = matchTypeOfPhoneNumber;
|
|
9
9
|
exports.validatePhoneNumber = validatePhoneNumber;
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var country = _countries[_i];
|
|
20
|
-
expanded[country] = {
|
|
21
|
-
fixedLine: {
|
|
22
|
-
possibleLengths: {
|
|
23
|
-
_national: compressed[country][3].map(String)
|
|
24
|
-
},
|
|
25
|
-
nationalNumberPattern: compressed[country][11][0][0]
|
|
26
|
-
},
|
|
27
|
-
mobile: {
|
|
28
|
-
nationalNumberPattern: compressed[country][11][1][0]
|
|
29
|
-
},
|
|
30
|
-
tollFree: {
|
|
31
|
-
nationalNumberPattern: compressed[country][11][2][0]
|
|
32
|
-
},
|
|
33
|
-
premiumRate: {
|
|
34
|
-
nationalNumberPattern: compressed[country][11][3][0]
|
|
35
|
-
},
|
|
36
|
-
sharedCost: {
|
|
37
|
-
nationalNumberPattern: compressed[country][11][9][0]
|
|
38
|
-
},
|
|
39
|
-
personalNumber: {
|
|
40
|
-
nationalNumberPattern: compressed[country][11][4][0]
|
|
41
|
-
},
|
|
42
|
-
uan: {
|
|
43
|
-
nationalNumberPattern: compressed[country][11][6][0]
|
|
44
|
-
},
|
|
45
|
-
id: country,
|
|
46
|
-
countryCode: compressed[country][0],
|
|
47
|
-
internationalPrefix: compressed[country][1]
|
|
48
|
-
};
|
|
10
|
+
var gPhoneNumber = _interopRequireWildcard(require("google-libphonenumber"));
|
|
11
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
12
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
13
|
+
function validatePhoneNumber(phoneNumber) {
|
|
14
|
+
var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
|
|
15
|
+
var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
|
|
16
|
+
var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
17
|
+
if (!countries || countries.length === 0) {
|
|
18
|
+
return true;
|
|
49
19
|
}
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return
|
|
20
|
+
return countries.some(function (country) {
|
|
21
|
+
try {
|
|
22
|
+
var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
|
|
23
|
+
if (phoneUtil.isValidNumber(phone)) {
|
|
24
|
+
if (typeOfPhoneNumber) {
|
|
25
|
+
if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
|
|
26
|
+
return true;
|
|
27
|
+
} else {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.error(error);
|
|
37
|
+
return false;
|
|
68
38
|
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// If no category matches, return 'Unknown'
|
|
72
|
-
return "unknown";
|
|
73
|
-
}
|
|
74
|
-
function getNumberType(phoneNumber, country) {
|
|
75
|
-
var spec = PHONENUMBER_SPEC[country.toUpperCase()];
|
|
76
|
-
if (!spec) {
|
|
77
|
-
throw new Error("Country ".concat(country, " is not supported"));
|
|
78
|
-
}
|
|
79
|
-
var type = discoverPhoneType(phoneNumber, spec);
|
|
80
|
-
return type;
|
|
39
|
+
});
|
|
81
40
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
41
|
+
var phoneNumberTypes = {
|
|
42
|
+
0: 'landline',
|
|
43
|
+
1: 'mobile',
|
|
44
|
+
2: 'landline_or_mobile'
|
|
45
|
+
};
|
|
46
|
+
function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
|
|
47
|
+
try {
|
|
48
|
+
var phoneNumberType = phoneUtil.getNumberType(phone);
|
|
49
|
+
var numberType = phoneNumberTypes[phoneNumberType];
|
|
50
|
+
if (numberType === 'landline_or_mobile' || numberType === type) {
|
|
87
51
|
return true;
|
|
52
|
+
} else {
|
|
53
|
+
return false;
|
|
88
54
|
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error);
|
|
89
57
|
return false;
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
function isNumberOfType(phoneNumber, country, type) {
|
|
93
|
-
var numberType = getNumberType(phoneNumber, country);
|
|
94
|
-
if (numberType === type) {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
function validatePhoneNumber(phoneNumber) {
|
|
100
|
-
var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
|
|
101
|
-
var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
|
|
102
|
-
if (!countries || countries.length === 0) {
|
|
103
|
-
return true;
|
|
104
58
|
}
|
|
105
|
-
return isPhoneNumberValid(phoneNumber, countries, typeOfPhoneNumber ? [typeOfPhoneNumber] : undefined);
|
|
106
59
|
}
|
|
107
60
|
function validateMobile(value) {
|
|
108
|
-
|
|
61
|
+
var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
62
|
+
var origValue = value;
|
|
63
|
+
// probably catch all the cases with a regex instead of gphonenumber
|
|
64
|
+
if (!value.match(/^((\+|00){0,1}\d{1,3}[- ]?)?\d{10}$/)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
var phone;
|
|
69
|
+
try {
|
|
70
|
+
phone = phoneUtil.parse(value);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
try {
|
|
73
|
+
value = '+' + value;
|
|
74
|
+
phone = phoneUtil.parse(value);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
value = '+30' + origValue;
|
|
77
|
+
phone = phoneUtil.parse(value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return phoneUtil.isValidNumber(phone);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.error(err);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
109
85
|
}
|
|
110
86
|
var MOBILE_PHONE_VALIDATOR = exports.MOBILE_PHONE_VALIDATOR = {
|
|
111
87
|
name: 'mobile-phone-validator',
|
|
@@ -119,7 +95,7 @@ var MOBILE_PHONE_VALIDATOR = exports.MOBILE_PHONE_VALIDATOR = {
|
|
|
119
95
|
};
|
|
120
96
|
var PHONE_NUMBER_VALIDATOR = exports.PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
|
|
121
97
|
var _field$extra, _field$extra2;
|
|
122
|
-
var
|
|
98
|
+
var countryCode = field === null || field === void 0 ? void 0 : (_field$extra = field.extra) === null || _field$extra === void 0 ? void 0 : _field$extra.countries;
|
|
123
99
|
var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.phoneType;
|
|
124
100
|
return {
|
|
125
101
|
name: 'phone-number-validator',
|
|
@@ -137,7 +113,7 @@ var PHONE_NUMBER_VALIDATOR = exports.PHONE_NUMBER_VALIDATOR = function PHONE_NUM
|
|
|
137
113
|
if (!value) {
|
|
138
114
|
return true;
|
|
139
115
|
}
|
|
140
|
-
return validatePhoneNumber(value,
|
|
116
|
+
return validatePhoneNumber(value, countryCode, typeOfPhone);
|
|
141
117
|
}
|
|
142
118
|
};
|
|
143
119
|
};
|
package/index.js
CHANGED
|
@@ -8,7 +8,6 @@ export * from '@digigov/form/inputs/Input/__stories__/Default';
|
|
|
8
8
|
export * from '@digigov/form/inputs/Input/__stories__/Integer';
|
|
9
9
|
export * from '@digigov/form/inputs/Input/__stories__/Boolean';
|
|
10
10
|
export * from '@digigov/form/inputs/Input/__stories__/PhoneNumber';
|
|
11
|
-
export * from '@digigov/form/inputs/Input/__stories__/LandlineNumber';
|
|
12
11
|
export * from '@digigov/form/inputs/Input/__stories__/MobilePhone';
|
|
13
12
|
export * from '@digigov/form/inputs/Input/__stories__/AFM';
|
|
14
13
|
export * from '@digigov/form/inputs/Input/__stories__/IBAN';
|
|
@@ -8,7 +8,6 @@ export * from "@digigov/form/inputs/Input/__stories__/Default";
|
|
|
8
8
|
export * from "@digigov/form/inputs/Input/__stories__/Integer";
|
|
9
9
|
export * from "@digigov/form/inputs/Input/__stories__/Boolean";
|
|
10
10
|
export * from "@digigov/form/inputs/Input/__stories__/PhoneNumber";
|
|
11
|
-
export * from "@digigov/form/inputs/Input/__stories__/LandlineNumber";
|
|
12
11
|
export * from "@digigov/form/inputs/Input/__stories__/MobilePhone";
|
|
13
12
|
export * from "@digigov/form/inputs/Input/__stories__/AFM";
|
|
14
13
|
export * from "@digigov/form/inputs/Input/__stories__/IBAN";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digigov/form",
|
|
3
|
-
"version": "1.1.2
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "@digigov form builder",
|
|
5
5
|
"author": "GRNET Developers <devs@lists.grnet.gr>",
|
|
6
6
|
"license": "BSD-2-Clause",
|
|
@@ -12,14 +12,15 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"react-hook-form": "7.33.1",
|
|
14
14
|
"yup": "0.32.11",
|
|
15
|
+
"google-libphonenumber": "3.2.8",
|
|
15
16
|
"dayjs": "1.10.4",
|
|
16
17
|
"@hookform/resolvers": "2.9.6",
|
|
17
18
|
"publint": "0.1.8",
|
|
18
|
-
"@digigov/react-icons": "1.1.2
|
|
19
|
+
"@digigov/react-icons": "1.1.2"
|
|
19
20
|
},
|
|
20
21
|
"peerDependencies": {
|
|
21
|
-
"@digigov/ui": "1.1.2
|
|
22
|
-
"@digigov/react-core": "1.2.0
|
|
22
|
+
"@digigov/ui": "1.1.2",
|
|
23
|
+
"@digigov/react-core": "1.2.0",
|
|
23
24
|
"clsx": "1.1.1",
|
|
24
25
|
"react": "^16.8.0 || ^17.0.0",
|
|
25
26
|
"react-dom": "^16.8.0 || ^17.0.0"
|
|
@@ -8,7 +8,6 @@ export * from '@digigov/form/inputs/Input/__stories__/Default';
|
|
|
8
8
|
export * from '@digigov/form/inputs/Input/__stories__/Integer';
|
|
9
9
|
export * from '@digigov/form/inputs/Input/__stories__/Boolean';
|
|
10
10
|
export * from '@digigov/form/inputs/Input/__stories__/PhoneNumber';
|
|
11
|
-
export * from '@digigov/form/inputs/Input/__stories__/LandlineNumber';
|
|
12
11
|
export * from '@digigov/form/inputs/Input/__stories__/MobilePhone';
|
|
13
12
|
export * from '@digigov/form/inputs/Input/__stories__/AFM';
|
|
14
13
|
export * from '@digigov/form/inputs/Input/__stories__/IBAN';
|
|
@@ -1,167 +1,87 @@
|
|
|
1
|
+
import * as gPhoneNumber from 'google-libphonenumber';
|
|
1
2
|
import { FieldSpec } from '@digigov/form/types';
|
|
2
3
|
import { ValidatorSchema } from '@digigov/form/validators/types';
|
|
3
4
|
|
|
4
5
|
export type PhoneNumberType = 'landline' | 'mobile' | null;
|
|
5
|
-
// add more countries from here libphonenumber-js/metadata.full.json
|
|
6
|
-
const countryPhoneData = {
|
|
7
|
-
"GR": [
|
|
8
|
-
"30",
|
|
9
|
-
"00",
|
|
10
|
-
"5005000\\d{3}|8\\d{9,11}|(?:[269]\\d|70)\\d{8}",
|
|
11
|
-
[10, 11, 12],
|
|
12
|
-
[
|
|
13
|
-
["(\\d{2})(\\d{4})(\\d{4})", "$1 $2 $3", ["21|7"]],
|
|
14
|
-
[
|
|
15
|
-
"(\\d{4})(\\d{6})",
|
|
16
|
-
"$1 $2",
|
|
17
|
-
["2(?:2|3[2-57-9]|4[2-469]|5[2-59]|6[2-9]|7[2-69]|8[2-49])|5"]
|
|
18
|
-
],
|
|
19
|
-
["(\\d{3})(\\d{3})(\\d{4})", "$1 $2 $3", ["[2689]"]],
|
|
20
|
-
["(\\d{3})(\\d{3,4})(\\d{5})", "$1 $2 $3", ["8"]]
|
|
21
|
-
],
|
|
22
|
-
0,
|
|
23
|
-
0,
|
|
24
|
-
0,
|
|
25
|
-
0,
|
|
26
|
-
0,
|
|
27
|
-
0,
|
|
28
|
-
[
|
|
29
|
-
[
|
|
30
|
-
"2(?:1\\d\\d|2(?:2[1-46-9]|[36][1-8]|4[1-7]|5[1-4]|7[1-5]|[89][1-9])|3(?:1\\d|2[1-57]|[35][1-3]|4[13]|7[1-7]|8[124-6]|9[1-79])|4(?:1\\d|2[1-8]|3[1-4]|4[13-5]|6[1-578]|9[1-5])|5(?:1\\d|[29][1-4]|3[1-5]|4[124]|5[1-6])|6(?:1\\d|[269][1-6]|3[1245]|4[1-7]|5[13-9]|7[14]|8[1-5])|7(?:1\\d|2[1-5]|3[1-6]|4[1-7]|5[1-57]|6[135]|9[125-7])|8(?:1\\d|2[1-5]|[34][1-4]|9[1-57]))\\d{6}",
|
|
31
|
-
[10]
|
|
32
|
-
],
|
|
33
|
-
["68[57-9]\\d{7}|(?:69|94)\\d{8}", [10]],
|
|
34
|
-
["800\\d{7,9}"],
|
|
35
|
-
["90[19]\\d{7}", [10]],
|
|
36
|
-
["70\\d{8}", [10]],
|
|
37
|
-
0,
|
|
38
|
-
["5005000\\d{3}", [10]],
|
|
39
|
-
0,
|
|
40
|
-
0,
|
|
41
|
-
["8(?:0[16]|12|[27]5|50)\\d{7}", [10]]
|
|
42
|
-
]
|
|
43
|
-
]
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function expandPhoneNumberStructure(compressed) {
|
|
47
|
-
const countries = Object.keys(compressed);
|
|
48
|
-
const expanded: any = {}
|
|
49
|
-
for (const country of countries) {
|
|
50
|
-
expanded[country] = {
|
|
51
|
-
fixedLine: {
|
|
52
|
-
possibleLengths: {
|
|
53
|
-
_national:
|
|
54
|
-
compressed[country][3].map(
|
|
55
|
-
String
|
|
56
|
-
),
|
|
57
|
-
},
|
|
58
|
-
nationalNumberPattern:
|
|
59
|
-
compressed[country][11][0][0],
|
|
60
|
-
},
|
|
61
|
-
mobile: {
|
|
62
|
-
nationalNumberPattern:
|
|
63
|
-
compressed[country][11][1][0],
|
|
64
|
-
},
|
|
65
|
-
tollFree: {
|
|
66
|
-
nationalNumberPattern:
|
|
67
|
-
compressed[country][11][2][0],
|
|
68
|
-
},
|
|
69
|
-
premiumRate: {
|
|
70
|
-
nationalNumberPattern:
|
|
71
|
-
compressed[country][11][3][0],
|
|
72
|
-
},
|
|
73
|
-
sharedCost: {
|
|
74
|
-
nationalNumberPattern:
|
|
75
|
-
compressed[country][11][9][0],
|
|
76
|
-
},
|
|
77
|
-
personalNumber: {
|
|
78
|
-
nationalNumberPattern:
|
|
79
|
-
compressed[country][11][4][0],
|
|
80
|
-
},
|
|
81
|
-
uan: {
|
|
82
|
-
nationalNumberPattern:
|
|
83
|
-
compressed[country][11][6][0],
|
|
84
|
-
},
|
|
85
|
-
id: country,
|
|
86
|
-
countryCode:
|
|
87
|
-
compressed[country][0],
|
|
88
|
-
internationalPrefix:
|
|
89
|
-
compressed[country][1],
|
|
90
6
|
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return expanded;
|
|
94
|
-
}
|
|
95
|
-
const PHONENUMBER_SPEC = expandPhoneNumberStructure(countryPhoneData);
|
|
96
|
-
export function discoverPhoneType(
|
|
97
|
-
phoneNumber: string,
|
|
98
|
-
config: any
|
|
99
|
-
): string {
|
|
100
|
-
const cleanNumber = phoneNumber
|
|
101
|
-
.replace(/\D/g, "")
|
|
102
|
-
.replace(/\s/g, "")
|
|
103
|
-
.replace(/^\+/, "")
|
|
104
|
-
.replace(new RegExp("^" + config.internationalPrefix, ""), "")
|
|
105
|
-
.replace(new RegExp("^" + config.countryCode, ""), "");
|
|
106
|
-
const categories = {
|
|
107
|
-
"landline": config.fixedLine,
|
|
108
|
-
mobile: config.mobile,
|
|
109
|
-
"toll-free": config.tollFree,
|
|
110
|
-
"premium-rate": config.premiumRate,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
for (const [categoryName, categoryDetails] of Object.entries(categories)) {
|
|
114
|
-
const pattern = new RegExp(
|
|
115
|
-
"^(" + categoryDetails.nationalNumberPattern.replace(/\s/g, "") + ")$",
|
|
116
|
-
""
|
|
117
|
-
);
|
|
118
|
-
if (cleanNumber.match(pattern)) {
|
|
119
|
-
return categoryName; // Returns the category name if the number matches the pattern
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// If no category matches, return 'Unknown'
|
|
124
|
-
return "unknown";
|
|
125
|
-
}
|
|
126
|
-
function getNumberType(phoneNumber: string, country: string) {
|
|
127
|
-
const spec = PHONENUMBER_SPEC[country.toUpperCase()];
|
|
128
|
-
if(!spec) {
|
|
129
|
-
throw new Error(`Country ${country} is not supported`);
|
|
130
|
-
}
|
|
131
|
-
const type = discoverPhoneType(phoneNumber, spec);
|
|
132
|
-
return type;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function isPhoneNumberValid(phoneNumber: string, countries: string[], types=['mobile', 'landline']) {
|
|
136
|
-
return countries.some((country) => {
|
|
137
|
-
const numberType = getNumberType(phoneNumber, country);
|
|
138
|
-
if (numberType && types.includes(numberType)) {
|
|
139
|
-
return true
|
|
140
|
-
}
|
|
141
|
-
return false
|
|
142
|
-
})
|
|
143
|
-
}
|
|
144
|
-
function isNumberOfType(phoneNumber: string, country: string, type: string) {
|
|
145
|
-
const numberType = getNumberType(phoneNumber, country);
|
|
146
|
-
if (numberType === type) {
|
|
147
|
-
return true
|
|
148
|
-
}
|
|
149
|
-
return false
|
|
150
|
-
}
|
|
151
7
|
export function validatePhoneNumber(
|
|
152
8
|
phoneNumber: string,
|
|
153
9
|
countries: Array<string> = ['gr'],
|
|
154
|
-
typeOfPhoneNumber
|
|
10
|
+
typeOfPhoneNumber: PhoneNumberType
|
|
155
11
|
): boolean {
|
|
12
|
+
const phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
156
13
|
if (!countries || countries.length === 0) {
|
|
157
14
|
return true;
|
|
158
15
|
}
|
|
159
|
-
return
|
|
16
|
+
return countries.some((country) => {
|
|
17
|
+
try {
|
|
18
|
+
const phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
|
|
19
|
+
if (phoneUtil.isValidNumber(phone)) {
|
|
20
|
+
if (typeOfPhoneNumber) {
|
|
21
|
+
if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
|
|
22
|
+
return true;
|
|
23
|
+
} else {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error(error);
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const phoneNumberTypes = {
|
|
38
|
+
0: 'landline',
|
|
39
|
+
1: 'mobile',
|
|
40
|
+
2: 'landline_or_mobile',
|
|
41
|
+
};
|
|
42
|
+
export function matchTypeOfPhoneNumber(
|
|
43
|
+
phone: any,
|
|
44
|
+
type: string,
|
|
45
|
+
phoneUtil: any
|
|
46
|
+
): boolean {
|
|
47
|
+
try {
|
|
48
|
+
const phoneNumberType = phoneUtil.getNumberType(phone);
|
|
49
|
+
const numberType = phoneNumberTypes[phoneNumberType];
|
|
50
|
+
if (numberType === 'landline_or_mobile' || numberType === type) {
|
|
51
|
+
return true;
|
|
52
|
+
} else {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error);
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
160
59
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
60
|
function validateMobile(value): boolean {
|
|
164
|
-
|
|
61
|
+
const phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
62
|
+
const origValue = value;
|
|
63
|
+
// probably catch all the cases with a regex instead of gphonenumber
|
|
64
|
+
if (!value.match(/^((\+|00){0,1}\d{1,3}[- ]?)?\d{10}$/)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
let phone;
|
|
69
|
+
try {
|
|
70
|
+
phone = phoneUtil.parse(value);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
try {
|
|
73
|
+
value = '+' + value;
|
|
74
|
+
phone = phoneUtil.parse(value);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
value = '+30' + origValue;
|
|
77
|
+
phone = phoneUtil.parse(value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return phoneUtil.isValidNumber(phone);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.error(err);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
165
85
|
}
|
|
166
86
|
|
|
167
87
|
export const MOBILE_PHONE_VALIDATOR = {
|
|
@@ -176,7 +96,7 @@ export const MOBILE_PHONE_VALIDATOR = {
|
|
|
176
96
|
};
|
|
177
97
|
|
|
178
98
|
export const PHONE_NUMBER_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
|
|
179
|
-
const
|
|
99
|
+
const countryCode = field?.extra?.countries;
|
|
180
100
|
const typeOfPhone: PhoneNumberType | null = field?.extra?.phoneType;
|
|
181
101
|
return {
|
|
182
102
|
name: 'phone-number-validator',
|
|
@@ -194,7 +114,7 @@ export const PHONE_NUMBER_VALIDATOR = (field: FieldSpec): ValidatorSchema => {
|
|
|
194
114
|
if (!value) {
|
|
195
115
|
return true;
|
|
196
116
|
}
|
|
197
|
-
return validatePhoneNumber(value,
|
|
117
|
+
return validatePhoneNumber(value, countryCode, typeOfPhone);
|
|
198
118
|
},
|
|
199
119
|
};
|
|
200
120
|
};
|
|
@@ -1,102 +1,76 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var expanded = {};
|
|
9
|
-
for (var _i = 0, _countries = countries; _i < _countries.length; _i++) {
|
|
10
|
-
var country = _countries[_i];
|
|
11
|
-
expanded[country] = {
|
|
12
|
-
fixedLine: {
|
|
13
|
-
possibleLengths: {
|
|
14
|
-
_national: compressed[country][3].map(String)
|
|
15
|
-
},
|
|
16
|
-
nationalNumberPattern: compressed[country][11][0][0]
|
|
17
|
-
},
|
|
18
|
-
mobile: {
|
|
19
|
-
nationalNumberPattern: compressed[country][11][1][0]
|
|
20
|
-
},
|
|
21
|
-
tollFree: {
|
|
22
|
-
nationalNumberPattern: compressed[country][11][2][0]
|
|
23
|
-
},
|
|
24
|
-
premiumRate: {
|
|
25
|
-
nationalNumberPattern: compressed[country][11][3][0]
|
|
26
|
-
},
|
|
27
|
-
sharedCost: {
|
|
28
|
-
nationalNumberPattern: compressed[country][11][9][0]
|
|
29
|
-
},
|
|
30
|
-
personalNumber: {
|
|
31
|
-
nationalNumberPattern: compressed[country][11][4][0]
|
|
32
|
-
},
|
|
33
|
-
uan: {
|
|
34
|
-
nationalNumberPattern: compressed[country][11][6][0]
|
|
35
|
-
},
|
|
36
|
-
id: country,
|
|
37
|
-
countryCode: compressed[country][0],
|
|
38
|
-
internationalPrefix: compressed[country][1]
|
|
39
|
-
};
|
|
1
|
+
import * as gPhoneNumber from 'google-libphonenumber';
|
|
2
|
+
export function validatePhoneNumber(phoneNumber) {
|
|
3
|
+
var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
|
|
4
|
+
var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
|
|
5
|
+
var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
6
|
+
if (!countries || countries.length === 0) {
|
|
7
|
+
return true;
|
|
40
8
|
}
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return
|
|
9
|
+
return countries.some(function (country) {
|
|
10
|
+
try {
|
|
11
|
+
var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
|
|
12
|
+
if (phoneUtil.isValidNumber(phone)) {
|
|
13
|
+
if (typeOfPhoneNumber) {
|
|
14
|
+
if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
|
|
15
|
+
return true;
|
|
16
|
+
} else {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error(error);
|
|
26
|
+
return false;
|
|
59
27
|
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// If no category matches, return 'Unknown'
|
|
63
|
-
return "unknown";
|
|
64
|
-
}
|
|
65
|
-
function getNumberType(phoneNumber, country) {
|
|
66
|
-
var spec = PHONENUMBER_SPEC[country.toUpperCase()];
|
|
67
|
-
if (!spec) {
|
|
68
|
-
throw new Error("Country ".concat(country, " is not supported"));
|
|
69
|
-
}
|
|
70
|
-
var type = discoverPhoneType(phoneNumber, spec);
|
|
71
|
-
return type;
|
|
28
|
+
});
|
|
72
29
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
30
|
+
var phoneNumberTypes = {
|
|
31
|
+
0: 'landline',
|
|
32
|
+
1: 'mobile',
|
|
33
|
+
2: 'landline_or_mobile'
|
|
34
|
+
};
|
|
35
|
+
export function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
|
|
36
|
+
try {
|
|
37
|
+
var phoneNumberType = phoneUtil.getNumberType(phone);
|
|
38
|
+
var numberType = phoneNumberTypes[phoneNumberType];
|
|
39
|
+
if (numberType === 'landline_or_mobile' || numberType === type) {
|
|
78
40
|
return true;
|
|
41
|
+
} else {
|
|
42
|
+
return false;
|
|
79
43
|
}
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error(error);
|
|
80
46
|
return false;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
function isNumberOfType(phoneNumber, country, type) {
|
|
84
|
-
var numberType = getNumberType(phoneNumber, country);
|
|
85
|
-
if (numberType === type) {
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
export function validatePhoneNumber(phoneNumber) {
|
|
91
|
-
var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
|
|
92
|
-
var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
|
|
93
|
-
if (!countries || countries.length === 0) {
|
|
94
|
-
return true;
|
|
95
47
|
}
|
|
96
|
-
return isPhoneNumberValid(phoneNumber, countries, typeOfPhoneNumber ? [typeOfPhoneNumber] : undefined);
|
|
97
48
|
}
|
|
98
49
|
function validateMobile(value) {
|
|
99
|
-
|
|
50
|
+
var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
|
|
51
|
+
var origValue = value;
|
|
52
|
+
// probably catch all the cases with a regex instead of gphonenumber
|
|
53
|
+
if (!value.match(/^((\+|00){0,1}\d{1,3}[- ]?)?\d{10}$/)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
var phone;
|
|
58
|
+
try {
|
|
59
|
+
phone = phoneUtil.parse(value);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
try {
|
|
62
|
+
value = '+' + value;
|
|
63
|
+
phone = phoneUtil.parse(value);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
value = '+30' + origValue;
|
|
66
|
+
phone = phoneUtil.parse(value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return phoneUtil.isValidNumber(phone);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error(err);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
100
74
|
}
|
|
101
75
|
export var MOBILE_PHONE_VALIDATOR = {
|
|
102
76
|
name: 'mobile-phone-validator',
|
|
@@ -110,7 +84,7 @@ export var MOBILE_PHONE_VALIDATOR = {
|
|
|
110
84
|
};
|
|
111
85
|
export var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
|
|
112
86
|
var _field$extra, _field$extra2;
|
|
113
|
-
var
|
|
87
|
+
var countryCode = field === null || field === void 0 ? void 0 : (_field$extra = field.extra) === null || _field$extra === void 0 ? void 0 : _field$extra.countries;
|
|
114
88
|
var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.phoneType;
|
|
115
89
|
return {
|
|
116
90
|
name: 'phone-number-validator',
|
|
@@ -128,7 +102,7 @@ export var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
|
|
|
128
102
|
if (!value) {
|
|
129
103
|
return true;
|
|
130
104
|
}
|
|
131
|
-
return validatePhoneNumber(value,
|
|
105
|
+
return validatePhoneNumber(value, countryCode, typeOfPhone);
|
|
132
106
|
}
|
|
133
107
|
};
|
|
134
108
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { FieldSpec } from '@digigov/form/types';
|
|
2
2
|
import { ValidatorSchema } from '@digigov/form/validators/types';
|
|
3
3
|
export type PhoneNumberType = 'landline' | 'mobile' | null;
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
4
|
+
export declare function validatePhoneNumber(phoneNumber: string, countries: string[] | undefined, typeOfPhoneNumber: PhoneNumberType): boolean;
|
|
5
|
+
export declare function matchTypeOfPhoneNumber(phone: any, type: string, phoneUtil: any): boolean;
|
|
6
6
|
export declare const MOBILE_PHONE_VALIDATOR: {
|
|
7
7
|
name: string;
|
|
8
8
|
message: string;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = exports.LandlineNumber = void 0;
|
|
9
|
-
var _react = _interopRequireDefault(require("react"));
|
|
10
|
-
var _form = _interopRequireWildcard(require("@digigov/form"));
|
|
11
|
-
var _Button = require("@digigov/ui/cjs/form/Button");
|
|
12
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
13
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
14
|
-
var _ref = /*#__PURE__*/_react["default"].createElement(_Button.Button, {
|
|
15
|
-
type: "submit"
|
|
16
|
-
}, "\u03A3\u03C5\u03BD\u03AD\u03C7\u03B5\u03B9\u03B1");
|
|
17
|
-
var LandlineNumber = exports.LandlineNumber = function LandlineNumber() {
|
|
18
|
-
return /*#__PURE__*/_react["default"].createElement(_form["default"], {
|
|
19
|
-
onSubmit: function onSubmit(data) {
|
|
20
|
-
console.log(data);
|
|
21
|
-
}
|
|
22
|
-
}, /*#__PURE__*/_react["default"].createElement(_form.Field, {
|
|
23
|
-
key: "phone-number",
|
|
24
|
-
name: "phone-number",
|
|
25
|
-
type: "phone_number",
|
|
26
|
-
label: {
|
|
27
|
-
primary: 'Σταθερό τηλέφωνο'
|
|
28
|
-
},
|
|
29
|
-
extra: {
|
|
30
|
-
countries: ['gr'],
|
|
31
|
-
phoneType: 'landline'
|
|
32
|
-
},
|
|
33
|
-
required: true
|
|
34
|
-
}), _ref);
|
|
35
|
-
};
|
|
36
|
-
var _default = exports["default"] = LandlineNumber;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import FormBuilder, { Field } from '@digigov/form';
|
|
3
|
-
import { Button } from '@digigov/ui/form/Button';
|
|
4
|
-
var _ref = /*#__PURE__*/React.createElement(Button, {
|
|
5
|
-
type: "submit"
|
|
6
|
-
}, "\u03A3\u03C5\u03BD\u03AD\u03C7\u03B5\u03B9\u03B1");
|
|
7
|
-
export var LandlineNumber = function LandlineNumber() {
|
|
8
|
-
return /*#__PURE__*/React.createElement(FormBuilder, {
|
|
9
|
-
onSubmit: function onSubmit(data) {
|
|
10
|
-
console.log(data);
|
|
11
|
-
}
|
|
12
|
-
}, /*#__PURE__*/React.createElement(Field, {
|
|
13
|
-
key: "phone-number",
|
|
14
|
-
name: "phone-number",
|
|
15
|
-
type: "phone_number",
|
|
16
|
-
label: {
|
|
17
|
-
primary: 'Σταθερό τηλέφωνο'
|
|
18
|
-
},
|
|
19
|
-
extra: {
|
|
20
|
-
countries: ['gr'],
|
|
21
|
-
phoneType: 'landline'
|
|
22
|
-
},
|
|
23
|
-
required: true
|
|
24
|
-
}), _ref);
|
|
25
|
-
};
|
|
26
|
-
export default LandlineNumber;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import FormBuilder, { Field } from '@digigov/form';
|
|
3
|
-
import { Button } from '@digigov/ui/form/Button';
|
|
4
|
-
|
|
5
|
-
export const LandlineNumber = () => (
|
|
6
|
-
<FormBuilder
|
|
7
|
-
onSubmit={(data) => {
|
|
8
|
-
console.log(data);
|
|
9
|
-
}}
|
|
10
|
-
>
|
|
11
|
-
<Field
|
|
12
|
-
key="phone-number"
|
|
13
|
-
name="phone-number"
|
|
14
|
-
type="phone_number"
|
|
15
|
-
label={{
|
|
16
|
-
primary: 'Σταθερό τηλέφωνο',
|
|
17
|
-
}}
|
|
18
|
-
extra={{
|
|
19
|
-
countries: ['gr'],
|
|
20
|
-
phoneType:'landline',
|
|
21
|
-
}}
|
|
22
|
-
required
|
|
23
|
-
/>
|
|
24
|
-
<Button type="submit">Συνέχεια</Button>
|
|
25
|
-
</FormBuilder>
|
|
26
|
-
);
|
|
27
|
-
export default LandlineNumber;
|