From dcc9247c8efc74b48c26886fb054e21f29a90158 Mon Sep 17 00:00:00 2001 From: dylan96dashintha Date: Sat, 7 Mar 2020 12:17:28 +0530 Subject: [PATCH 1/2] validation --- src/models/schema/attribute.js | 11 +++++- src/models/schema/item.js | 6 ++++ src/models/schema/request.js | 32 +++++++++++++++++- src/models/schema/request_item.js | 56 +++++++++++++++++++++++++++++-- 4 files changed, 100 insertions(+), 5 deletions(-) diff --git a/src/models/schema/attribute.js b/src/models/schema/attribute.js index 8f285d2..a97f970 100644 --- a/src/models/schema/attribute.js +++ b/src/models/schema/attribute.js @@ -10,7 +10,16 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, }, // This must be provided if editable is false - defaultValue: { type: DataTypes.TEXT }, + defaultValue: { + type: DataTypes.TEXT , + validation: { + defaultValueValidation: function(editable){ + if(editable === false && defaultValue !== null) throw new Error("defaultValue validation Error") + } + } + + + }, editable: { type: DataTypes.BOOLEAN, defaultValue: false, diff --git a/src/models/schema/item.js b/src/models/schema/item.js index 0d0b3f3..014c636 100644 --- a/src/models/schema/item.js +++ b/src/models/schema/item.js @@ -19,6 +19,11 @@ module.exports = (sequelize, DataTypes) => { labId: { type: DataTypes.UUID, references: { model: 'Lab', key: 'id' }, + Validation: { + labIdValidation: function(isDraft) { + if (isDraft === false && labId !== null) throw new Error ("LabId validation failed") + } + } }, itemSetId: { type: DataTypes.UUID, @@ -31,3 +36,4 @@ module.exports = (sequelize, DataTypes) => { }; // TODO: (Validation): Lab id can be null iff isDraft is true + diff --git a/src/models/schema/request.js b/src/models/schema/request.js index ad24ac2..78f9311 100644 --- a/src/models/schema/request.js +++ b/src/models/schema/request.js @@ -20,10 +20,40 @@ module.exports = (sequelize, DataTypes) => { references: { model: 'Supervisor', key: 'id' }, }, reason: { type: DataTypes.TEXT }, - declineReason: { type: DataTypes.TEXT }, + declineReason: { + type: DataTypes.TEXT, + Validation: { + declineReasonValidation: function(status){ + let nNullList = ['DECLINED', 'INVALIDATED'] + let nullList = ['REQUESTED', 'ACCEPTED'] + + nNullList.forEach(function(item, index, array) { + if(status.type === item && declineReason === null) throw new Error("Decline Reason Validation Error!") + }) + + nullList.forEach(function(item, index, array) { + if(status.type === item && returnedDate !== null) throw new Error("Decline Reason Validation Error!") + }) + } + } + }, supervisorToken: { type: DataTypes.UUID, unique: 'supervisor_token', + Validation: { + supervisorTokenValidation: function(status){ + let nNullList = ['REQUESTED','ACCEPTED'] + let nullList = [ 'DECLINED', 'INVALIDATED'] + + nNullList.forEach(function(item, index, array) { + if(status.type === item && supervisorToken === null) throw new Error("Supervisor Token Validation Error!") + }) + + nullList.forEach(function(item, index, array) { + if(status.type === item && supervisorToken !== null) throw new Error("Supervisor Token Validation Error!") + }) + } + } }, status: { type: DataTypes.ENUM('REQUESTED', 'ACCEPTED', 'DECLINED', 'INVALIDATED'), diff --git a/src/models/schema/request_item.js b/src/models/schema/request_item.js index 2dc0188..47c640e 100644 --- a/src/models/schema/request_item.js +++ b/src/models/schema/request_item.js @@ -10,9 +10,59 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, references: { model: 'Request', key: 'id' }, }, - returnedDate: { type: DataTypes.DATE }, - dueDate: { type: DataTypes.DATE }, - borrowedDate: { type: DataTypes.DATE }, + returnedDate: { + type: DataTypes.DATE, + Validation:{ + returnedDateValidation: function(status) { + let nNullList = ['PENDING', 'BORROWED', 'REJECTED'] + let nullList = ['RETURNED','EXPIRED' ] + nNullList.forEach(function(item, index, array) { + if(status.type === item && returnedDate !== null) throw new Error("Returned Date Validation Error!") + }) + + nullList.forEach(function(item, index, array) { + if(status.type === item && returnedDate === null) throw new Error("Returned Date Validation Error!") + }) + + } + } + }, + dueDate: { + type: DataTypes.DATE, + Validation:{ + dueDateValidation: function(status) { + + let nNullList = ['PENDING', 'REJECTED'] + let nullList = ['RETURNED','BORROWED','EXPIRED' ] + nNullList.forEach(function(item, index, array) { + if(status.type === item && dueDate !== null) throw new Error("Due Date Validation Error!") + }) + + nullList.forEach(function(item, index, array) { + if(status.type === item && dueDate === null) throw new Error("Due Date Validation Error!") + }) + + } + } + }, + borrowedDate: { + type: DataTypes.DATE, + Validation:{ + borrowedDateValidation: function(status) { + + let nNullList = ['PENDING', 'REJECTED'] + let nullList = ['RETURNED','BORROWED','EXPIRED' ] + nNullList.forEach(function(item, index, array) { + if(status.type === item && borrowedDate !== null) throw new Error("Borrowed Date Validation Error!") + }) + + nullList.forEach(function(item, index, array) { + if(status.type === item && borrowedDate === null) throw new Error("Borrowed Date Validation Error!") + }) + + } + } + }, status: { type: DataTypes.ENUM('PENDING', 'BORROWED', 'RETURNED', 'REJECTED', 'EXPIRED'), defaultValue: 'PENDING', From d042490cf3f2f13fc074a4d408b77d006b8d0781 Mon Sep 17 00:00:00 2001 From: dylan96dashintha Date: Sat, 7 Mar 2020 12:56:30 +0530 Subject: [PATCH 2/2] validations with eslint --- package-lock.json | 18 +++---- package.json | 2 +- src/models/schema/attribute.js | 16 +++--- src/models/schema/item.js | 9 ++-- src/models/schema/request.js | 46 ++++++++-------- src/models/schema/request_item.js | 89 +++++++++++++++---------------- 6 files changed, 87 insertions(+), 93 deletions(-) diff --git a/package-lock.json b/package-lock.json index a025f74..377e71c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -264,11 +264,11 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "bcrypt": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-4.0.1.tgz", - "integrity": "sha512-hSIZHkUxIDS5zA2o00Kf2O5RfVbQ888n54xQoF/eIaquU4uaLxK8vhhBdktd0B3n2MjkcAWzv4mnhogykBKOUQ==", + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-3.0.8.tgz", + "integrity": "sha512-jKV6RvLhI36TQnPDvUFqBEnGX9c8dRRygKxCZu7E+MgLfKZbmmXL8a7/SFFOyHoPNX9nV81cKRC5tbQfvEQtpw==", "requires": { - "node-addon-api": "^2.0.0", + "nan": "2.14.0", "node-pre-gyp": "0.14.0" } }, @@ -2044,6 +2044,11 @@ "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", "dev": true }, + "nan": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" + }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -2086,11 +2091,6 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, - "node-addon-api": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz", - "integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==" - }, "node-pre-gyp": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz", diff --git a/package.json b/package.json index d818cae..cbac758 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "license": "MIT", "dependencies": { "@hapi/joi": "^17.1.0", - "bcrypt": "^4.0.1", + "bcrypt": "^3.0.8", "body-parser": "^1.19.0", "dotenv": "^8.2.0", "express": "^4.17.1", diff --git a/src/models/schema/attribute.js b/src/models/schema/attribute.js index a97f970..6b37d3e 100644 --- a/src/models/schema/attribute.js +++ b/src/models/schema/attribute.js @@ -10,15 +10,15 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, }, // This must be provided if editable is false - defaultValue: { - type: DataTypes.TEXT , + defaultValue: { + type: DataTypes.TEXT, validation: { - defaultValueValidation: function(editable){ - if(editable === false && defaultValue !== null) throw new Error("defaultValue validation Error") - } - } - - + defaultValueValidation(editable) { + if (editable === false && this.defaultValue !== null) throw new Error('defaultValue validation Error'); + }, + }, + + }, editable: { type: DataTypes.BOOLEAN, diff --git a/src/models/schema/item.js b/src/models/schema/item.js index 014c636..8053f35 100644 --- a/src/models/schema/item.js +++ b/src/models/schema/item.js @@ -20,10 +20,10 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.UUID, references: { model: 'Lab', key: 'id' }, Validation: { - labIdValidation: function(isDraft) { - if (isDraft === false && labId !== null) throw new Error ("LabId validation failed") - } - } + labIdValidation(isDraft) { + if (isDraft === false && this.labId !== null) throw new Error('LabId validation failed'); + }, + }, }, itemSetId: { type: DataTypes.UUID, @@ -36,4 +36,3 @@ module.exports = (sequelize, DataTypes) => { }; // TODO: (Validation): Lab id can be null iff isDraft is true - diff --git a/src/models/schema/request.js b/src/models/schema/request.js index 78f9311..57ef16d 100644 --- a/src/models/schema/request.js +++ b/src/models/schema/request.js @@ -21,39 +21,39 @@ module.exports = (sequelize, DataTypes) => { }, reason: { type: DataTypes.TEXT }, declineReason: { - type: DataTypes.TEXT, + type: DataTypes.TEXT, Validation: { - declineReasonValidation: function(status){ - let nNullList = ['DECLINED', 'INVALIDATED'] - let nullList = ['REQUESTED', 'ACCEPTED'] + declineReasonValidation(status) { + const nNullList = ['DECLINED', 'INVALIDATED']; + const nullList = ['REQUESTED', 'ACCEPTED']; - nNullList.forEach(function(item, index, array) { - if(status.type === item && declineReason === null) throw new Error("Decline Reason Validation Error!") - }) + nNullList.forEach((item) => { + if (status.type === item && this.declineReason === null) throw new Error('Decline Reason Validation Error!'); + }); - nullList.forEach(function(item, index, array) { - if(status.type === item && returnedDate !== null) throw new Error("Decline Reason Validation Error!") - }) - } - } + nullList.forEach((item) => { + if (status.type === item && this.returnedDate !== null) throw new Error('Decline Reason Validation Error!'); + }); + }, }, + }, supervisorToken: { type: DataTypes.UUID, unique: 'supervisor_token', Validation: { - supervisorTokenValidation: function(status){ - let nNullList = ['REQUESTED','ACCEPTED'] - let nullList = [ 'DECLINED', 'INVALIDATED'] + supervisorTokenValidation(status) { + const nNullList = ['REQUESTED', 'ACCEPTED']; + const nullList = ['DECLINED', 'INVALIDATED']; - nNullList.forEach(function(item, index, array) { - if(status.type === item && supervisorToken === null) throw new Error("Supervisor Token Validation Error!") - }) + nNullList.forEach((item) => { + if (status.type === item && this.supervisorToken === null) throw new Error('Supervisor Token Validation Error!'); + }); - nullList.forEach(function(item, index, array) { - if(status.type === item && supervisorToken !== null) throw new Error("Supervisor Token Validation Error!") - }) - } - } + nullList.forEach((item) => { + if (status.type === item && this.supervisorToken !== null) throw new Error('Supervisor Token Validation Error!'); + }); + }, + }, }, status: { type: DataTypes.ENUM('REQUESTED', 'ACCEPTED', 'DECLINED', 'INVALIDATED'), diff --git a/src/models/schema/request_item.js b/src/models/schema/request_item.js index 47c640e..7dc10e7 100644 --- a/src/models/schema/request_item.js +++ b/src/models/schema/request_item.js @@ -10,59 +10,54 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, references: { model: 'Request', key: 'id' }, }, - returnedDate: { + returnedDate: { type: DataTypes.DATE, - Validation:{ - returnedDateValidation: function(status) { - let nNullList = ['PENDING', 'BORROWED', 'REJECTED'] - let nullList = ['RETURNED','EXPIRED' ] - nNullList.forEach(function(item, index, array) { - if(status.type === item && returnedDate !== null) throw new Error("Returned Date Validation Error!") - }) + Validation: { + returnedDateValidation(status) { + const nNullList = ['PENDING', 'BORROWED', 'REJECTED']; + const nullList = ['RETURNED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.returnedDate !== null) throw new Error('Returned Date Validation Error!'); + }); - nullList.forEach(function(item, index, array) { - if(status.type === item && returnedDate === null) throw new Error("Returned Date Validation Error!") - }) - - } - } - }, - dueDate: { - type: DataTypes.DATE, - Validation:{ - dueDateValidation: function(status) { - - let nNullList = ['PENDING', 'REJECTED'] - let nullList = ['RETURNED','BORROWED','EXPIRED' ] - nNullList.forEach(function(item, index, array) { - if(status.type === item && dueDate !== null) throw new Error("Due Date Validation Error!") - }) + nullList.forEach((item) => { + if (status.type === item && this.returnedDate === null) throw new Error('Returned Date Validation Error!'); + }); + }, + }, + }, + dueDate: { + type: DataTypes.DATE, + Validation: { + dueDateValidation(status) { + const nNullList = ['PENDING', 'REJECTED']; + const nullList = ['RETURNED', 'BORROWED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.dueDate !== null) throw new Error('Due Date Validation Error!'); + }); - nullList.forEach(function(item, index, array) { - if(status.type === item && dueDate === null) throw new Error("Due Date Validation Error!") - }) - - } - } + nullList.forEach((item) => { + if (status.type === item && this.dueDate === null) throw new Error('Due Date Validation Error!'); + }); + }, + }, }, - borrowedDate: { + borrowedDate: { type: DataTypes.DATE, - Validation:{ - borrowedDateValidation: function(status) { - - let nNullList = ['PENDING', 'REJECTED'] - let nullList = ['RETURNED','BORROWED','EXPIRED' ] - nNullList.forEach(function(item, index, array) { - if(status.type === item && borrowedDate !== null) throw new Error("Borrowed Date Validation Error!") - }) + Validation: { + borrowedDateValidation(status) { + const nNullList = ['PENDING', 'REJECTED']; + const nullList = ['RETURNED', 'BORROWED', 'EXPIRED']; + nNullList.forEach((item) => { + if (status.type === item && this.borrowedDate !== null) throw new Error('Borrowed Date Validation Error!'); + }); - nullList.forEach(function(item, index, array) { - if(status.type === item && borrowedDate === null) throw new Error("Borrowed Date Validation Error!") - }) - - } - } - }, + nullList.forEach((item) => { + if (status.type === item && this.borrowedDate === null) throw new Error('Borrowed Date Validation Error!'); + }); + }, + }, + }, status: { type: DataTypes.ENUM('PENDING', 'BORROWED', 'RETURNED', 'REJECTED', 'EXPIRED'), defaultValue: 'PENDING',