@digigov/form 0.6.6 → 0.6.7

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/es/index.js +1 -1
  3. package/es/validators.js +88 -4
  4. package/es/validators.spec.js +41 -2
  5. package/esm/index.js +2 -2
  6. package/esm/validators.js +88 -4
  7. package/esm/validators.spec.js +41 -2
  8. package/index.js +1 -1
  9. package/libs/form/src/index.d.ts +1 -1
  10. package/libs/form/src/validators.d.ts +3 -0
  11. package/libs/ui/src/core/index.d.ts +0 -1
  12. package/libs/ui/src/locales/el.d.ts +2 -0
  13. package/package.json +2 -2
  14. package/validators.js +93 -4
  15. package/validators.spec.js +40 -1
  16. package/libs/ui/src/app/App.d.ts +0 -14
  17. package/libs/ui/src/app/CopyToClipboard.d.ts +0 -9
  18. package/libs/ui/src/app/Header/HeaderLogo.d.ts +0 -3
  19. package/libs/ui/src/app/Header/HeaderSection.d.ts +0 -5
  20. package/libs/ui/src/app/Header/HeaderTitle.d.ts +0 -5
  21. package/libs/ui/src/app/Header/index.d.ts +0 -11
  22. package/libs/ui/src/app/I18nText.d.ts +0 -10
  23. package/libs/ui/src/app/PhaseBannerHeader.d.ts +0 -8
  24. package/libs/ui/src/app/QrCodeScanner/index.d.ts +0 -29
  25. package/libs/ui/src/app/index.d.ts +0 -8
  26. package/libs/ui/src/core/PaginationLabel/index.d.ts +0 -21
  27. package/libs-ui/react-core/src/Header/index.d.ts +0 -10
  28. package/libs-ui/react-core/src/HeaderContent/index.d.ts +0 -9
  29. package/libs-ui/react-core/src/HeaderLogo/index.d.ts +0 -17
  30. package/libs-ui/react-core/src/HeaderSecondaryLogo/index.d.ts +0 -17
  31. package/libs-ui/react-core/src/HeaderSection/index.d.ts +0 -9
  32. package/libs-ui/react-core/src/HeaderSubtitle/index.d.ts +0 -9
  33. package/libs-ui/react-core/src/HeaderTitle/index.d.ts +0 -13
  34. package/libs-ui/react-core/src/PhaseBannerHeaderContainer/index.d.ts +0 -8
  35. package/libs-ui/react-extensions/src/admin/CopyToClipboardContainer/index.d.ts +0 -14
  36. package/libs-ui/react-extensions/src/admin/CopyToClipboardMessage/index.d.ts +0 -21
  37. package/libs-ui/react-extensions/src/admin/PaginationLabel/index.d.ts +0 -9
package/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Change Log - @digigov/form
2
2
 
3
- This log was last generated on Tue, 24 May 2022 09:13:18 GMT and should not be manually modified.
3
+ This log was last generated on Wed, 08 Jun 2022 12:26:07 GMT and should not be manually modified.
4
+
5
+ ## 0.6.7
6
+ Wed, 08 Jun 2022 12:26:07 GMT
7
+
8
+ ### Patches
9
+
10
+ - create phone_number validation rule which accepts country and type of phone
11
+ - Add missing dependency in handleSubmit
4
12
 
5
13
  ## 0.6.6
6
14
  Tue, 24 May 2022 09:13:18 GMT
package/es/index.js CHANGED
@@ -50,7 +50,7 @@ export var FormBase = /*#__PURE__*/React.forwardRef(function FormBase(_ref, ref)
50
50
  form.setError(key, errors[key]);
51
51
  }
52
52
  }
53
- }, []);
53
+ }, [onSubmit]);
54
54
  var ctx = {
55
55
  fieldsMap: fieldsMap,
56
56
  fieldsetsMap: fieldsetsMap,
package/es/validators.js CHANGED
@@ -51,6 +51,58 @@ export function validateAFM(afm) {
51
51
  var valid = calc % 10 === d9;
52
52
  return valid;
53
53
  }
54
+ export function validatePhoneNumber(phoneNumber) {
55
+ var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
56
+ var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
57
+ var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
58
+
59
+ if (!countries || countries.length === 0) {
60
+ return true;
61
+ }
62
+
63
+ return countries.some(function (country) {
64
+ try {
65
+ var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
66
+
67
+ if (phoneUtil.isValidNumber(phone)) {
68
+ if (typeOfPhoneNumber) {
69
+ if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
70
+ return true;
71
+ } else {
72
+ return false;
73
+ }
74
+ } else {
75
+ return true;
76
+ }
77
+ }
78
+
79
+ return false;
80
+ } catch (error) {
81
+ console.error(error);
82
+ return false;
83
+ }
84
+ });
85
+ }
86
+ var phoneNumberTypes = {
87
+ 0: 'landline',
88
+ 1: 'mobile',
89
+ 2: 'landline_or_mobile'
90
+ };
91
+ export function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
92
+ try {
93
+ var phoneNumberType = phoneUtil.getNumberType(phone);
94
+ var numberType = phoneNumberTypes[phoneNumberType];
95
+
96
+ if (numberType === 'landline_or_mobile' || numberType === type) {
97
+ return true;
98
+ } else {
99
+ return false;
100
+ }
101
+ } catch (error) {
102
+ console.error(error);
103
+ return false;
104
+ }
105
+ }
54
106
 
55
107
  function validateMobile(value) {
56
108
  var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
@@ -90,7 +142,7 @@ var POSTALCODE_VALIDATOR = function POSTALCODE_VALIDATOR(field) {
90
142
  name: 'postal-code-validator',
91
143
  message: 'form.error.postalCode',
92
144
  test: function test(value) {
93
- if (!value || value.length === 0) {
145
+ if (!value) {
94
146
  return true;
95
147
  }
96
148
 
@@ -110,6 +162,35 @@ var MOBILE_PHONE_VALIDATOR = {
110
162
  return true;
111
163
  }
112
164
  };
165
+
166
+ var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
167
+ var _field$extra2, _field$extra3;
168
+
169
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.countries;
170
+ var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra3 = field.extra) === null || _field$extra3 === void 0 ? void 0 : _field$extra3.phoneType;
171
+ return {
172
+ name: 'phone-number-validator',
173
+ message: function message() {
174
+ if (typeOfPhone === 'mobile') {
175
+ return 'form.error.mobile_phone';
176
+ }
177
+
178
+ if (typeOfPhone === 'landline') {
179
+ return 'form.error.landline';
180
+ } else {
181
+ return 'form.error.phone_number';
182
+ }
183
+ },
184
+ test: function test(value) {
185
+ if (!value) {
186
+ return true;
187
+ }
188
+
189
+ return validatePhoneNumber(value, countryCode, typeOfPhone);
190
+ }
191
+ };
192
+ };
193
+
113
194
  var AFM_VALIDATOR = {
114
195
  name: 'afm-validator',
115
196
  message: 'form.error.afm',
@@ -297,14 +378,14 @@ export function validateIban(value, countryCode) {
297
378
  }
298
379
 
299
380
  var IBAN_VALIDATOR = function IBAN_VALIDATOR(field) {
300
- var _field$extra2;
381
+ var _field$extra4;
301
382
 
302
- var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.country;
383
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra4 = field.extra) === null || _field$extra4 === void 0 ? void 0 : _field$extra4.country;
303
384
  return {
304
385
  name: 'iban-validator',
305
386
  message: 'form.error.iban',
306
387
  test: function test(value) {
307
- if (!value || value.length === 0) {
388
+ if (!value) {
308
389
  return true;
309
390
  }
310
391
 
@@ -454,6 +535,9 @@ var getYUPTypeMap = function getYUPTypeMap() {
454
535
  mobile_phone: function mobile_phone() {
455
536
  return yup.string().test(MOBILE_PHONE_VALIDATOR);
456
537
  },
538
+ phone_number: function phone_number(field) {
539
+ return yup.string().test(PHONE_NUMBER_VALIDATOR(field));
540
+ },
457
541
  'choice:multiple': function choiceMultiple() {
458
542
  return yup.array().of(yup.string()).nullable();
459
543
  },
@@ -1,4 +1,4 @@
1
- import { validateUUID4, validateIban, validatePostalCode } from '@digigov/form/validators';
1
+ import { validateUUID4, validateIban, validatePostalCode, validatePhoneNumber } from '@digigov/form/validators';
2
2
  it('validates wrong uuid4 for empty value', function () {
3
3
  expect(validateUUID4('')).toBe(false);
4
4
  });
@@ -41,6 +41,45 @@ it('validates wrong postal code with wrong country code', function () {
41
41
  it('validates wrong postal code with greek country code', function () {
42
42
  expect(validatePostalCode('123', ['GR'])).toBe(false);
43
43
  });
44
- it('validates postal code with greek country code', function () {
44
+ it('validates postal code with greek country code', function () {
45
45
  expect(validatePostalCode('11143', ['GR'])).toBe(true);
46
+ });
47
+ it('validates phone number type landline with greek country code', function () {
48
+ expect(validatePhoneNumber('2102934896', ['GR'], 'landline')).toBe(true);
49
+ });
50
+ it('validates phone number type landline with ch country code', function () {
51
+ expect(validatePhoneNumber('2102934896', ['CH'], 'landline')).toBe(false);
52
+ });
53
+ it('validates phone number type landline with ch and gr countries code', function () {
54
+ expect(validatePhoneNumber('2102934896', ['GR', 'CH'], 'landline')).toBe(true);
55
+ });
56
+ it('validatesphone number type landline with ch and gr countries code', function () {
57
+ expect(validatePhoneNumber('41446681800', ['GR', 'CH'], 'landline')).toBe(true);
58
+ });
59
+ it('validates phone number type landline with ch and gr countries code but phone number is mobile', function () {
60
+ expect(validatePhoneNumber('2102934896', ['GR', 'CH'], 'mobile')).toBe(false);
61
+ });
62
+ it('validates phone number type mobile with ch and gr countries code', function () {
63
+ expect(validatePhoneNumber('6934100982', ['GR', 'CH'], 'mobile')).toBe(true);
64
+ });
65
+ it('validates phone number type mobile with gr country code', function () {
66
+ expect(validatePhoneNumber('6934100982', ['GR'], 'mobile')).toBe(true);
67
+ });
68
+ it('validates phone number type mobile with ch country code', function () {
69
+ expect(validatePhoneNumber('6934100982', ['CH'], 'mobile')).toBe(false);
70
+ });
71
+ it('validates phone number with no type and ch country code', function () {
72
+ expect(validatePhoneNumber('6934100982', ['CH'], null)).toBe(false);
73
+ });
74
+ it('validates phone number with no type and gr country code', function () {
75
+ expect(validatePhoneNumber('6934100982', ['gr'], null)).toBe(true);
76
+ });
77
+ it('validates phone number with no type and ch country code', function () {
78
+ expect(validatePhoneNumber('41446681800', ['CH'], null)).toBe(true);
79
+ });
80
+ it('validates phone number with no type and gr country code', function () {
81
+ expect(validatePhoneNumber('41446681800', ['gr'], null)).toBe(false);
82
+ });
83
+ it('validates phone number with no type and gr and ch countries code', function () {
84
+ expect(validatePhoneNumber('41446681800', ['gr', 'ch'], null)).toBe(true);
46
85
  });
package/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Digigov v0.6.6
1
+ /** @license Digigov v0.6.7
2
2
  *
3
3
  * This source code is licensed under the BSD-2-Clause license found in the
4
4
  * LICENSE file in the root directory of this source tree.
@@ -55,7 +55,7 @@ export var FormBase = /*#__PURE__*/React.forwardRef(function FormBase(_ref, ref)
55
55
  form.setError(key, errors[key]);
56
56
  }
57
57
  }
58
- }, []);
58
+ }, [onSubmit]);
59
59
  var ctx = {
60
60
  fieldsMap: fieldsMap,
61
61
  fieldsetsMap: fieldsetsMap,
package/esm/validators.js CHANGED
@@ -51,6 +51,58 @@ export function validateAFM(afm) {
51
51
  var valid = calc % 10 === d9;
52
52
  return valid;
53
53
  }
54
+ export function validatePhoneNumber(phoneNumber) {
55
+ var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
56
+ var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
57
+ var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
58
+
59
+ if (!countries || countries.length === 0) {
60
+ return true;
61
+ }
62
+
63
+ return countries.some(function (country) {
64
+ try {
65
+ var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
66
+
67
+ if (phoneUtil.isValidNumber(phone)) {
68
+ if (typeOfPhoneNumber) {
69
+ if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
70
+ return true;
71
+ } else {
72
+ return false;
73
+ }
74
+ } else {
75
+ return true;
76
+ }
77
+ }
78
+
79
+ return false;
80
+ } catch (error) {
81
+ console.error(error);
82
+ return false;
83
+ }
84
+ });
85
+ }
86
+ var phoneNumberTypes = {
87
+ 0: 'landline',
88
+ 1: 'mobile',
89
+ 2: 'landline_or_mobile'
90
+ };
91
+ export function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
92
+ try {
93
+ var phoneNumberType = phoneUtil.getNumberType(phone);
94
+ var numberType = phoneNumberTypes[phoneNumberType];
95
+
96
+ if (numberType === 'landline_or_mobile' || numberType === type) {
97
+ return true;
98
+ } else {
99
+ return false;
100
+ }
101
+ } catch (error) {
102
+ console.error(error);
103
+ return false;
104
+ }
105
+ }
54
106
 
55
107
  function validateMobile(value) {
56
108
  var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
@@ -90,7 +142,7 @@ var POSTALCODE_VALIDATOR = function POSTALCODE_VALIDATOR(field) {
90
142
  name: 'postal-code-validator',
91
143
  message: 'form.error.postalCode',
92
144
  test: function test(value) {
93
- if (!value || value.length === 0) {
145
+ if (!value) {
94
146
  return true;
95
147
  }
96
148
 
@@ -110,6 +162,35 @@ var MOBILE_PHONE_VALIDATOR = {
110
162
  return true;
111
163
  }
112
164
  };
165
+
166
+ var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
167
+ var _field$extra2, _field$extra3;
168
+
169
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.countries;
170
+ var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra3 = field.extra) === null || _field$extra3 === void 0 ? void 0 : _field$extra3.phoneType;
171
+ return {
172
+ name: 'phone-number-validator',
173
+ message: function message() {
174
+ if (typeOfPhone === 'mobile') {
175
+ return 'form.error.mobile_phone';
176
+ }
177
+
178
+ if (typeOfPhone === 'landline') {
179
+ return 'form.error.landline';
180
+ } else {
181
+ return 'form.error.phone_number';
182
+ }
183
+ },
184
+ test: function test(value) {
185
+ if (!value) {
186
+ return true;
187
+ }
188
+
189
+ return validatePhoneNumber(value, countryCode, typeOfPhone);
190
+ }
191
+ };
192
+ };
193
+
113
194
  var AFM_VALIDATOR = {
114
195
  name: 'afm-validator',
115
196
  message: 'form.error.afm',
@@ -297,14 +378,14 @@ export function validateIban(value, countryCode) {
297
378
  }
298
379
 
299
380
  var IBAN_VALIDATOR = function IBAN_VALIDATOR(field) {
300
- var _field$extra2;
381
+ var _field$extra4;
301
382
 
302
- var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.country;
383
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra4 = field.extra) === null || _field$extra4 === void 0 ? void 0 : _field$extra4.country;
303
384
  return {
304
385
  name: 'iban-validator',
305
386
  message: 'form.error.iban',
306
387
  test: function test(value) {
307
- if (!value || value.length === 0) {
388
+ if (!value) {
308
389
  return true;
309
390
  }
310
391
 
@@ -454,6 +535,9 @@ var getYUPTypeMap = function getYUPTypeMap() {
454
535
  mobile_phone: function mobile_phone() {
455
536
  return yup.string().test(MOBILE_PHONE_VALIDATOR);
456
537
  },
538
+ phone_number: function phone_number(field) {
539
+ return yup.string().test(PHONE_NUMBER_VALIDATOR(field));
540
+ },
457
541
  'choice:multiple': function choiceMultiple() {
458
542
  return yup.array().of(yup.string()).nullable();
459
543
  },
@@ -1,4 +1,4 @@
1
- import { validateUUID4, validateIban, validatePostalCode } from '@digigov/form/validators';
1
+ import { validateUUID4, validateIban, validatePostalCode, validatePhoneNumber } from '@digigov/form/validators';
2
2
  it('validates wrong uuid4 for empty value', function () {
3
3
  expect(validateUUID4('')).toBe(false);
4
4
  });
@@ -41,6 +41,45 @@ it('validates wrong postal code with wrong country code', function () {
41
41
  it('validates wrong postal code with greek country code', function () {
42
42
  expect(validatePostalCode('123', ['GR'])).toBe(false);
43
43
  });
44
- it('validates postal code with greek country code', function () {
44
+ it('validates postal code with greek country code', function () {
45
45
  expect(validatePostalCode('11143', ['GR'])).toBe(true);
46
+ });
47
+ it('validates phone number type landline with greek country code', function () {
48
+ expect(validatePhoneNumber('2102934896', ['GR'], 'landline')).toBe(true);
49
+ });
50
+ it('validates phone number type landline with ch country code', function () {
51
+ expect(validatePhoneNumber('2102934896', ['CH'], 'landline')).toBe(false);
52
+ });
53
+ it('validates phone number type landline with ch and gr countries code', function () {
54
+ expect(validatePhoneNumber('2102934896', ['GR', 'CH'], 'landline')).toBe(true);
55
+ });
56
+ it('validatesphone number type landline with ch and gr countries code', function () {
57
+ expect(validatePhoneNumber('41446681800', ['GR', 'CH'], 'landline')).toBe(true);
58
+ });
59
+ it('validates phone number type landline with ch and gr countries code but phone number is mobile', function () {
60
+ expect(validatePhoneNumber('2102934896', ['GR', 'CH'], 'mobile')).toBe(false);
61
+ });
62
+ it('validates phone number type mobile with ch and gr countries code', function () {
63
+ expect(validatePhoneNumber('6934100982', ['GR', 'CH'], 'mobile')).toBe(true);
64
+ });
65
+ it('validates phone number type mobile with gr country code', function () {
66
+ expect(validatePhoneNumber('6934100982', ['GR'], 'mobile')).toBe(true);
67
+ });
68
+ it('validates phone number type mobile with ch country code', function () {
69
+ expect(validatePhoneNumber('6934100982', ['CH'], 'mobile')).toBe(false);
70
+ });
71
+ it('validates phone number with no type and ch country code', function () {
72
+ expect(validatePhoneNumber('6934100982', ['CH'], null)).toBe(false);
73
+ });
74
+ it('validates phone number with no type and gr country code', function () {
75
+ expect(validatePhoneNumber('6934100982', ['gr'], null)).toBe(true);
76
+ });
77
+ it('validates phone number with no type and ch country code', function () {
78
+ expect(validatePhoneNumber('41446681800', ['CH'], null)).toBe(true);
79
+ });
80
+ it('validates phone number with no type and gr country code', function () {
81
+ expect(validatePhoneNumber('41446681800', ['gr'], null)).toBe(false);
82
+ });
83
+ it('validates phone number with no type and gr and ch countries code', function () {
84
+ expect(validatePhoneNumber('41446681800', ['gr', 'ch'], null)).toBe(true);
46
85
  });
package/index.js CHANGED
@@ -102,7 +102,7 @@ var FormBase = /*#__PURE__*/_react["default"].forwardRef(function FormBase(_ref,
102
102
  form.setError(key, errors[key]);
103
103
  }
104
104
  }
105
- }, []);
105
+ }, [onSubmit]);
106
106
  var ctx = {
107
107
  fieldsMap: fieldsMap,
108
108
  fieldsetsMap: fieldsetsMap,
@@ -19,7 +19,7 @@ export interface FieldCondition {
19
19
  }
20
20
  export interface FieldSpec {
21
21
  key: string;
22
- type?: 'int' | 'string' | 'boolean' | 'choice:multiple' | 'choice:single' | 'mobile_phone' | 'afm' | 'iban' | 'postal_code';
22
+ type?: 'int' | 'string' | 'boolean' | 'choice:multiple' | 'choice:single' | 'mobile_phone' | 'afm' | 'iban' | 'postal_code' | 'phone_number';
23
23
  component?: any;
24
24
  condition?: Record<string, FieldCondition>;
25
25
  controlled?: boolean;
@@ -4,6 +4,9 @@ import { MutableRefObject } from 'react';
4
4
  export declare type ValidatorSchema = yup.BaseTestOptions;
5
5
  export declare function validatePostalCode(number: string, countries: Array<string>): boolean;
6
6
  export declare function validateAFM(afm: string): boolean;
7
+ export declare type PhoneNumberType = 'landline' | 'mobile' | null;
8
+ export declare function validatePhoneNumber(phoneNumber: string, countries: string[] | undefined, typeOfPhoneNumber: PhoneNumberType): boolean;
9
+ export declare function matchTypeOfPhoneNumber(phone: any, type: string, phoneUtil: any): boolean;
7
10
  export declare function validateUUID4(uuid4: string): boolean;
8
11
  export declare function validateIban(value: string, countryCode: string): boolean;
9
12
  export interface MutableRefObjectProps {
@@ -18,6 +18,5 @@ export * from '@digigov/ui/core/Table';
18
18
  export * from '@digigov/ui/core/Tabs';
19
19
  export * from '@digigov/ui/core/VisuallyHidden';
20
20
  export { default as WarningText } from '@digigov/ui/core/WarningText';
21
- export * from '@digigov/ui/core/PaginationLabel';
22
21
  export * from '@digigov/ui/core/SvgIcon';
23
22
  export * from '@digigov/ui/core/FileUpload';
@@ -14,6 +14,8 @@ declare const _default: {
14
14
  afm: string;
15
15
  file_size: string;
16
16
  mobile_phone: string;
17
+ phone_number: string;
18
+ landline: string;
17
19
  uuid4: string;
18
20
  iban: string;
19
21
  postalCode: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digigov/form",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "@digigov form builder",
5
5
  "author": "GRNET Developers <devs@lists.grnet.gr>",
6
6
  "license": "BSD-2-Clause",
@@ -17,7 +17,7 @@
17
17
  "dayjs": "1.10.4"
18
18
  },
19
19
  "peerDependencies": {
20
- "@digigov/ui": "0.20.2",
20
+ "@digigov/ui": "0.21.0",
21
21
  "@material-ui/core": "4.11.3",
22
22
  "@material-ui/icons": "4.11.2",
23
23
  "clsx": "1.1.1",
package/validators.js CHANGED
@@ -7,9 +7,11 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
+ exports.matchTypeOfPhoneNumber = matchTypeOfPhoneNumber;
10
11
  exports.useValidationSchema = useValidationSchema;
11
12
  exports.validateAFM = validateAFM;
12
13
  exports.validateIban = validateIban;
14
+ exports.validatePhoneNumber = validatePhoneNumber;
13
15
  exports.validatePostalCode = validatePostalCode;
14
16
  exports.validateUUID4 = validateUUID4;
15
17
 
@@ -80,6 +82,61 @@ function validateAFM(afm) {
80
82
  return valid;
81
83
  }
82
84
 
85
+ function validatePhoneNumber(phoneNumber) {
86
+ var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
87
+ var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
88
+ var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
89
+
90
+ if (!countries || countries.length === 0) {
91
+ return true;
92
+ }
93
+
94
+ return countries.some(function (country) {
95
+ try {
96
+ var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
97
+
98
+ if (phoneUtil.isValidNumber(phone)) {
99
+ if (typeOfPhoneNumber) {
100
+ if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
101
+ return true;
102
+ } else {
103
+ return false;
104
+ }
105
+ } else {
106
+ return true;
107
+ }
108
+ }
109
+
110
+ return false;
111
+ } catch (error) {
112
+ console.error(error);
113
+ return false;
114
+ }
115
+ });
116
+ }
117
+
118
+ var phoneNumberTypes = {
119
+ 0: 'landline',
120
+ 1: 'mobile',
121
+ 2: 'landline_or_mobile'
122
+ };
123
+
124
+ function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
125
+ try {
126
+ var phoneNumberType = phoneUtil.getNumberType(phone);
127
+ var numberType = phoneNumberTypes[phoneNumberType];
128
+
129
+ if (numberType === 'landline_or_mobile' || numberType === type) {
130
+ return true;
131
+ } else {
132
+ return false;
133
+ }
134
+ } catch (error) {
135
+ console.error(error);
136
+ return false;
137
+ }
138
+ }
139
+
83
140
  function validateMobile(value) {
84
141
  var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
85
142
  var origValue = value; // probably catch all the cases with a regex instead of gphonenumber
@@ -118,7 +175,7 @@ var POSTALCODE_VALIDATOR = function POSTALCODE_VALIDATOR(field) {
118
175
  name: 'postal-code-validator',
119
176
  message: 'form.error.postalCode',
120
177
  test: function test(value) {
121
- if (!value || value.length === 0) {
178
+ if (!value) {
122
179
  return true;
123
180
  }
124
181
 
@@ -138,6 +195,35 @@ var MOBILE_PHONE_VALIDATOR = {
138
195
  return true;
139
196
  }
140
197
  };
198
+
199
+ var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
200
+ var _field$extra2, _field$extra3;
201
+
202
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.countries;
203
+ var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra3 = field.extra) === null || _field$extra3 === void 0 ? void 0 : _field$extra3.phoneType;
204
+ return {
205
+ name: 'phone-number-validator',
206
+ message: function message() {
207
+ if (typeOfPhone === 'mobile') {
208
+ return 'form.error.mobile_phone';
209
+ }
210
+
211
+ if (typeOfPhone === 'landline') {
212
+ return 'form.error.landline';
213
+ } else {
214
+ return 'form.error.phone_number';
215
+ }
216
+ },
217
+ test: function test(value) {
218
+ if (!value) {
219
+ return true;
220
+ }
221
+
222
+ return validatePhoneNumber(value, countryCode, typeOfPhone);
223
+ }
224
+ };
225
+ };
226
+
141
227
  var AFM_VALIDATOR = {
142
228
  name: 'afm-validator',
143
229
  message: 'form.error.afm',
@@ -328,14 +414,14 @@ function validateIban(value, countryCode) {
328
414
  }
329
415
 
330
416
  var IBAN_VALIDATOR = function IBAN_VALIDATOR(field) {
331
- var _field$extra2;
417
+ var _field$extra4;
332
418
 
333
- var countryCode = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.country;
419
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra4 = field.extra) === null || _field$extra4 === void 0 ? void 0 : _field$extra4.country;
334
420
  return {
335
421
  name: 'iban-validator',
336
422
  message: 'form.error.iban',
337
423
  test: function test(value) {
338
- if (!value || value.length === 0) {
424
+ if (!value) {
339
425
  return true;
340
426
  }
341
427
 
@@ -485,6 +571,9 @@ var getYUPTypeMap = function getYUPTypeMap() {
485
571
  mobile_phone: function mobile_phone() {
486
572
  return yup.string().test(MOBILE_PHONE_VALIDATOR);
487
573
  },
574
+ phone_number: function phone_number(field) {
575
+ return yup.string().test(PHONE_NUMBER_VALIDATOR(field));
576
+ },
488
577
  'choice:multiple': function choiceMultiple() {
489
578
  return yup.array().of(yup.string()).nullable();
490
579
  },
@@ -44,6 +44,45 @@ it('validates wrong postal code with wrong country code', function () {
44
44
  it('validates wrong postal code with greek country code', function () {
45
45
  expect((0, _validators.validatePostalCode)('123', ['GR'])).toBe(false);
46
46
  });
47
- it('validates postal code with greek country code', function () {
47
+ it('validates postal code with greek country code', function () {
48
48
  expect((0, _validators.validatePostalCode)('11143', ['GR'])).toBe(true);
49
+ });
50
+ it('validates phone number type landline with greek country code', function () {
51
+ expect((0, _validators.validatePhoneNumber)('2102934896', ['GR'], 'landline')).toBe(true);
52
+ });
53
+ it('validates phone number type landline with ch country code', function () {
54
+ expect((0, _validators.validatePhoneNumber)('2102934896', ['CH'], 'landline')).toBe(false);
55
+ });
56
+ it('validates phone number type landline with ch and gr countries code', function () {
57
+ expect((0, _validators.validatePhoneNumber)('2102934896', ['GR', 'CH'], 'landline')).toBe(true);
58
+ });
59
+ it('validatesphone number type landline with ch and gr countries code', function () {
60
+ expect((0, _validators.validatePhoneNumber)('41446681800', ['GR', 'CH'], 'landline')).toBe(true);
61
+ });
62
+ it('validates phone number type landline with ch and gr countries code but phone number is mobile', function () {
63
+ expect((0, _validators.validatePhoneNumber)('2102934896', ['GR', 'CH'], 'mobile')).toBe(false);
64
+ });
65
+ it('validates phone number type mobile with ch and gr countries code', function () {
66
+ expect((0, _validators.validatePhoneNumber)('6934100982', ['GR', 'CH'], 'mobile')).toBe(true);
67
+ });
68
+ it('validates phone number type mobile with gr country code', function () {
69
+ expect((0, _validators.validatePhoneNumber)('6934100982', ['GR'], 'mobile')).toBe(true);
70
+ });
71
+ it('validates phone number type mobile with ch country code', function () {
72
+ expect((0, _validators.validatePhoneNumber)('6934100982', ['CH'], 'mobile')).toBe(false);
73
+ });
74
+ it('validates phone number with no type and ch country code', function () {
75
+ expect((0, _validators.validatePhoneNumber)('6934100982', ['CH'], null)).toBe(false);
76
+ });
77
+ it('validates phone number with no type and gr country code', function () {
78
+ expect((0, _validators.validatePhoneNumber)('6934100982', ['gr'], null)).toBe(true);
79
+ });
80
+ it('validates phone number with no type and ch country code', function () {
81
+ expect((0, _validators.validatePhoneNumber)('41446681800', ['CH'], null)).toBe(true);
82
+ });
83
+ it('validates phone number with no type and gr country code', function () {
84
+ expect((0, _validators.validatePhoneNumber)('41446681800', ['gr'], null)).toBe(false);
85
+ });
86
+ it('validates phone number with no type and gr and ch countries code', function () {
87
+ expect((0, _validators.validatePhoneNumber)('41446681800', ['gr', 'ch'], null)).toBe(true);
49
88
  });