Comparing version 2.0.1 to 2.0.2
# Config changelog | ||
## [2.0.1](http://github.com/ivanoff/2valid/tree/2.0.1) (2016-08-..) | ||
## [2.0.2](http://github.com/ivanoff/2valid/tree/2.0.2) (2016-08-24) | ||
[Full Changelog](http://github.com/ivanoff/2valid/compare/2.0.1...2.0.2) | ||
**What Was Done:** | ||
- add validate unsync mode | ||
## [2.0.1](http://github.com/ivanoff/2valid/tree/2.0.1) (2016-08-24) | ||
[Full Changelog](http://github.com/ivanoff/2valid/compare/2.0.1...2.0.1) | ||
@@ -5,0 +13,0 @@ |
12
index.js
@@ -134,3 +134,3 @@ /*! | ||
// Check if entity pass modelName's validation | ||
exports.validate = function( modelName, entity ) { | ||
exports.validate = function( modelName, entity, next ) { | ||
var modelObject = this.registeredModels[ modelName ]; | ||
@@ -141,5 +141,9 @@ var errors = validateObjectRequired ( | ||
); | ||
if( !Object.keys(errors).length ) errors = {}; | ||
if(errors.text) errors.text = errors.text.join('. '); | ||
return errors; | ||
if(errors && errors.text) errors.text = errors.text.join('. '); | ||
if (typeof next === "function") { | ||
next(Object.keys(errors).length? errors : null); | ||
} else { | ||
return Object.keys(errors).length? errors : {}; | ||
} | ||
} | ||
@@ -146,0 +150,0 @@ |
{ | ||
"name": "2valid", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "JavaScript simple data validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,3 @@ 2valid | ||
v2.0.1 | ||
v2.0.2 | ||
@@ -28,5 +28,10 @@ | ||
// check if object is valid. | ||
// check if object is valid sync | ||
var valid = v.validate('user', userObject); | ||
console.log(valid.text || 'object is valid'); | ||
// check if object is valid with callback | ||
v.validate('user', userObject, function(err) { | ||
console.log(err || 'object is valid'); | ||
}); | ||
``` | ||
@@ -163,3 +168,3 @@ | ||
- registerModel( modelName, modelObject ) - register model modelName with modelObject to check | ||
- validate( modelName, entity ) - validate model modelName with entity. Return empty object if validate is ok. | ||
- validate( modelName, entity [, callback] ) - validate model modelName with entity. Return empty object if validate is ok. As callback, return error as first argument. | ||
- registeredModels - list of registered models | ||
@@ -166,0 +171,0 @@ - showModelsFull() - show full information of registered model |
@@ -5,3 +5,3 @@ 'use strict'; | ||
describe('Validate-me tests', function () { | ||
describe('2valid tests', function () { | ||
@@ -17,3 +17,3 @@ beforeEach(function () { | ||
it('register new model', function() { | ||
it('register new model', function(done) { | ||
this.vm.registerModel( 'user', { | ||
@@ -28,3 +28,6 @@ id: { type: 'uuid', required: true }, | ||
}).should.be.false; | ||
done(); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user', { | ||
@@ -34,4 +37,9 @@ id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
metadata: { tt1:1, tt2:2 }, | ||
}).should.eql({}); | ||
}, function(err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'user', { | ||
@@ -42,24 +50,36 @@ id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
createdAt : new Date(), | ||
}).should.eql({ notFound: [ '.createdAt', '.name.first' ], | ||
text: 'Field .createdAt not found in registered model. Field .name.first not found in registered model' }); | ||
}, function(err) { | ||
err.should.eql({ notFound: [ '.createdAt', '.name.first' ], | ||
text: 'Field .createdAt not found in registered model. Field .name.first not found in registered model' }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate match data', function () { | ||
it('register model with match', function() { | ||
it('register model with match data', function(done) { | ||
this.vm.registerModel( 'user_match', { | ||
name: { type: 'string', match : /^[A-Z]+$/ } | ||
}).should.be.false; | ||
done(); | ||
}); | ||
this.vm.validate( 'user_match', | ||
{ name : 'ILIKECAPS' } | ||
).should.eql({}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_match', { | ||
name : 'ILIKECAPS' | ||
}, function(err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
this.vm.validate( 'user_match', | ||
{ name : 'ILIKEcAPS' } | ||
).should.eql({ notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'user_match', { | ||
name : 'ILIKEcAPS' | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }); | ||
done(); | ||
}); | ||
}); | ||
@@ -69,24 +89,29 @@ | ||
describe('Model to validate integer data', function() { | ||
it('register model with integer', function() { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_int', { | ||
id: { type: 'integer' }, | ||
}).should.be.false; | ||
done(); | ||
}); | ||
it('check integer data', function() { | ||
this.vm.validate( 'user_int', | ||
{ id : 123 } | ||
).should.eql({}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_int', { | ||
id : 123 | ||
}, function(err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
it('check bad integer data', function() { | ||
this.vm.validate( 'user_int', | ||
{ id : 123.1 } | ||
).should.eql({ notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }); | ||
it('check bad integer data', function(done) { | ||
this.vm.validate( 'user_int', { | ||
id : 123.1 | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.id': 'integer' }, text: 'Field .id not matched with type integer' }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -97,14 +122,25 @@ | ||
it('register model with password', function() { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_pass', { | ||
pass: { type: 'password' }, | ||
}).should.be.false; | ||
done(); | ||
}); | ||
this.vm.validate( 'user_pass', | ||
{ pass : 'R2d=' } | ||
).should.eql({}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_pass', { | ||
pass : 'R2d=' | ||
}, function(err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
this.vm.validate( 'user_pass', | ||
{ pass : 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'password' }, text: 'Field .pass not matched with type password' }); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'user_pass', { | ||
pass : 'r2D2' | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.pass': 'password' }, text: 'Field .pass not matched with type password' }); | ||
done(); | ||
}); | ||
}); | ||
@@ -117,18 +153,34 @@ | ||
it('register model with md5', function() { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_pass_md5', { | ||
pass: { type: 'md5' }, | ||
}).should.be.false; | ||
done(); | ||
}); | ||
this.vm.validate( 'user_pass_md5', | ||
{ pass : '4124bc0a9335c27f086f24ba207a4912' } | ||
).should.eql({}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_pass_md5', { | ||
pass : '4124bc0a9335c27f086f24ba207a4912' | ||
}, function(err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
this.vm.validate( 'user_pass_md5', | ||
{ pass : 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
it('string validate failed', function(done) { | ||
this.vm.validate( 'user_pass_md5', { | ||
pass : 'r2D2' | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
done(); | ||
}); | ||
}); | ||
this.vm.validate( 'user_pass_md5', | ||
{ pass : 123 } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
it('integer validate failed', function(done) { | ||
this.vm.validate( 'user_pass_md5', { | ||
pass : 123 | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
done(); | ||
}); | ||
}); | ||
@@ -138,9 +190,2 @@ | ||
describe('Errors while registration', function() { | ||
it('register new bad model', function() { | ||
this.vm.registerModel( ).should.equal('Name is not defined'); | ||
}); | ||
}); | ||
}); |
27195
9
467
244