json-schema-models
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -48,2 +48,3 @@ 'use strict'; | ||
this.collections = this.settings.mongo.collections; | ||
this._collectionNames = []; | ||
this.db = null; | ||
@@ -55,4 +56,2 @@ }; | ||
const compileErr = this.compile(); | ||
const definitions = {}; | ||
const records = {}; | ||
if (compileErr) { | ||
@@ -84,20 +83,11 @@ return cb(compileErr); | ||
const keys = Object.keys(this.schema.records); | ||
for (let i = 0; i < keys.length; ++i) { | ||
return this.createSchemas(db, (err, schemas) => { | ||
const name = keys[i]; | ||
records[name] = class extends Wadofgum.mixin(Validation, Datastore) {}; | ||
records[name].schema = this.schema.records[name]; | ||
records[name].validator = this.validator; | ||
records[name].db = db; | ||
} | ||
const defs = Object.keys(this.schema.definitions); | ||
for (let i = 0; i < defs.length; ++i) { | ||
const name = defs[i]; | ||
definitions[name] = class extends Wadofgum.mixin(Validation) {}; | ||
definitions[name].schema = this.schema.definitions[name]; | ||
definitions[name].validator = this.validator; | ||
} | ||
return cb(null, { collections, db, definitions, records }); | ||
if (err) { | ||
return cb(err); | ||
} | ||
const definitions = schemas.definitions; | ||
const records = schemas.records; | ||
return cb(null, { collections, db, definitions, records }); | ||
}); | ||
}); | ||
@@ -207,2 +197,3 @@ }); | ||
const collectionName = rawCollection.name; | ||
this._collectionNames.push(collectionName); | ||
const options = rawCollection.options; | ||
@@ -226,2 +217,43 @@ | ||
createSchemas(db, cb) { | ||
const definitions = {}; | ||
const records = {}; | ||
const keys = Object.keys(this.schema.records); | ||
for (let i = 0; i < keys.length; ++i) { | ||
const name = keys[i]; | ||
const record = this.schema.records[name]; | ||
if (this._collectionNames.indexOf(record.metaSchema.base) !== -1) { | ||
records[name] = class extends Wadofgum.mixin(Validation, Datastore) {}; | ||
records[name].schema = record; | ||
records[name].validator = this.validator; | ||
records[name].db = db; | ||
} | ||
else { | ||
return cb(new Error('Base name for record schema ' + name + ' has no corresponding collection')); | ||
} | ||
} | ||
const defs = Object.keys(this.schema.definitions); | ||
for (let i = 0; i < defs.length; ++i) { | ||
const name = defs[i]; | ||
definitions[name] = class extends Wadofgum.mixin(Validation) {}; | ||
definitions[name].schema = this.schema.definitions[name]; | ||
definitions[name].validator = this.validator; | ||
} | ||
return cb(null, { definitions, records }); | ||
}; | ||
addSchemas(schemas) { | ||
this.schema.addSchemas(schemas); | ||
}; | ||
addFormats(formats) { | ||
this.schema.addFormats(formats); | ||
}; | ||
}; | ||
@@ -228,0 +260,0 @@ |
{ | ||
"name": "json-schema-models", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "json schema based models", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -22,4 +22,9 @@ # json-schema-models [![build status](https://travis-ci.org/simon-p-r/json-schema-models.svg?branch=master)](https://travis-ci.org/simon-p-r/json-schema-models) | ||
+ formats - an object with keys being name of format to register and value being the custom function to register for z-schema validation | ||
+ validator - an object created by z-schema constructor function | ||
##### .addSchemas(schemas) | ||
+ method to addSchemas prior to connecting to database, schemas can be an object with keys being records and definitions and the values being key of schema name and value the schema object to loaded | ||
##### .start(callback) | ||
@@ -41,3 +46,3 @@ | ||
+ method to build indexes based on schema definition | ||
+ method to build indexes based on schema definitions | ||
@@ -44,0 +49,0 @@ ##### params |
@@ -9,3 +9,3 @@ 'use strict'; | ||
type: 'record', | ||
base: 'testCollection', | ||
base: 'lookup', | ||
jsonSchema: 'v4', | ||
@@ -12,0 +12,0 @@ name: 'testRec', |
@@ -67,3 +67,3 @@ 'use strict'; | ||
manager.settings.mongo.url = 'mongodb://localhost:27018'; | ||
manager.schema.addSchemas(Schemas); | ||
manager.addSchemas(Schemas); | ||
manager.start((err, result) => { | ||
@@ -97,4 +97,4 @@ | ||
manager.schema.addSchemas(Schemas); | ||
manager.schema.addSchemas([InvalidDef]); | ||
manager.addSchemas(Schemas); | ||
manager.addSchemas([InvalidDef]); | ||
manager.start((err, result) => { | ||
@@ -111,3 +111,3 @@ | ||
manager.schema.addSchemas([InvalidRef]); | ||
manager.addSchemas([InvalidRef]); | ||
manager.start((err, result) => { | ||
@@ -139,3 +139,3 @@ | ||
}); | ||
datastore.schema.addSchemas(Schemas); | ||
datastore.addSchemas(Schemas); | ||
datastore.start((err, result) => { | ||
@@ -150,3 +150,30 @@ | ||
it('should return an error from start method when a createSchemas returns an error', (done) => { | ||
const Invalid = Hoek.clone(Schemas); | ||
Invalid[1].metaSchema.base = 'invalid'; | ||
const datastore = new Manager({ | ||
mongo: { | ||
name: 'test_db', | ||
url: 'mongodb://localhost:27017', | ||
options: { | ||
}, | ||
collections: Collections | ||
}, | ||
schema: { | ||
formats: Formats | ||
} | ||
}); | ||
datastore.addSchemas(Invalid); | ||
datastore.start((err, result) => { | ||
expect(err).to.exist(); | ||
expect(err.toString()).to.contain('Base name for record schema'); | ||
done(); | ||
}); | ||
}); | ||
it('should return an error if no record schemas have been loaded by addSchemas method', (done) => { | ||
@@ -164,4 +191,4 @@ | ||
manager.schema.addSchemas(Schemas); | ||
manager.schema.addSchemas([InvalidRec]); | ||
manager.addSchemas(Schemas); | ||
manager.addSchemas([InvalidRec]); | ||
manager.start((err, result) => { | ||
@@ -177,4 +204,4 @@ | ||
manager.schema.addSchemas(Schemas); | ||
manager.schema.addSchemas([InvalidDef]); | ||
manager.addSchemas(Schemas); | ||
manager.addSchemas([InvalidDef]); | ||
manager.start((err, result) => { | ||
@@ -188,5 +215,23 @@ | ||
it('should add custom formats via addFormats method', (done) => { | ||
manager.addSchemas(Schemas); | ||
manager.addFormats({ | ||
customFormat: function (string) { | ||
return true; | ||
} | ||
}); | ||
manager.start((err, result) => { | ||
expect(err).not.to.exist(); | ||
expect(manager.schema.formats.customFormat).to.be.a.function(); | ||
manager.stop(done); | ||
}); | ||
}); | ||
it('should successfully start db and return collections, records and db objects', (done) => { | ||
manager.schema.addSchemas(Schemas); | ||
manager.addSchemas(Schemas); | ||
manager.start((err, result) => { | ||
@@ -206,3 +251,3 @@ | ||
manager.schema.addSchemas(Schemas); | ||
manager.addSchemas(Schemas); | ||
manager.start((err, result) => { | ||
@@ -218,3 +263,3 @@ | ||
manager.schema.addSchemas(Schemas); | ||
manager.addSchemas(Schemas); | ||
manager.start((err, result) => { | ||
@@ -237,3 +282,3 @@ | ||
manager.collections = invalid; | ||
manager.schema.addSchemas(Schemas); | ||
manager.addSchemas(Schemas); | ||
manager.start((err, result) => { | ||
@@ -240,0 +285,0 @@ |
Sorry, the diff of this file is not supported yet
23795
637
55