Skip to content

Commit

Permalink
feat: Add a Binary property decorator for use with binary data proper…
Browse files Browse the repository at this point in the history
…ties in Buffer format
  • Loading branch information
notheotherben committed Nov 17, 2015
1 parent b182464 commit c857951
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 17 deletions.
16 changes: 16 additions & 0 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,20 @@ export function ObjectID(target: Instance<any, any>, name: string) {
DefaultTransforms.ObjectID.fromDB,
DefaultTransforms.ObjectID.toDB
)(target, name);
}

/**
* Specifies that this property should be stored using the MongoDB binary type and represented as a Buffer.
*
* This decorator applies a Buffer validator to the property, which ensures that values you send to the database
* are well formatted Buffer objects represented using the BSON Binary datatype. In addition to this, it will
* apply a transform which ensures you only work with Buffer objects and that data is always stored in Binary
* format.
*/
export function Binary(target: Instance<any, any>, name: string) {
Property(Buffer)(target, name);
Transform(
DefaultTransforms.Binary.fromDB,
DefaultTransforms.Binary.toDB
)(target, name);
}
115 changes: 98 additions & 17 deletions test/Validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import skmatc = require('skmatc');
import MongoDB = require('mongodb');

interface Document {
_id?: string;
name: string;
dateOfBirth: Date;
siblings: {
name: string;
related: boolean;
ageDifference: number;
}[];
avatar: Buffer;
}

@Iridium.Validate('Over18', function(schema, data) {
return this.assert(data.getTime && data.getTime() < (new Date().getTime() - 365 * 86400 * 18));
return this.assert(data.getTime && data.getTime() < (new Date().getTime() - 365 * 86400 * 18 * 1000));
})
class Person extends Iridium.Instance<Document, Person> {
static collection = 'test';
Expand All @@ -26,16 +28,23 @@ class Person extends Iridium.Instance<Document, Person> {
name: String,
related: Boolean,
ageDifference: Number
}]
}],
avatar: Buffer
};

@Iridium.ObjectID
_id: string;
name: string;

dateOfBirth: Date;
siblings: {
name: string;
related: boolean;
ageDifference: number;
}[];

@Iridium.Binary
avatar: Buffer;
}

describe("Validation", () => {
Expand All @@ -57,7 +66,8 @@ describe("Validation", () => {
name: 'Jane',
related: true,
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.ok;
});

Expand All @@ -69,8 +79,67 @@ describe("Validation", () => {
name: 'Jane',
related: true,
ageDifference: -2
}]
})).to.eventually.be.ok;
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.rejected;
});

describe("ObjectID", () => {
it("should successfully validate valid documents", () => {
return chai.expect(model.insert({
_id: '012345670123456701234567',
name: 'John',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jane',
related: true,
ageDifference: -2
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.ok;
});

it("should fail to validate documents which are invalid", () => {
return chai.expect(model.insert({
_id: 'this is an invalid id',
name: 'John',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jane',
related: true,
ageDifference: -2
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.rejected;
});
});

describe("Binary", () => {
it("should successfully validate valid documents", () => {
return chai.expect(model.insert({
name: 'John',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jane',
related: true,
ageDifference: -2
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.ok;
});

it("should fail to validate documents which are invalid", () => {
return chai.expect(model.insert({
name: 'John',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jane',
related: true,
ageDifference: -2
}],
avatar: <any>'test'
})).to.eventually.be.rejected;
});
});
});

Expand All @@ -83,7 +152,8 @@ describe("Validation", () => {
name: 'Jane',
related: true,
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.ok;
});

Expand All @@ -95,7 +165,8 @@ describe("Validation", () => {
name: 'Jane',
related: true,
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
})).to.eventually.be.rejected;
});

Expand All @@ -107,7 +178,8 @@ describe("Validation", () => {
name: 'Jane',
related: true,
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
}).catch(() => chai.expect(model.findOne({ dateOfBirth: 0 })).to.eventually.be.null);
});

Expand All @@ -119,15 +191,17 @@ describe("Validation", () => {
name: 'Francie',
related: false,
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
}, {
name: 'Jack',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
ageDifference: 2
}]
}],
avatar: new Buffer("test", "utf8")
}])).to.eventually.be.ok;
});

Expand All @@ -139,15 +213,17 @@ describe("Validation", () => {
name: 'Francie',
related: <any>'related',
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
}, {
name: <any>5,
dateOfBirth: new Date(),
siblings: [{
name: 'Jill',
related: true,
ageDifference: 2
}]
}],
avatar: new Buffer("test", "utf8")
}])).to.eventually.be.rejected;
});

Expand All @@ -159,15 +235,17 @@ describe("Validation", () => {
name: 'Francie',
related: <any>'related',
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
}, {
name: 'Jack',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
ageDifference: 2
}]
}],
avatar: new Buffer("test", "utf8")
}])).to.eventually.be.rejected;
});

Expand All @@ -179,15 +257,17 @@ describe("Validation", () => {
name: 'Francie',
related: <any>'related',
ageDifference: -2
}]
}],
avatar: new Buffer("test", "utf8")
}, {
name: 'Jack',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
ageDifference: 2
}]
}],
avatar: new Buffer("test", "utf8")
}]).catch(() => chai.expect(model.findOne({ 'siblings.related': 'related' })).to.eventually.be.null);
});
});
Expand All @@ -196,7 +276,8 @@ describe("Validation", () => {
beforeEach(() => model.remove().then(() => model.insert({
name: 'Frank',
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: []
siblings: [],
avatar: new Buffer("test", "utf8")
})));

it("should validate documents when you attempt to change them", () => {
Expand Down

0 comments on commit c857951

Please sign in to comment.