Skip to content

Commit

Permalink
Added verification to foreign keys to make sure the target exists
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed May 17, 2016
1 parent 7804994 commit 1676417
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
45 changes: 29 additions & 16 deletions server/dist/src/models/schema-items/schema-foreign-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
const schema_item_1 = require("./schema-item");
const Model_1 = require("../Model");
const model_1 = require("../model");
const mongodb_1 = require("mongodb");
const utils_1 = require("../../utils");
/**
Expand Down Expand Up @@ -46,20 +46,33 @@ class SchemaForeignKey extends schema_item_1.SchemaItem {
* @returns {Promise<boolean|Error>}
*/
validate() {
var transformedValue = this.value;
if (typeof this.value == "string") {
if (utils_1.Utils.isValidObjectID(this.value))
transformedValue = this.value = new mongodb_1.ObjectID(this.value);
else if (this.value.trim() != "")
return Promise.reject(new Error(`Please use a valid ID for '${this.name}'`));
else
transformedValue = null;
}
if (!transformedValue) {
this.value = null;
return Promise.resolve(true);
}
return Promise.resolve(true);
return __awaiter(this, void 0, Promise, function* () {
var transformedValue = this.value;
if (typeof this.value == "string") {
if (utils_1.Utils.isValidObjectID(this.value))
transformedValue = this.value = new mongodb_1.ObjectID(this.value);
else if (this.value.trim() != "")
return Promise.reject(new Error(`Please use a valid ID for '${this.name}'`));
else
transformedValue = null;
}
if (!transformedValue) {
this.value = null;
return Promise.resolve(true);
}
else if (!this.optionalKey) {
// If they key is required then it must exist
var model = model_1.Model.getByName(this.targetCollection);
if (model) {
var result = yield model.findOne({ _id: this.value });
if (!result)
throw new Error(`${this.name} does not exist`);
}
else
throw new Error(`${this.name} references a foreign key '${this.targetCollection}' which doesn't seem to exist`);
}
return true;
});
}
/**
* Gets the value of this item
Expand All @@ -71,7 +84,7 @@ class SchemaForeignKey extends schema_item_1.SchemaItem {
if (!options.expandForeignKeys)
return this.value;
else {
var model = Model_1.Model.getByName(this.targetCollection);
var model = model_1.Model.getByName(this.targetCollection);
if (model) {
var result = yield model.findOne({ _id: this.value });
return yield result.schema.getAsJson(result.dbEntry._id, options);
Expand Down
19 changes: 16 additions & 3 deletions server/src/models/schema-items/schema-foreign-key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SchemaItem} from "./schema-item";
import {ISchemaOptions} from "modepress-api";
import {Model} from "../Model";
import {Model} from "../model";
import {ObjectID} from "mongodb";
import {Utils} from "../../utils"

Expand Down Expand Up @@ -46,7 +46,7 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
* Checks the value stored to see if its correct in its current form
* @returns {Promise<boolean|Error>}
*/
public validate(): Promise<boolean|Error>
public async validate(): Promise<boolean|Error>
{
var transformedValue = this.value;

Expand All @@ -65,8 +65,21 @@ export class SchemaForeignKey extends SchemaItem<ObjectID | string | Modepress.I
this.value = null;
return Promise.resolve(true);
}
else if (!this.optionalKey)
{
// If they key is required then it must exist
var model = Model.getByName(this.targetCollection);
if (model)
{
var result = await model.findOne<Modepress.IModelEntry>( { _id : <ObjectID>this.value } );
if (!result)
throw new Error(`${this.name} does not exist`);
}
else
throw new Error(`${this.name} references a foreign key '${this.targetCollection}' which doesn't seem to exist`);
}

return Promise.resolve(true);
return true;
}

/**
Expand Down

0 comments on commit 1676417

Please sign in to comment.