Skip to content

Commit

Permalink
Switched to skmatc for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Mar 20, 2014
1 parent 1dbaff9 commit f2f1f44
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 315 deletions.
43 changes: 4 additions & 39 deletions Iridium.njsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
<ItemGroup>
<Content Include="chutzpah.json" />
<Content Include=".gitattributes" />
<Content Include=".gitignore" />
<Content Include=".travis.yml" />
<Content Include="LICENCE" />
<Content Include="package.json" />
<Content Include="README.md" />
<Compile Include="index.js" />
Expand All @@ -29,43 +32,6 @@
<Compile Include="lib\Instance.js" />
<Compile Include="lib\Model.js" />
<Compile Include="lib\utils\Inherit.js" />
<Compile Include="nodelib\assert.js" />
<Compile Include="nodelib\buffer.js" />
<Compile Include="nodelib\child_process.js" />
<Compile Include="nodelib\cluster.js" />
<Compile Include="nodelib\console.js" />
<Compile Include="nodelib\core.js" />
<Compile Include="nodelib\crypto.js" />
<Compile Include="nodelib\dgram.js" />
<Compile Include="nodelib\dns.js" />
<Compile Include="nodelib\domain.js" />
<Compile Include="nodelib\events.js" />
<Compile Include="nodelib\fs.js" />
<Compile Include="nodelib\http.js" />
<Compile Include="nodelib\https.js" />
<Compile Include="nodelib\lodash.js" />
<Compile Include="nodelib\mocha.js" />
<Compile Include="nodelib\module.js" />
<Compile Include="nodelib\net.js" />
<Compile Include="nodelib\node.js" />
<Compile Include="nodelib\os.js" />
<Compile Include="nodelib\path.js" />
<Compile Include="nodelib\process.js" />
<Compile Include="nodelib\punycode.js" />
<Compile Include="nodelib\querystring.js" />
<Compile Include="nodelib\readline.js" />
<Compile Include="nodelib\repl.js" />
<Compile Include="nodelib\should.js" />
<Compile Include="nodelib\stream.js" />
<Compile Include="nodelib\stringdecoder.js" />
<Compile Include="nodelib\superagent.js" />
<Compile Include="nodelib\supertest.js" />
<Compile Include="nodelib\tls.js" />
<Compile Include="nodelib\tty.js" />
<Compile Include="nodelib\url.js" />
<Compile Include="nodelib\util.js" />
<Compile Include="nodelib\vm.js" />
<Compile Include="nodelib\zlib.js" />
<Compile Include="test\cache.js" />
<Compile Include="test\config.js" />
<Compile Include="test\diff.js" />
Expand All @@ -91,7 +57,6 @@
<Folder Include="lib" />
<Folder Include="lib\caches" />
<Folder Include="lib\utils" />
<Folder Include="nodelib" />
<Folder Include="test" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ module.exports = function(db) {
};
```

### 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
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.

Expand Down Expand Up @@ -366,17 +383,10 @@ var model = new Model(db, 'modelName', modelSchema, {
```

## 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) {
this.collection = collection.toLowerCase();
this.schema._id = String,
Expand Down
20 changes: 13 additions & 7 deletions example/StringCaseValidationPlugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,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;
5 changes: 2 additions & 3 deletions lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var ObjectID = require('mongodb').ObjectID,
debug = require('debug')('iridium:Instance'),

inherit = require('./utils/Inherit.js'),
validate = require('./utils/validation.js'),
diff = require('./utils/diff.js');


Expand Down Expand Up @@ -129,8 +128,8 @@ Instance.prototype.save = function(conditions, changes, callback) {
}

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);

var original = _.cloneDeep(this.__state.original);
var modified = _.cloneDeep(this.__state.modified);
Expand Down
29 changes: 18 additions & 11 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
ObjectID = require('mongodb').ObjectID,
EventEmitter = require('events').EventEmitter,
Concoction = require('concoction'),
skmatc = require('skmatc'),

inherit = require('./utils/Inherit.js'),
Database = require('./Database.js'),
Instance = require('./Instance.js'),
validate = require('./utils/validation.js'),
NoOpCache = require('./caches/NoOpCache.js');

(require.modules || {}).Model = module.exports = Model;
Expand Down Expand Up @@ -64,6 +64,12 @@ function Model(database, collection, schema, options) {
get: function() { return schema; },
enumerable: false
});

var schemaValidator = new skmatc(schema);
Object.defineProperty(this, 'schemaValidator', {
get: function() { return schemaValidator; },
enumerable: false
});

Object.defineProperty(this, 'options', {
get: function () { return options; },
Expand All @@ -88,15 +94,17 @@ function Model(database, collection, schema, 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;

Object.defineProperty(this, 'extraValidators', {
get: function() { return extraValidators; }
});
if(Array.isArray(validators))
_.forEach(validators, function(validator) { this.schemaValidator.register(validator); }, this);
else
this.schemaValidator.register(validators);
}
}

for(i = 0; i < database.plugins.length; i++) {
if(database.plugins[i].newModel) database.plugins[i].newModel.call(this, database, collection, schema, options);
Expand Down Expand Up @@ -489,9 +497,8 @@ Model.prototype.insert = Model.prototype.create = function (object, options, cal
var postHook = (function (err) {
if (err) return done(err);

// 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);

// Transform the object
this.toSource(obj);
Expand Down
141 changes: 0 additions & 141 deletions lib/utils/validation.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"mongodb": "*",
"skmatc": "*",
"lodash": "*",
"async": "*",
"concoction": "*",
Expand Down
2 changes: 1 addition & 1 deletion test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ describe('orm', function () {
'preprocessor',
'options',
'schema',
'schemaValidator',
'database',
'collection',
'extraValidators',
'wrap',
'toSource',
'fromSource',
Expand Down
7 changes: 3 additions & 4 deletions test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ var Database = require('../index.js');
var Model = Database.Model;
var Instance = Database.Instance;
var should = require('should');
var skmatc = require('skmatc');

describe('plugins', function() {
describe('custom validation', 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); })
};

it('should correctly validate models upon creation', function(done) {
Expand Down

0 comments on commit f2f1f44

Please sign in to comment.