From 96629f5020f90af6efd436f920751dfb4446fc84 Mon Sep 17 00:00:00 2001 From: Alexi Rahman Date: Mon, 29 Jul 2024 15:54:03 +0200 Subject: [PATCH] Update streetNumber check to allow only numbers (also negative) and only letters --- lib/validation-helpers/schemas.js | 8 ++++- .../validation-helpers/schemas-test-data.js | 34 +++++++++++++++++++ test/unit/validation-helpers/schemas-test.js | 13 ------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/validation-helpers/schemas.js b/lib/validation-helpers/schemas.js index 545e5ef..1f6af5c 100644 --- a/lib/validation-helpers/schemas.js +++ b/lib/validation-helpers/schemas.js @@ -61,7 +61,13 @@ const addressSchema = joi }) .keys({ streetName: joi.string().required().max(50), - streetNumber: joi.string().pattern(new RegExp("^[0-9]+$")).optional().allow(null).max(50).messages({ "string.pattern.base": '"streetNumber" length must be less than or equal to 50 characters long' }), + streetNumber: joi + .string() + .pattern(new RegExp("(^[\\-\\d]+$)|(^[a-öA-Ö]+$)")) + .optional() + .allow(null) + .max(50) + .messages({ "string.pattern.base": '"streetNumber" length must be less than or equal to 50 characters long' }), stairCase: joi.string().optional().allow(null).max(50), stairs: joi.string().optional().allow(null).max(50), apartmentNumber: joi.string().optional().allow(null).max(50), diff --git a/test-data/unit/validation-helpers/schemas-test-data.js b/test-data/unit/validation-helpers/schemas-test-data.js index eaec071..49d3860 100644 --- a/test-data/unit/validation-helpers/schemas-test-data.js +++ b/test-data/unit/validation-helpers/schemas-test-data.js @@ -114,6 +114,40 @@ const addressScenarios = [ country: "SE", }, }, + { + text: "Valid address with negative streetNumber", + expected: true, + address: { + streetName: "Testgatan", + streetNumber: "-39", + zipCode: "12345", + city: "Teststaden", + country: "SE", + }, + }, + { + text: "Valid address with only letters as streetNumber", + expected: true, + address: { + streetName: "Testgatan", + streetNumber: "ABC", + zipCode: "12345", + city: "Teststaden", + country: "SE", + }, + }, + { + text: "Invalid address due to bad streetNumber 1A", + expected: false, + error: '"streetNumber" length must be less than or equal to 50 characters long', + address: { + streetName: "Testgatan", + streetNumber: "1A", + zipCode: "12345", + city: "Teststaden", + country: "SE", + }, + }, { text: "Invalid address default country address, bad zipCode", expected: false, diff --git a/test/unit/validation-helpers/schemas-test.js b/test/unit/validation-helpers/schemas-test.js index 5c9bdda..ed98678 100644 --- a/test/unit/validation-helpers/schemas-test.js +++ b/test/unit/validation-helpers/schemas-test.js @@ -31,19 +31,6 @@ describe("check if address is correct", () => { } }); -describe("check if address is NOT correct", () => { - it("should invalidate address with a letter in streetNumber", () => { - const notValidAddress = addressSchema.validate({ - streetName: "Testgatan", - streetNumber: "1A", - zipCode: "12345", - city: "Teststaden", - }); - - notValidAddress.error.details[0].message.should.eql('"streetNumber" length must be less than or equal to 50 characters long'); - }); -}); - describe("check if distributionFee is correct", () => { for (const s of distributionFeeScenarios) { describe(`${s.text || s.nandText} with nand schema`, () => {