Skip to content

Commit

Permalink
Added more tests for validation and fixed custom validators not being…
Browse files Browse the repository at this point in the history
… detected
  • Loading branch information
notheotherben committed Jun 12, 2015
1 parent 6bbf45b commit ee6b053
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/ModelHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Bluebird = require('bluebird');
export default class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
constructor(public model: Model<TDocument, TInstance>) {
this._validator = new skmatc(model.schema);
model.validators.forEach(validator => this._validator.register(validator));
}

private _validator: Skmatc.Skmatc;
Expand Down
10 changes: 9 additions & 1 deletion test/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument
}

class TestWithCustomID extends Test {
static transforms: { [key: string]: { fromDB: (value: any) => any; toDB: (value: any) => any; }} = {
static transforms: Iridium.Transforms = {
_id: {
fromDB: x => x * 10,
toDB: x => x / 10
Expand Down Expand Up @@ -203,6 +203,10 @@ describe("Model",() => {
it("should allow you to provide options to control the creation",() => {
return chai.expect(model.create({ answer: 14 }, { upsert: true })).to.eventually.exist;
});

it("should return an error if you don't meet the schema validation requirements",() => {
return chai.expect(model.create(<any>{ answer: 'wrong' })).to.eventually.be.rejected;
});

it("should support a callback style instead of promises",(done) => {
model.create({ answer: 15 },(err, inserted) => {
Expand Down Expand Up @@ -247,6 +251,10 @@ describe("Model",() => {
it("should allow you to provide options to control the creation",() => {
return chai.expect(model.insert({ answer: 14 }, { upsert: true })).to.eventually.exist;
});

it("should return an error if you don't meet the schema validation requirements",() => {
return chai.expect(model.insert(<any>{ answer: 'wrong' })).to.eventually.be.rejected;
});

it("should support a callback style instead of promises",(done) => {
model.insert({ answer: 15 },(err, inserted) => {
Expand Down
46 changes: 38 additions & 8 deletions test/Validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../_references.d.ts" />
import * as Iridium from '../index';
import skmatc = require('skmatc');

interface Document {
name: string;
Expand All @@ -11,12 +12,15 @@ interface Document {
}[];
}

@Iridium.Validate('Over18', function(schema, data) {
return this.assert(data.getTime && data.getTime() < (new Date().getTime() - 365 * 86400 * 18));
})
class Person extends Iridium.Instance<Document, Person> {
static collection = 'test';
static schema: Iridium.Schema = {
_id: false,
name: String,
dateOfBirth: Date,
dateOfBirth: 'Over18',
siblings: [{
name: String,
related: Boolean,
Expand All @@ -42,12 +46,38 @@ describe("Validation", () => {
after(() => model.remove().then(() => core.close()));

beforeEach(() => model.remove());

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

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

describe("inserting", () => {
it("should successfully validate single documents which match the schema", () => {
return chai.expect(model.insert({
name: 'John',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jane',
related: true,
Expand Down Expand Up @@ -83,15 +113,15 @@ describe("Validation", () => {
it("should successfully validate multiple documents which match the schema", () => {
return chai.expect(model.insert([{
name: 'Frank',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Francie',
related: false,
ageDifference: -2
}]
}, {
name: 'Jack',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
Expand All @@ -103,7 +133,7 @@ describe("Validation", () => {
it("should fail to validate multiple documents which do not match the schema", () => {
return chai.expect(model.insert([{
name: 'Frank',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Francie',
related: <any>'related',
Expand Down Expand Up @@ -131,7 +161,7 @@ describe("Validation", () => {
}]
}, {
name: 'Jack',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
Expand All @@ -151,7 +181,7 @@ describe("Validation", () => {
}]
}, {
name: 'Jack',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: [{
name: 'Jill',
related: true,
Expand All @@ -164,7 +194,7 @@ describe("Validation", () => {
describe("instances", () => {
beforeEach(() => model.remove().then(() => model.insert({
name: 'Frank',
dateOfBirth: new Date(),
dateOfBirth: new Date('1993-02-14T00:00:00.000Z'),
siblings: []
})));

Expand Down

0 comments on commit ee6b053

Please sign in to comment.