Comparing version 2.10.5 to 2.11.0
@@ -1,12 +0,18 @@ | ||
var Database = require('iridium'); | ||
var Database = require('iridium'), | ||
skmatc = require('skmatc'); | ||
var Plugin = { | ||
validate: function(schema, value, propertyName) { | ||
if(schema == 'Uppercase') | ||
return this.assert(value.toUpperCase() == value, 'uppercase string', value); | ||
if(schema == 'Lowercase') | ||
return this.assert(value.toLowerCase() == value, 'lowercase string', value); | ||
} | ||
validate: [ | ||
skmatc.Validator.module(function(schema) { | ||
return schema == "Uppercase"; | ||
}, function(schema, data, path) { | ||
return this.assert(value.toUpperCase() == value); | ||
}), | ||
skmatc.Validator.module(function(schema) { | ||
return schema == "Lowercase"; | ||
}, function(schema, data, path) { | ||
return this.assert(value.toLowerCase() == value); | ||
})] | ||
}; | ||
module.exports = Plugin; |
@@ -7,3 +7,2 @@ var ObjectID = require('mongodb').ObjectID, | ||
inherit = require('./utils/Inherit.js'), | ||
validate = require('./utils/validation.js'), | ||
diff = require('./utils/diff.js'); | ||
@@ -35,3 +34,3 @@ | ||
model: model, | ||
isNew: isNew, | ||
isNew: isNew || false, | ||
original: _.cloneDeep(doc), | ||
@@ -134,4 +133,4 @@ modified: _.cloneDeep(doc) | ||
if(!changes) { | ||
var validation = validate(this.__state.model.schema, this.__state.modified, undefined, this.__state.model.extraValidators); | ||
if(!validation.passed) return callback(validation.toError()); | ||
var validation = this.__state.model.schemaValidator.validate(this.__state.modified); | ||
if(validation.failed) return callback(validation.error); | ||
@@ -165,2 +164,6 @@ var original = _.cloneDeep(this.__state.original); | ||
if (err) return onError(err); | ||
if(!latest) { | ||
this.__state.isNew = true; | ||
return (callback || function () { })(null, this); | ||
} | ||
@@ -211,4 +214,9 @@ this.__state.model.onRetrieved(conditions, latest, callback || function () { }, (function (value) { | ||
Instance.prototype.remove = Instance.prototype.delete = function(callback) { | ||
/// <signature> | ||
/// <summary>Removes this object from the database collection</summary> | ||
/// </signature> | ||
/// <signature> | ||
/// <summary>Removes this object from the database collection</summary> | ||
/// <param name="callback" value="callback(new Error())">A function to be called when the object has been removed</param> | ||
/// </signature> | ||
@@ -215,0 +223,0 @@ if(this.__state.isNew) return (callback || function() { })(null, 0); |
@@ -7,2 +7,3 @@ var _ = require('lodash'), | ||
Concoction = require('concoction'), | ||
skmatc = require('skmatc'), | ||
@@ -12,3 +13,2 @@ inherit = require('./utils/Inherit.js'), | ||
Instance = require('./Instance.js'), | ||
validate = require('./utils/validation.js'), | ||
NoOpCache = require('./caches/NoOpCache.js'); | ||
@@ -69,2 +69,8 @@ | ||
}); | ||
var schemaValidator = new skmatc(schema); | ||
Object.defineProperty(this, 'schemaValidator', { | ||
get: function() { return schemaValidator; }, | ||
enumerable: false | ||
}); | ||
@@ -94,12 +100,14 @@ Object.defineProperty(this, 'options', { | ||
var i, extraValidators = []; | ||
var i; | ||
for(i = 0; i < database.plugins.length; i++) { | ||
if(database.plugins[i].validate) | ||
extraValidators.push(database.plugins[i].validate); | ||
if(database.plugins[i].validate) { | ||
var validators = database.plugins[i].validate; | ||
if(Array.isArray(validators)) | ||
_.forEach(validators, function(validator) { this.schemaValidator.register(validator); }, this); | ||
else | ||
this.schemaValidator.register(validators); | ||
} | ||
} | ||
Object.defineProperty(this, 'extraValidators', { | ||
get: function() { return extraValidators; } | ||
}); | ||
for(i = 0; i < database.plugins.length; i++) { | ||
@@ -496,5 +504,4 @@ if(database.plugins[i].newModel) database.plugins[i].newModel.call(this, database, collection, schema, options); | ||
// Validate the object | ||
var validation = validate(this.schema, obj, undefined, this.extraValidators); | ||
if (!validation.passed) return done(validation.toError()); | ||
var validation = this.schemaValidator.validate(obj); | ||
if(validation.failed) return done(validation.error); | ||
@@ -501,0 +508,0 @@ // Transform the object |
{ | ||
"name": "iridium", | ||
"version": "2.10.5", | ||
"version": "2.11.0", | ||
"author": "Benjamin Pannell <admin@sierrasoftworks.com>", | ||
@@ -24,2 +24,3 @@ "description": "A custom lightweight ORM for MongoDB designed for power-users", | ||
"mongodb": "*", | ||
"skmatc": "*", | ||
"lodash": "*", | ||
@@ -26,0 +27,0 @@ "async": "*", |
@@ -168,2 +168,19 @@ # Iridium [![Build Status](https://travis-ci.org/SierraSoftworks/Iridium.png?branch=master)](https://travis-ci.org/SierraSoftworks/Iridium) [![](https://badge.fury.io/js/iridium.png)](https://npmjs.org/package/iridium) | ||
### Validation | ||
Iridium now (as of **v2.11.0**) uses [skmatc](https://sierrasoftworks.com/skmatc)(pronounced schematic) for schema validation, it allows you to quickly and easily write complex schemas in a readable manner and provides a powerful extensibility framework which you can use if you require more complex validation logic. | ||
```javascript | ||
var schema = { | ||
username: /\w[\w\d_]{7,}/, | ||
email: String, | ||
dateOfBirth: Date, | ||
sessions: [{ | ||
id: String, | ||
started: Date, | ||
valid: Boolean | ||
}] | ||
}; | ||
``` | ||
### Methods | ||
@@ -370,13 +387,6 @@ Methods allow you to provide instance specific functionality in the form of callable methods on a model's instances. These methods have access to the instance, and allow you to call them directly as 1st class members. | ||
## Plugins | ||
Iridium allows you to use plugins which extend the functionality provided by a number of Iridium's components. These plugins can add everything from extra validation behaviour to extra functions for models and instances. Plugins are imported using the `db.register(plugin)` method overload (similar to the way models are loaded), and are declared using the following layout. | ||
Iridium allows you to use plugins which extend the functionality provided by a number of Iridium's components. These plugins can add extra functions for models and instances as well has allowing hooks to be added automatically. Plugins are imported using the `db.register(plugin)` method overload (similar to the way models are loaded), and are declared using the following layout. | ||
```javascript | ||
module.exports = { | ||
validate: function(type, value, propertyName) { | ||
// this.fail(expected, got) | ||
if(type == 'fail') return this.fail('anything', 'something'); | ||
if(type == 'pass') return this.pass; | ||
// this.assert(condition, expected, got) | ||
if(type == 'Positive') return this.assert(value >= 0, 'positive number'); | ||
}, | ||
newModel: function(db, collection, schema, options) { | ||
@@ -383,0 +393,0 @@ this.collection = collection.toLowerCase(); |
@@ -132,3 +132,3 @@ var Database = require('../index.js'); | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -144,3 +144,3 @@ }) | ||
should.exist(billy); | ||
billy.remove(function(err) { | ||
@@ -159,2 +159,26 @@ if(err) return done(err); | ||
}); | ||
it('should set instance isNew to true after removal', function(done) { | ||
model.create({ | ||
username: 'billy', | ||
age: 10, | ||
sessions: [{ | ||
id: 'aaaa', | ||
expires: new Date() | ||
}] | ||
}, function(err, original) { | ||
/// <param name="original" value="new model.Instance({})"/> | ||
if(err) return done(err); | ||
should.exist(original); | ||
original.__state.isNew.should.be.false; | ||
original.remove(function(err) { | ||
should.not.exist(err); | ||
original.__state.isNew.should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -161,0 +185,0 @@ }); |
@@ -44,5 +44,5 @@ var config = require('./config.js'); | ||
'schema', | ||
'schemaValidator', | ||
'database', | ||
'collection', | ||
'extraValidators', | ||
'wrap', | ||
@@ -49,0 +49,0 @@ 'toSource', |
@@ -5,2 +5,3 @@ var Database = require('../index.js'); | ||
var should = require('should'); | ||
var skmatc = require('skmatc'); | ||
@@ -10,6 +11,4 @@ describe('plugins', function() { | ||
var plugin = { | ||
validate: function(schema, value) { | ||
if(schema == 'Positive') | ||
return this.assert(value >= 0, 'positive number'); | ||
} | ||
validate: skmatc.Validator.module(function(schema) { return schema == 'Positive'; }, | ||
function(schema, data, path) { return this.assert(data >= 0); }) | ||
}; | ||
@@ -16,0 +15,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
408
140827
6
33
2589
7
+ Addedskmatc@*
+ Addedskmatc@1.2.3(transitive)