Skip to content

Commit

Permalink
Added transform decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 12, 2015
1 parent c4f221f commit 5e13027
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,12 @@ export function ObjectID(target: { constructor: typeof Instance }, name: string)
if (value && /^[a-f0-9]{24}$/.test(value)) return MongoDB.ObjectID.createFromHexString(value);
return value;
}
export function Transform(fromDB: (value: any) => any, toDB: (value: any) => any) {
return function(target: any, property: string) {
target.constructor.transforms = _.clone(target.constructor.transforms || {})
target.constructor.transforms[property] = {
fromDB: fromDB,
toDB: toDB
};
};
}
63 changes: 52 additions & 11 deletions test/Decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@
import * as Iridium from '../index';
import 'reflect-metadata';
import skmatc = require('skmatc');
import MongoDB = require('mongodb');

interface TestDocument {
_id?: string;
name: string;
email: string;
}

function VersionValidator(schema, data) {
return this.assert(/^\d+\.\d+\.\d+(?:-.+)?$/.test(data));
}

@Iridium.Collection('test')
@Iridium.Index({ name: 1 })
@Iridium.Index({ email: 1 }, { background: true })
@Iridium.Validate('version', function(schema, data) { return this.assert(/^\d+\.\d+\.\d+(?:-.+)?$/.test(data)); })
@Iridium.Validate('version', VersionValidator)
@Iridium.Property('version', 'version')
class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument {
@Iridium.ObjectID
_id: string;

@Iridium.Property(String)
name: string;

@Iridium.Property(/^.+@.+$/)

@Iridium.Property(/^.+@.+$/)
@Iridium.Transform(email => email.toLowerCase().trim(), email => email.toLowerCase().trim())
email: string;

version: string;
Expand All @@ -32,6 +38,10 @@ describe("Decorators", () => {
it("should populate the collection static field", () => {
chai.expect(Test.collection).to.equal('test');
});

it("should not pollute the parent's collection property", () => {
chai.expect(Iridium.Instance.collection).to.not.exist;
});
});

describe("Index", () => {
Expand All @@ -40,23 +50,31 @@ describe("Decorators", () => {
});

it("should support just spec indexes", () => {
chai.expect(Test.indexes).to.containOneLike({ spec: { name: 1 } });
chai.expect(Test.indexes).to.containOneLike({ spec: { name: 1 }, options: {} });
});

it("should support indexes with both a spec and options", () => {
chai.expect(Test.indexes).to.containOneLike({ spec: { email: 1 }, options: { background: true }});
});

it("should not pollute the parent's index object", () => {
chai.expect(Iridium.Instance.indexes).to.exist.and.have.length(0);
});
});

describe("Validate", () => {
it("should populate the constructor's valdiators property", () => {
chai.expect(Test.validators).to.exist.and.have.length(1);
chai.expect(Test.validators).to.exist.and.have.length(Iridium.Instance.validators.length + 1);
});

it("should create a valid Skmatc validator instance", () => {
chai.expect(Test.validators[0]).to.be.a('function');
var s = new skmatc(Test.schema);
s.register(Test.validators[0]);
let s = new skmatc(Test.schema);

for (let i = 0; i < Test.validators.length; i++) {
chai.expect(Test.validators[i]).to.be.a('function');
s.register(Test.validators[i]);
}

chai.expect(s.validate({
name: 'Test',
email: 'test@test.com',
Expand All @@ -65,15 +83,23 @@ describe("Decorators", () => {
});

it("should correctly include the validations", () => {
chai.expect(Test.validators[0]).to.be.a('function');
var s = new skmatc(Test.schema);
s.register(Test.validators[0]);
let s = new skmatc(Test.schema);

for (let i = 0; i < Test.validators.length; i++) {
chai.expect(Test.validators[i]).to.be.a('function');
s.register(Test.validators[i]);
}

chai.expect(s.validate({
name: 'Test',
email: 'test@test.com',
version: 'a1.0.0'
})).to.exist.and.have.property('failures').not.eql([]);
});

it("should not pollute the parent's validators object", () => {
chai.expect(Iridium.Instance.validators).to.exist.and.have.length(1);
});
});

describe("ObjectID", () => {
Expand All @@ -97,4 +123,19 @@ describe("Decorators", () => {
chai.expect(Test.schema).to.exist.and.have.property('version', 'version');
});
});
describe("Transform", () => {
it("should not remove existing entries in the transforms object", () => {
chai.expect(Test.transforms).to.exist.and.have.property('_id').with.property('fromDB').which.is.a('function');
chai.expect(Test.transforms).to.exist.and.have.property('_id').with.property('toDB').which.is.a('function');
});

it("should populate the constructor's transforms object", () => {
chai.expect(Test.transforms).to.exist.and.have.property('email').with.property('fromDB').which.is.a('function');
chai.expect(Test.transforms).to.exist.and.have.property('email').with.property('toDB').which.is.a('function');
});

it("should not pollute the parent's transforms object", () => {
chai.expect(Iridium.Instance.transforms).to.exist.and.not.have.property('email');
});
});
});

0 comments on commit 5e13027

Please sign in to comment.