From e1c4b0c23752b28da19cfa9afd4809a54f07d8a4 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Thu, 7 Sep 2023 12:07:34 +0200 Subject: [PATCH] use setters for data validation --- objects/Entity.mjs | 13 +++++++++---- objects/Publication.mjs | 11 +++++++---- objects/Story.mjs | 12 ++++++++---- objects/Submission.mjs | 8 ++++++++ objects/Title.mjs | 25 ++++++++++++++++--------- test/Entity.test.mjs | 2 +- test/Submission.test.mjs | 10 ++++++++++ 7 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 objects/Submission.mjs create mode 100644 test/Submission.test.mjs diff --git a/objects/Entity.mjs b/objects/Entity.mjs index c06ab25..7dadefd 100644 --- a/objects/Entity.mjs +++ b/objects/Entity.mjs @@ -1,11 +1,16 @@ import dv from "./dv.mjs" export default class Entity{ - constructor(data){ - if(data?.id){ - if(!Number.isInteger(data.id)){throw new TypeError("id must be an integer!")} - this.id = data.id + set _id(prop){ + if(prop){ + if(!Number.isInteger(prop)){throw new TypeError("id must be an integer!")} + this.id = prop } } + + constructor(data){ + this._id = data?.id + } + async insert(db){ return db(this.table) diff --git a/objects/Publication.mjs b/objects/Publication.mjs index 8c1113a..ffc6212 100644 --- a/objects/Publication.mjs +++ b/objects/Publication.mjs @@ -1,12 +1,15 @@ import Title from "./Title.mjs"; import dv from "./dv.mjs"; export default class Publication extends Title{ + set _link(prop){ + if(prop){ + if(!dv.isString(prop)){throw new TypeError("link must be a string")} + this.link=prop + } + } constructor(data){ super(data) - if(data?.link){ - if(!dv.isString(data.link)){throw new TypeError("link must be a string")} - this.link=data.link - } + this._link=data?.link } } \ No newline at end of file diff --git a/objects/Story.mjs b/objects/Story.mjs index b83befe..3316a3a 100644 --- a/objects/Story.mjs +++ b/objects/Story.mjs @@ -1,10 +1,14 @@ import Title from "./Title.mjs" export default class Story extends Title{ + set _word_count(prop){ + if(prop){ + if(!Number.isInteger(prop)){throw new TypeError("word_count must be integer!")} + this.word_count=prop + } + } + constructor(data){ super(data) - if(data?.word_count){ - if(!Number.isInteger(data.word_count)){throw new TypeError("word_count must be integer!")} - this.word_count=data.word_count - } + this._word_count=data?.word_count } } \ No newline at end of file diff --git a/objects/Submission.mjs b/objects/Submission.mjs new file mode 100644 index 0000000..21e3613 --- /dev/null +++ b/objects/Submission.mjs @@ -0,0 +1,8 @@ +import Entity from "./Entity.mjs"; + +export default class Submission extends Entity{ + constructor(data){ + super(data) + + } +} \ No newline at end of file diff --git a/objects/Title.mjs b/objects/Title.mjs index 8c36fde..49589b0 100644 --- a/objects/Title.mjs +++ b/objects/Title.mjs @@ -2,15 +2,22 @@ import Entity from "./Entity.mjs"; import dv from "./dv.mjs"; //THIS CLASS WILL HANDLE JUNCTION TABLE STUFF export default class Title extends Entity{ - constructor(data){ - super(data) - if(data?.title){ - if(!dv.isString(data.title)){throw new TypeError("title must be a string")} - this.title=data.title - } - if(data?.genres){ - if(!dv.isObject(data.genres)){throw new TypeError("genres must be an object")} - this.genres=data.genres + set _title(prop){ + if(prop){ + if(!dv.isString(prop)){throw new TypeError("title must be a string")} + this.title=prop } } + set _genres(prop){ + if(prop){ + if(!dv.isObject(prop)){throw new TypeError("genres must be an object")} + this.genres=prop + } + } + + constructor(data){ + super(data) + this._title = data?.title + this._genres = data?.genres + } } \ No newline at end of file diff --git a/test/Entity.test.mjs b/test/Entity.test.mjs index f130479..4be9f09 100644 --- a/test/Entity.test.mjs +++ b/test/Entity.test.mjs @@ -7,7 +7,7 @@ import { testDb as db } from "../db.mjs"; chai.use(chaiAsPromised) describe("tetsing Entity object",async function(){ - it("should throw with code:400 if passed an invalid data.id",async function(){ + it("should throw TypeError if passed an invalid data.id",async function(){ expect(()=>{new Entity({id:"string"})}).to.throw(TypeError) expect(()=>{new Entity({id:1.1})}).to.throw(TypeError) expect(()=>{new Entity({id:{}})}).to.throw(TypeError) diff --git a/test/Submission.test.mjs b/test/Submission.test.mjs new file mode 100644 index 0000000..f975326 --- /dev/null +++ b/test/Submission.test.mjs @@ -0,0 +1,10 @@ +import { describe } from "mocha"; +import chaiAsPromised from "chai-as-promised"; +import chai from "chai"; +import { expect} from "chai"; +import { testDb as db } from "../db.mjs"; +import Submission from "../objects/Submission.mjs" +chai.use(chaiAsPromised) +describe("testing Submission object",function(){ + it("should throw if passed invalid") +}) \ No newline at end of file