use setters for data validation
This commit is contained in:
parent
eb40822142
commit
e1c4b0c237
|
@ -1,11 +1,16 @@
|
||||||
import dv from "./dv.mjs"
|
import dv from "./dv.mjs"
|
||||||
export default class Entity{
|
export default class Entity{
|
||||||
constructor(data){
|
set _id(prop){
|
||||||
if(data?.id){
|
if(prop){
|
||||||
if(!Number.isInteger(data.id)){throw new TypeError("id must be an integer!")}
|
if(!Number.isInteger(prop)){throw new TypeError("id must be an integer!")}
|
||||||
this.id = data.id
|
this.id = prop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor(data){
|
||||||
|
this._id = data?.id
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async insert(db){
|
async insert(db){
|
||||||
return db(this.table)
|
return db(this.table)
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
import Title from "./Title.mjs";
|
import Title from "./Title.mjs";
|
||||||
import dv from "./dv.mjs";
|
import dv from "./dv.mjs";
|
||||||
export default class Publication extends Title{
|
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){
|
constructor(data){
|
||||||
super(data)
|
super(data)
|
||||||
if(data?.link){
|
this._link=data?.link
|
||||||
if(!dv.isString(data.link)){throw new TypeError("link must be a string")}
|
|
||||||
this.link=data.link
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,14 @@
|
||||||
import Title from "./Title.mjs"
|
import Title from "./Title.mjs"
|
||||||
export default class Story extends Title{
|
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){
|
constructor(data){
|
||||||
super(data)
|
super(data)
|
||||||
if(data?.word_count){
|
this._word_count=data?.word_count
|
||||||
if(!Number.isInteger(data.word_count)){throw new TypeError("word_count must be integer!")}
|
|
||||||
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";
|
import dv from "./dv.mjs";
|
||||||
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
|
//THIS CLASS WILL HANDLE JUNCTION TABLE STUFF
|
||||||
export default class Title extends Entity{
|
export default class Title extends Entity{
|
||||||
constructor(data){
|
set _title(prop){
|
||||||
super(data)
|
if(prop){
|
||||||
if(data?.title){
|
if(!dv.isString(prop)){throw new TypeError("title must be a string")}
|
||||||
if(!dv.isString(data.title)){throw new TypeError("title must be a string")}
|
this.title=prop
|
||||||
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 _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
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ import { testDb as db } from "../db.mjs";
|
||||||
chai.use(chaiAsPromised)
|
chai.use(chaiAsPromised)
|
||||||
|
|
||||||
describe("tetsing Entity object",async function(){
|
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:"string"})}).to.throw(TypeError)
|
||||||
expect(()=>{new Entity({id:1.1})}).to.throw(TypeError)
|
expect(()=>{new Entity({id:1.1})}).to.throw(TypeError)
|
||||||
expect(()=>{new Entity({id:{}})}).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