From 010e01a5540da127daeecce45d65bc44c6d41ccb Mon Sep 17 00:00:00 2001 From: Jorge Osorio Date: Wed, 23 Oct 2024 11:41:16 +0200 Subject: [PATCH 1/2] feat: Add contact phone number --- schema-definitions/other.json | 8 +++++++- src/other.ts | 1 + src/tests/Others.test.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/tests/Others.test.ts diff --git a/schema-definitions/other.json b/schema-definitions/other.json index dacb3c5..0ba8d92 100644 --- a/schema-definitions/other.json +++ b/schema-definitions/other.json @@ -7,6 +7,11 @@ "enableTokenToggleRestrictions": { "type": "boolean" }, + "contactPhoneNumber": { + "type": "string", + "pattern": "^\\+\\d{1,3}\\d{1,14}$", + "default": "" + }, "enableNynorsk": { "type": "boolean" }, @@ -193,7 +198,8 @@ } }, "required": [ - "vatPercent" + "vatPercent", + "contactPhoneNumber" ], "additionalProperties": false, "$schema": "http://json-schema.org/draft-07/schema#" diff --git a/src/other.ts b/src/other.ts index a91d4cd..d61c1f0 100644 --- a/src/other.ts +++ b/src/other.ts @@ -7,6 +7,7 @@ export const LoginMethod = z.enum(['otp', 'email', 'vipps']); export const Other = z.object({ vatPercent: z.number(), enableTokenToggleRestrictions: z.boolean().optional(), + contactPhoneNumber: z.string().regex(/^\+\d{1,3}\d{1,14}$/).default(""), enableNynorsk: z.boolean().optional(), tokenToggleMaxLimit: z.number().optional(), travelcardNumberLength: z.number().default(16), diff --git a/src/tests/Others.test.ts b/src/tests/Others.test.ts new file mode 100644 index 0000000..65af5a4 --- /dev/null +++ b/src/tests/Others.test.ts @@ -0,0 +1,32 @@ +import {assertType, expect, test} from 'vitest'; + +import {Other} from '../other'; + +test('Phone number', () => { + expect( + () => + Other.parse({ + contactPhoneNumber: 'foo', + vatPercent: 0, + }), + 'Phone number must be valid', + ).toThrowError(); + + expect( + () => + Other.parse({ + vatPercent: 1, + contactPhoneNumber: '+12345678901234567890', + }), + 'Phone must be at most 17 digits', + ).toThrowError(); + + expect( + () => + Other.parse({ + vatPercent: 1, + contactPhoneNumber: '+4712345678', + }), + 'Phone must be at least +(3 digits)', + ).not.toThrowError(); +}); From 989e6e93fc971de027bce34d42830e337833ac3f Mon Sep 17 00:00:00 2001 From: Jorge Osorio Date: Wed, 23 Oct 2024 11:45:39 +0200 Subject: [PATCH 2/2] refactor: make optional --- schema-definitions/other.json | 6 ++---- src/other.ts | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/schema-definitions/other.json b/schema-definitions/other.json index 0ba8d92..55f170b 100644 --- a/schema-definitions/other.json +++ b/schema-definitions/other.json @@ -9,8 +9,7 @@ }, "contactPhoneNumber": { "type": "string", - "pattern": "^\\+\\d{1,3}\\d{1,14}$", - "default": "" + "pattern": "^\\+\\d{1,3}\\d{1,14}$" }, "enableNynorsk": { "type": "boolean" @@ -198,8 +197,7 @@ } }, "required": [ - "vatPercent", - "contactPhoneNumber" + "vatPercent" ], "additionalProperties": false, "$schema": "http://json-schema.org/draft-07/schema#" diff --git a/src/other.ts b/src/other.ts index d61c1f0..6193ac1 100644 --- a/src/other.ts +++ b/src/other.ts @@ -7,7 +7,7 @@ export const LoginMethod = z.enum(['otp', 'email', 'vipps']); export const Other = z.object({ vatPercent: z.number(), enableTokenToggleRestrictions: z.boolean().optional(), - contactPhoneNumber: z.string().regex(/^\+\d{1,3}\d{1,14}$/).default(""), + contactPhoneNumber: z.string().regex(/^\+\d{1,3}\d{1,14}$/).optional(), enableNynorsk: z.boolean().optional(), tokenToggleMaxLimit: z.number().optional(), travelcardNumberLength: z.number().default(16),