Skip to content

Commit

Permalink
Added set/get required property for schema items. Closes issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed Apr 11, 2016
1 parent 9240cc8 commit 30167c4
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 14 deletions.
12 changes: 12 additions & 0 deletions server/dist/definitions/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ declare module Modepress
*/
public setSensitive(val: boolean);

/**
* Gets if this item is required. This will throw an error on the item if the value is not set before validation.
* @returns {boolean}
*/
public getRequired(): boolean;

/**
* Sets if this item is required. This will throw an error on the item if the value is not set before validation.
* @returns {SchemaItem<T>}
*/
public setRequired(val: boolean);

/**
* Gets if this item is indexable by mongodb
* @returns {boolean}
Expand Down
4 changes: 2 additions & 2 deletions server/dist/src/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ var Model = (function () {
if (data)
instance.schema.set(data);
// Make sure the new updates are valid
if (!instance.schema.validate()) {
if (!instance.schema.validate(false)) {
if (instance.schema.error)
toRet.error = true;
toRet.tokens.push({ error: instance.schema.error, instance: instance });
Expand Down Expand Up @@ -397,7 +397,7 @@ var Model = (function () {
// Get the schema
var schema = instance.schema;
// Make sure the parameters are valid
if (!schema.validate()) {
if (!schema.validate(true)) {
reject(new Error(schema.error));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion server/dist/src/models/posts-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var PostsModel = (function (_super) {
_super.call(this, "posts");
this.defaultSchema.add(new schema_item_factory_1.text("author", "", 1));
this.defaultSchema.add(new schema_item_factory_1.text("title", "", 1));
this.defaultSchema.add(new schema_item_factory_1.text("slug", "", 1, 512)).setUnique(true);
this.defaultSchema.add(new schema_item_factory_1.text("slug", "", 1, 512)).setUnique(true).setRequired(true);
this.defaultSchema.add(new schema_item_factory_1.text("brief", ""));
this.defaultSchema.add(new schema_item_factory_1.text("featuredImage", ""));
this.defaultSchema.add(new schema_item_factory_1.html("content", "", schema_html_1.SchemaHtml.defaultTags.concat("img"), undefined, false));
Expand Down
3 changes: 3 additions & 0 deletions server/dist/src/models/schema-items/schema-bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var SchemaBool = (function (_super) {
* @returns {boolean | string} Returns true if successful or an error message string if unsuccessful
*/
SchemaBool.prototype.validate = function () {
var val = _super.prototype.validate.call(this);
if (!val)
return false;
return true;
};
/**
Expand Down
24 changes: 24 additions & 0 deletions server/dist/src/models/schema-items/schema-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var SchemaItem = (function () {
this._unique = false;
this._uniqueIndexer = false;
this._indexable = false;
this._required = false;
this._modified = false;
}
/**
* Creates a clone of this item
Expand All @@ -20,6 +22,7 @@ var SchemaItem = (function () {
copy = copy === undefined ? new SchemaItem(this.name, this.value, this.sensitive) : copy;
copy._unique = this._unique;
copy._uniqueIndexer = this._uniqueIndexer;
copy._required = this._required;
copy.sensitive = this.sensitive;
return copy;
};
Expand All @@ -37,6 +40,19 @@ var SchemaItem = (function () {
return this;
};
/**
* Gets if this item is required. If true, then validations will fail if they are not specified
* @returns {boolean}
*/
SchemaItem.prototype.getRequired = function () { return this._required; };
/**
* Sets if this item is required. If true, then validations will fail if they are not specified
* @returns {SchemaItem}
*/
SchemaItem.prototype.setRequired = function (val) {
this._required = val;
return this;
};
/**
* Gets if this item represents a unique value in the database. An example might be a username
* @returns {boolean}
*/
Expand Down Expand Up @@ -74,6 +90,13 @@ var SchemaItem = (function () {
return this.sensitive;
};
/**
* Gets if this item has been edited since its creation
* @returns {boolean}
*/
SchemaItem.prototype.getModified = function () {
return this._modified;
};
/**
* Sets if this item is sensitive
* @returns {SchemaItem<T>}
*/
Expand Down Expand Up @@ -103,6 +126,7 @@ var SchemaItem = (function () {
* @returns {SchemaValue}
*/
SchemaItem.prototype.setValue = function (val) {
this._modified = true;
return this.value = val;
};
return SchemaItem;
Expand Down
7 changes: 6 additions & 1 deletion server/dist/src/models/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ var Schema = (function () {
};
/**
* Checks the value stored to see if its correct in its current form
* @param {boolean} checkForRequiredFields If true, then required fields must be present otherwise an error is flagged
* @returns {boolean} Returns true if successful
*/
Schema.prototype.validate = function () {
Schema.prototype.validate = function (checkForRequiredFields) {
var items = this.items;
this.error = "";
for (var i = 0, l = items.length; i < l; i++) {
if (checkForRequiredFields && !items[i].getModified() && items[i].getRequired()) {
this.error = items[i].name + " is required";
return false;
}
var validated = items[i].validate();
if (validated !== true) {
this.error = validated;
Expand Down
12 changes: 12 additions & 0 deletions server/src/definitions/custom/modepress-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@
*/
public setSensitive(val: boolean);

/**
* Gets if this item is required. This will throw an error on the item if the value is not set before validation.
* @returns {boolean}
*/
public getRequired(): boolean;

/**
* Sets if this item is required. This will throw an error on the item if the value is not set before validation.
* @returns {SchemaItem<T>}
*/
public setRequired(val: boolean);

/**
* Gets if this item is indexable by mongodb
* @returns {boolean}
Expand Down
4 changes: 2 additions & 2 deletions server/src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class Model
instance.schema.set(data);

// Make sure the new updates are valid
if (!instance.schema.validate())
if (!instance.schema.validate(false))
{
if (instance.schema.error)
toRet.error = true;
Expand Down Expand Up @@ -527,7 +527,7 @@ export class Model
var schema = instance.schema;

// Make sure the parameters are valid
if (!schema.validate())
if (!schema.validate(true))
{
reject(new Error(schema.error));
return;
Expand Down
2 changes: 1 addition & 1 deletion server/src/models/posts-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PostsModel extends Model

this.defaultSchema.add(new text("author", "", 1));
this.defaultSchema.add(new text("title", "", 1));
this.defaultSchema.add(new text("slug", "", 1, 512)).setUnique(true);
this.defaultSchema.add(new text("slug", "", 1, 512)).setUnique(true).setRequired(true);
this.defaultSchema.add(new text("brief", ""));
this.defaultSchema.add(new text("featuredImage", ""));
this.defaultSchema.add(new html("content", "", SchemaHtml.defaultTags.concat("img"), undefined, false));
Expand Down
4 changes: 4 additions & 0 deletions server/src/models/schema-items/schema-bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class SchemaBool extends SchemaItem<boolean>
*/
public validate(): boolean | string
{
var val = super.validate();
if (!val)
return false;

return true;
}

Expand Down
37 changes: 34 additions & 3 deletions server/src/models/schema-items/schema-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class SchemaItem<T>
private _unique: boolean;
private _uniqueIndexer: boolean;
private _indexable: boolean;
private _required: boolean;
private _modified: boolean;

constructor(name: string, value: T, sensitive: boolean)
{
Expand All @@ -18,6 +20,8 @@ export class SchemaItem<T>
this._unique = false;
this._uniqueIndexer = false;
this._indexable = false;
this._required = false;
this._modified = false;
}

/**
Expand All @@ -30,6 +34,7 @@ export class SchemaItem<T>
copy = copy === undefined ? new SchemaItem(this.name, this.value, this.sensitive) : copy;
copy._unique = this._unique;
copy._uniqueIndexer = this._uniqueIndexer;
copy._required = this._required;
copy.sensitive = this.sensitive;
return copy;
}
Expand All @@ -39,7 +44,7 @@ export class SchemaItem<T>
* @returns {boolean}
*/
public getIndexable(): boolean { return this._indexable; }

/**
* Sets if this item is indexable by mongodb
* @returns {SchemaItem}
Expand All @@ -50,6 +55,22 @@ export class SchemaItem<T>
return this;
}

/**
* Gets if this item is required. If true, then validations will fail if they are not specified
* @returns {boolean}
*/
public getRequired(): boolean { return this._required; }

/**
* Sets if this item is required. If true, then validations will fail if they are not specified
* @returns {SchemaItem}
*/
public setRequired(val?: boolean): SchemaItem<T>
{
this._required = val;
return this;
}

/**
* Gets if this item represents a unique value in the database. An example might be a username
* @returns {boolean}
Expand All @@ -67,15 +88,15 @@ export class SchemaItem<T>
}

/**
* Gets if this item must be indexed when searching for uniqueness. For example, an item 'name' might be set as unique. But
* Gets if this item must be indexed when searching for uniqueness. For example, an item 'name' might be set as unique. But
* we might not be checking uniqueness for all items where name is the same. It might be where name is the same, but only in
* a given project. In this case the project item is set as a uniqueIndexer
* @returns {boolean}
*/
public getUniqueIndexer(): boolean { return this._uniqueIndexer; }

/**
* Sets if this item must be indexed when searching for uniqueness. For example, an item 'name' might be set as unique. But
* Sets if this item must be indexed when searching for uniqueness. For example, an item 'name' might be set as unique. But
* we might not be checking uniqueness for all items where name is the same. It might be where name is the same, but only in
* a given project. In this case the project item is set as a uniqueIndexer
* @returns {SchemaItem}
Expand All @@ -95,6 +116,15 @@ export class SchemaItem<T>
return this.sensitive;
}

/**
* Gets if this item has been edited since its creation
* @returns {boolean}
*/
public getModified(): boolean
{
return this._modified;
}

/**
* Sets if this item is sensitive
* @returns {SchemaItem<T>}
Expand Down Expand Up @@ -131,6 +161,7 @@ export class SchemaItem<T>
*/
public setValue(val : T): T
{
this._modified = true;
return this.value = val;
}
}
11 changes: 9 additions & 2 deletions server/src/models/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,22 @@ export class Schema

/**
* Checks the value stored to see if its correct in its current form
* @returns {boolean} Returns true if successful
* @param {boolean} checkForRequiredFields If true, then required fields must be present otherwise an error is flagged
* @returns {boolean} Returns true if successful
*/
public validate(): boolean
public validate( checkForRequiredFields: boolean ): boolean
{
var items = this.items;
this.error = "";

for (var i = 0, l = items.length; i < l; i++)
{
if ( checkForRequiredFields && !items[i].getModified() && items[i].getRequired() )
{
this.error = `${items[i].name} is required`;
return false;
}

var validated = items[i].validate();
if (validated !== true)
{
Expand Down
17 changes: 15 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Testing all post related endpoints', function() {
});
})

it('Cannot create post without title', function(done) {
it('Cannot create a post without title', function(done) {
modepressAgent
.post('/api/posts/create-post').set('Accept', 'application/json').expect(200).expect('Content-Type', /json/)
.set('Cookie', adminCookie)
Expand All @@ -102,8 +102,21 @@ describe('Testing all post related endpoints', function() {
done();
});
})

it('Cannot create a post without a slug field', function(done) {
modepressAgent
.post('/api/posts/create-post').set('Accept', 'application/json').expect(200).expect('Content-Type', /json/)
.set('Cookie', adminCookie)
.send( { title: "test" } )
.end(function(err, res) {
test
.string(res.body.message).is("slug is required")
.bool(res.body.error).isTrue()
done();
});
})

it('Cannot create post without slug', function(done) {
it('Cannot create a post without slug', function(done) {
modepressAgent
.post('/api/posts/create-post').set('Accept', 'application/json').expect(200).expect('Content-Type', /json/)
.set('Cookie', adminCookie)
Expand Down

0 comments on commit 30167c4

Please sign in to comment.