use setters for data validation
This commit is contained in:
parent
eb40822142
commit
e1c4b0c237
|
@ -1,12 +1,17 @@
|
|||
import dv from "./dv.mjs"
|
||||
export default class Entity{
|
||||
set _id(prop){
|
||||
if(prop){
|
||||
if(!Number.isInteger(prop)){throw new TypeError("id must be an integer!")}
|
||||
this.id = prop
|
||||
}
|
||||
}
|
||||
|
||||
constructor(data){
|
||||
if(data?.id){
|
||||
if(!Number.isInteger(data.id)){throw new TypeError("id must be an integer!")}
|
||||
this.id = data.id
|
||||
}
|
||||
this._id = data?.id
|
||||
}
|
||||
|
||||
|
||||
async insert(db){
|
||||
return db(this.table)
|
||||
.insert(this)
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import Entity from "./Entity.mjs";
|
||||
|
||||
export default class Submission extends Entity{
|
||||
constructor(data){
|
||||
super(data)
|
||||
|
||||
}
|
||||
}
|
|
@ -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{
|
||||
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)
|
||||
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
|
||||
}
|
||||
this._title = data?.title
|
||||
this._genres = data?.genres
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
})
|
Loading…
Reference in New Issue