From ee6b053162a26576dd1d53735842a0468738b40f Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Fri, 12 Jun 2015 17:27:44 +0200 Subject: [PATCH] Added more tests for validation and fixed custom validators not being detected --- lib/ModelHelpers.ts | 1 + test/Model.ts | 10 +++++++++- test/Validation.ts | 46 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/ModelHelpers.ts b/lib/ModelHelpers.ts index ce9e749..64f63bf 100644 --- a/lib/ModelHelpers.ts +++ b/lib/ModelHelpers.ts @@ -9,6 +9,7 @@ import Bluebird = require('bluebird'); export default class ModelHelpers { constructor(public model: Model) { this._validator = new skmatc(model.schema); + model.validators.forEach(validator => this._validator.register(validator)); } private _validator: Skmatc.Skmatc; diff --git a/test/Model.ts b/test/Model.ts index 6fa8faf..0995fd9 100644 --- a/test/Model.ts +++ b/test/Model.ts @@ -22,7 +22,7 @@ class Test extends Iridium.Instance 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 @@ -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({ answer: 'wrong' })).to.eventually.be.rejected; + }); it("should support a callback style instead of promises",(done) => { model.create({ answer: 15 },(err, inserted) => { @@ -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({ answer: 'wrong' })).to.eventually.be.rejected; + }); it("should support a callback style instead of promises",(done) => { model.insert({ answer: 15 },(err, inserted) => { diff --git a/test/Validation.ts b/test/Validation.ts index d0da82a..ef13860 100644 --- a/test/Validation.ts +++ b/test/Validation.ts @@ -1,5 +1,6 @@ /// import * as Iridium from '../index'; +import skmatc = require('skmatc'); interface Document { name: string; @@ -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 { static collection = 'test'; static schema: Iridium.Schema = { _id: false, name: String, - dateOfBirth: Date, + dateOfBirth: 'Over18', siblings: [{ name: String, related: Boolean, @@ -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, @@ -83,7 +113,7 @@ 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, @@ -91,7 +121,7 @@ describe("Validation", () => { }] }, { name: 'Jack', - dateOfBirth: new Date(), + dateOfBirth: new Date('1993-02-14T00:00:00.000Z'), siblings: [{ name: 'Jill', related: true, @@ -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: 'related', @@ -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, @@ -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, @@ -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: [] })));