Browse Source

+validate method in Savable

Ivan Asmer 3 years ago
parent
commit
a64a559b86
1 changed files with 4 additions and 1 deletions
  1. 4 1
      index.js

+ 4 - 1
index.js

@@ -99,6 +99,9 @@ const mm = db => {
         }
         }
 
 
         async save(noSync=false){
         async save(noSync=false){
+            if (this.validate && !this.validate()){
+                throw new SyntaxError(`validation error entity ${this._id} of class ${this.constructor.name} (${this.name || this.key})`)
+            }
             if (this.empty) return this;
             if (this.empty) return this;
 
 
             const syncRelations = async () => {
             const syncRelations = async () => {
@@ -207,7 +210,7 @@ const mm = db => {
                 this[ourRelationName] = this[ourRelationName] || []
                 this[ourRelationName] = this[ourRelationName] || []
                 this[ourRelationName] = this[ourRelationName] instanceof Array ? this[ourRelationName] : [this[ourRelationName]]
                 this[ourRelationName] = this[ourRelationName] instanceof Array ? this[ourRelationName] : [this[ourRelationName]]
 
 
-                if (Savable.existsInArray(this[ourRelationName], ref)) {
+                if (!Savable.existsInArray(this[ourRelationName], ref)) {
                     this[ourRelationName].push(ref)
                     this[ourRelationName].push(ref)
                     this._id && this._loadRelations[ourRelationName].push(ref)
                     this._id && this._loadRelations[ourRelationName].push(ref)
                 }
                 }