Skip to content

Commit

Permalink
Update streetNumber check to allow only numbers (also negative) and o…
Browse files Browse the repository at this point in the history
…nly letters
  • Loading branch information
odynvolk committed Jul 29, 2024
1 parent d5e256b commit 96629f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/validation-helpers/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
34 changes: 34 additions & 0 deletions test-data/unit/validation-helpers/schemas-test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 0 additions & 13 deletions test/unit/validation-helpers/schemas-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down

0 comments on commit 96629f5

Please sign in to comment.