Skip to content

Commit

Permalink
[CodeForPoznan#121] Added validation of postCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciek246 committed Feb 17, 2020
1 parent d07986f commit cac2bd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/db/models/school.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ const School = sequelize.define(
},
postCode: {
type: Sequelize.STRING(6),
allowNull: false
allowNull: false,
validate: {
isValidaPolishPostCode(value) {
const regex = /^[0-9]{2}-[0-9]{3}$/;
if (!regex.test(value)) throw new Error("Invalid polish postal code");
}
}
},
postOffice: {
type: Sequelize.STRING(80),
Expand Down
21 changes: 15 additions & 6 deletions src/test/models/school_model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { schoolData } = require("../factories/schoolFactory");
const { School } = require("../../db/models");

describe("School model", () => {
beforeAll(() => {
beforeEach(() => {
return sequelize
.sync({ force: true })
.then(() => schoolTypeFactory("mock"));
Expand All @@ -22,11 +22,20 @@ describe("School model", () => {
expect(school.name).toEqual("TEST");
});

// it("should validate `postCode`", () => {
// const school = await sequelize
// .sync({ force: true })
// .then(() => schoolFactory({ name: "TEST", type: "przedszkole" }));
// })
it("should validate `postCode`", async () => {
const school = await sequelize
.sync({ force: true })
.then(() =>
schoolFactory({
type: "mock",
postCode: "invalid code",
SchoolTypeName: "mock"
})
)
.catch(err => err.message);

expect(school).toEqual("Validation error: Invalid polish postal code");
});

describe("has virtual methods which", () => {
it("return concatenated address data by calling `address`", async () => {
Expand Down

0 comments on commit cac2bd3

Please sign in to comment.