Comparing version 2.2.3 to 2.2.4
277
index.js
@@ -10,3 +10,3 @@ /*! | ||
// All avaible type defenitions are in types.js | ||
exports.types = require( './types' ); | ||
exports.types = require('./types'); | ||
@@ -17,163 +17,170 @@ exports.errors = []; | ||
// Check new model before registration | ||
function deepLook ( obj, types ){ | ||
for( var key in obj ){ | ||
if ( !obj[key].type ) { | ||
obj[key] = deepLook( obj[key], types ); | ||
} else { | ||
if ( ! types[ obj[key].type ] ) { | ||
throw new Error('No type '+ obj[key].type +' in Types: key '+ key); | ||
} | ||
// check for range in new object | ||
if ( typeof obj[key].min !== 'undefined' | ||
&& typeof types[ obj[key].type ].min !== 'undefined' | ||
&& types[ obj[key].type ].min > obj[key].min ) { | ||
throw new Error('Key '+ key +' minimal value ( '+ obj[key].min | ||
+' ) is less than acceptable minimal in Types ( ' | ||
+ types[ obj[key].type ].min + ' )' ); | ||
} | ||
if ( typeof obj[key].max !== 'undefined' | ||
&& typeof types[ obj[key].type ].max !== 'undefined' | ||
&& types[ obj[key].type ].max < obj[key].max ) { | ||
throw new Error('Key '+ key +' maximal value ( '+ obj[key].max | ||
+' ) is in excess of maximal acceptable value in Types ( ' | ||
+ types[ obj[key].type ].max + ' )' ); | ||
} | ||
} | ||
// get properties and methods from Types | ||
for( var key_parent in types[ obj[key].type ] ){ | ||
if ( !obj[ key ][ key_parent ] ) { | ||
obj[ key ][ key_parent ] = types[ obj[key].type ][ key_parent ]; | ||
} | ||
} | ||
function deepLook(obj, types) { | ||
for (var key in obj) { | ||
if (!obj[key].type) { | ||
obj[key] = deepLook(obj[key], types); | ||
} else { | ||
if (!types[obj[key].type]) { | ||
throw new Error('No type ' + obj[key].type + ' in Types: key ' + key); | ||
} | ||
// check for range in new object | ||
if (typeof obj[key].min !== 'undefined' | ||
&& typeof types[obj[key].type].min !== 'undefined' | ||
&& types[obj[key].type].min > obj[key].min) { | ||
throw new Error('Key ' + key + ' minimal value (' + obj[key].min | ||
+ ') is less than acceptable minimal in Types (' | ||
+ types[obj[key].type].min + ')'); | ||
} | ||
if (typeof obj[key].max !== 'undefined' | ||
&& typeof types[obj[key].type].max !== 'undefined' | ||
&& types[obj[key].type].max < obj[key].max) { | ||
throw new Error('Key ' + key + ' maximal value (' + obj[key].max | ||
+ ') is in excess of maximal acceptable value in Types (' | ||
+ types[obj[key].type].max + ')'); | ||
} | ||
} | ||
return obj; | ||
} | ||
// get properties and methods from Types | ||
for (var keyParent in types[obj[key].type]) { | ||
if (!obj[key][keyParent]) { | ||
obj[key][keyParent] = types[obj[key].type][keyParent]; | ||
} | ||
} | ||
} | ||
return obj; | ||
}; | ||
// Register new model. Return validate-me object | ||
// Parameters: modelName - name of the model, modelObject - model object | ||
exports.registerModel = function ( modelName, modelObject ) { | ||
// check for name, object and if model already exists | ||
if(!modelName) return( 'Name is not defined' ); | ||
if(!modelObject) return( 'Model in '+ modelName +' is not defined' ); | ||
if(this.registeredModels[ modelName ]) | ||
return( 'Model '+ modelName +' is already registered' ); | ||
var o = Object.create( modelObject ); | ||
this.registeredModels[ modelName ] = deepLook( o, this.types ); | ||
return false; | ||
} | ||
exports.registerModel = function (modelName, modelObject) { | ||
// check for name, object and if model already exists | ||
if (!modelName) return ('Name is not defined'); | ||
if (!modelObject) return ('Model in ' + modelName + ' is not defined'); | ||
if (this.registeredModels[modelName]) | ||
return ('Model ' + modelName + ' is already registered'); | ||
var o = Object.create(modelObject); | ||
this.registeredModels[modelName] = deepLook(o, this.types); | ||
return false; | ||
}; | ||
// Show information of registered models. All registered models are stored in .registeredModels | ||
// If params.displayEverything is true, module will show additional info | ||
exports.showModels = function( params ) { | ||
var res = []; | ||
if ( typeof( params ) === 'undefined' ) params = { displayEverything: false }; | ||
if ( ! this.registeredModels || Object.keys(this.registeredModels).length === 0 ) { | ||
res.push( 'There is no registered models' ); | ||
} else { | ||
res.push( 'List of registered models' ); | ||
for( var modelName in this.registeredModels ){ | ||
res.push( ' - ' + modelName ); | ||
if( params.displayEverything ) { | ||
for( var key in this.registeredModels[ modelName ] ){ | ||
res.push( ' ' + key + ' : ' + this.registeredModels[ modelName ][ key ].type ); | ||
} | ||
} | ||
exports.showModels = function (params) { | ||
var res = []; | ||
if (typeof (params) === 'undefined') params = { displayEverything: false }; | ||
if (!this.registeredModels || Object.keys(this.registeredModels).length === 0) { | ||
res.push('There is no registered models'); | ||
} else { | ||
res.push('List of registered models'); | ||
for (var modelName in this.registeredModels) { | ||
res.push(' - ' + modelName); | ||
if (params.displayEverything) { | ||
for (var key in this.registeredModels[modelName]) { | ||
res.push(' ' + key + ' : ' + this.registeredModels[modelName][key].type); | ||
} | ||
} | ||
} | ||
return res.join("\n"); | ||
} | ||
} | ||
return res.join('\n'); | ||
}; | ||
// Show expanded information of registared models | ||
exports.showModelsExpanded = function() { | ||
return this.showModels( { displayEverything: true } ); | ||
} | ||
exports.showModelsExpanded = function () { | ||
return this.showModels({ displayEverything: true }); | ||
}; | ||
// check for required fields recursively | ||
function validateObjectRequired ( options, modelObject, entity, parents, errors ) { | ||
if( !options ) options = {}; | ||
for( var key in modelObject ){ | ||
if ( !modelObject[ key ].type ) { | ||
validateObjectRequired ( | ||
options, | ||
modelObject[ key ], | ||
entity[ key ], | ||
parents + '.' + key, | ||
errors ) | ||
} | ||
else if( !options.notRequired && modelObject[ key ].required && ( !entity || !entity[ key ] ) ) { | ||
if(!errors.notFound) errors.notFound = []; | ||
var fieldName = parents + '.' + key; | ||
errors.notFound.push(fieldName); | ||
errors.text.push('Field ' + fieldName + ' not found'); | ||
} | ||
function validateObjectRequired(options, modelObject, entity, parents, errors) { | ||
if (!options) options = {}; | ||
for (var key in modelObject) { | ||
if (!modelObject[key].type) { | ||
validateObjectRequired( | ||
options, | ||
modelObject[key], | ||
entity[key], | ||
parents + '.' + key, | ||
errors); | ||
} else if (!options.notRequired && modelObject[key].required && (!entity || !entity[key])) { | ||
if (!errors.notFound) errors.notFound = []; | ||
var fieldName = parents + '.' + key; | ||
errors.notFound.push(fieldName); | ||
errors.text.push('Field ' + fieldName + ' not found'); | ||
} | ||
return errors; | ||
} | ||
} | ||
return errors; | ||
}; | ||
// check for extra fields and match recursively | ||
function validateObjectEntity ( modelObject, entity, parents, errors ) { | ||
if(!errors) errors = {}; | ||
if(!errors.text) errors.text = []; | ||
if(!parents) parents = []; | ||
function validateObjectEntity(modelObject, entity, parents, errors) { | ||
if (!errors) errors = {}; | ||
if (!errors.text) errors.text = []; | ||
if (!parents) parents = []; | ||
for( var key in entity ){ | ||
var fieldName = parents + '.' + key; | ||
if ( !modelObject[ key ] ) { | ||
if(!errors.notRequired) errors.notRequired = []; | ||
errors.notRequired.push(fieldName); | ||
errors.text.push('Field ' + fieldName + ' not required'); | ||
} | ||
else if ( !modelObject[ key ].type ) { | ||
validateObjectEntity ( modelObject[ key ], entity[ key ], [parents, key], errors ) | ||
} | ||
else if( !modelObject[ key ].check( entity[ key ] ) ) { | ||
if(!errors.notMatched) errors.notMatched = {}; | ||
errors.notMatched[fieldName] = modelObject[key].type; | ||
errors.text.push('Field ' + fieldName + ' not matched with type ' + modelObject[key].type); | ||
} | ||
for (var key in entity) { | ||
var fieldName = parents + '.' + key; | ||
if (!modelObject[key]) { | ||
if (!errors.notRequired) errors.notRequired = []; | ||
errors.notRequired.push(fieldName); | ||
errors.text.push('Field ' + fieldName + ' not required'); | ||
} else if (!modelObject[key].type) { | ||
validateObjectEntity(modelObject[key], entity[key], [parents, key], errors); | ||
} else if (!modelObject[key].check(entity[key])) { | ||
if (!errors.notMatched) errors.notMatched = {}; | ||
errors.notMatched[fieldName] = modelObject[key].type; | ||
errors.text.push('Field ' + fieldName + ' not matched with type ' + modelObject[key].type); | ||
} | ||
return errors; | ||
} | ||
} | ||
return errors; | ||
}; | ||
// Check if entity pass modelName's validation | ||
exports.validate = function( modelName, entity, options, next ) { | ||
if(typeof options === "function") { | ||
next = options; | ||
options = {}; | ||
} | ||
exports.validate = function (modelName, entity, options, next) { | ||
if (typeof options === 'function') { | ||
next = options; | ||
options = {}; | ||
} | ||
var modelObject = this.registeredModels[modelName]; | ||
var modelObject = this.registeredModels[modelName]; | ||
if(typeof(modelName) === 'object') { | ||
modelObject = deepLook( modelName, this.types ) | ||
} else if(this.types[modelName]) { | ||
var result = this.types[modelName].check(entity)? null : {notMatched:modelName}; | ||
return typeof next === "function"? next(result) : result; | ||
} | ||
if (typeof modelName === 'object') { | ||
modelObject = deepLook(modelName, this.types); | ||
} else if (this.types[modelName]) { | ||
var result = this.types[modelName].check(entity) ? null : { notMatched: modelName }; | ||
return typeof next === 'function' ? next(result) : result; | ||
} | ||
var errors = validateObjectRequired ( | ||
options, modelObject, entity, [], | ||
validateObjectEntity ( modelObject, entity ) | ||
); | ||
if(!errors.text[0]) errors = {}; | ||
if(errors && errors.text) errors.text = errors.text.join('. '); | ||
var errors = validateObjectRequired( | ||
options, modelObject, entity, [], | ||
validateObjectEntity(modelObject, entity) | ||
); | ||
if (!errors.text[0]) 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 : {}; | ||
} | ||
} | ||
if (typeof next === 'function') { | ||
next(Object.keys(errors).length ? errors : null); | ||
} else { | ||
return Object.keys(errors).length ? errors : {}; | ||
} | ||
}; | ||
exports.getAllTypes = function() { | ||
exports.getAllTypes = function () { | ||
return Object.keys(this.types); | ||
} | ||
}; | ||
exports.getExample = function(type) { | ||
return this.types[type]? this.types[type].example : undefined; | ||
} | ||
exports.getExample = function (type) { | ||
return this.types[type] ? this.types[type].example : undefined; | ||
}; | ||
// 'Forget' about all registered models | ||
exports.dispose = function() { | ||
this.registeredModels = []; | ||
return 1; | ||
} | ||
exports.dispose = function () { | ||
this.registeredModels = []; | ||
return 1; | ||
}; | ||
{ | ||
"name": "2valid", | ||
"version": "2.2.3", | ||
"version": "2.2.4", | ||
"description": "JavaScript simple data validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ | ||
[![MIT License][license-image]][license-url] | ||
[![js-standard-style][standard-style-image]][standard-style-url] | ||
[![Build Status: Linux][travis-image]][travis-url] | ||
@@ -15,3 +16,3 @@ [![Build Status: Windows][appveyor-image]][appveyor-url] | ||
v2.2.2 | ||
v2.2.4 | ||
@@ -384,2 +385,5 @@ | ||
[standard-style-image]: https://img.shields.io/badge/code%20style-airbnb-blue.svg?style=flat | ||
[standard-style-url]: https://github.com/airbnb/javascript | ||
[npm-url]: https://npmjs.org/package/2valid | ||
@@ -386,0 +390,0 @@ [npm-version-image]: http://img.shields.io/npm/v/2valid.svg?style=flat |
@@ -8,70 +8,74 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('Errors while registration', function() { | ||
it('register new bad model', function() { | ||
this.vm.registerModel( ).should.equal('Name is not defined'); | ||
this.vm.registerModel( 'noObj' ).should.equal('Model in noObj is not defined'); | ||
}); | ||
describe('Errors while registration', function () { | ||
it('register new bad model', function () { | ||
this.vm.registerModel().should.equal('Name is not defined'); | ||
this.vm.registerModel('noObj').should.equal('Model in noObj is not defined'); | ||
}); | ||
it('register same name model', function() { | ||
this.vm.registerModel( 'sameName', { id: {type: 'integer'} } ); | ||
this.vm.registerModel( 'sameName', { name: {type: 'string'} } ).should.equal('Model sameName is already registered'); | ||
}); | ||
}); | ||
it('register same name model', function () { | ||
this.vm.registerModel('sameName', { id: { type: 'integer' } }); | ||
this.vm.registerModel('sameName', { name: { type: 'string' } }) | ||
.should.equal('Model sameName is already registered'); | ||
}); | ||
}); | ||
describe('Simple model to validate', function () { | ||
var userModel = { | ||
id: {type: 'notype'}, | ||
}; | ||
id: { type: 'notype' }, | ||
}; | ||
it('notype model error', function(done) { | ||
it('notype model error', function (done) { | ||
try { | ||
this.vm.validate( userModel, { id: 111 }, function() { should.fail() }); | ||
this.vm.validate(userModel, { id: 111 }, function () { should.fail(); }); | ||
} | ||
catch (err) { | ||
err.should.eql(Error('No type notype in Types: key id')); | ||
done(); | ||
err.should.eql(Error('No type notype in Types: key id')); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Min length validate', function () { | ||
var userModel = { | ||
password: {type: 'password', min: 1}, | ||
}; | ||
it('in registerModel', function(done) { | ||
password: { type: 'password', min: 1 }, | ||
}; | ||
it('in registerModel', function (done) { | ||
try { | ||
this.vm.registerModel( 'userPassMin', userModel ); | ||
this.vm.registerModel('userPassMin', userModel); | ||
} | ||
catch (err) { | ||
(err instanceof Error).should.equal(true); | ||
String(err).should.eql('Error: Key password minimal value ( 1 ) is less than acceptable minimal in Types ( 4 )'); | ||
done(); | ||
(err instanceof Error).should.equal(true); | ||
String(err).should.eql('Error: Key password minimal value (1) ' | ||
+ 'is less than acceptable minimal in Types (4)'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Max length validate', function () { | ||
var userModel = { | ||
password: {type: 'password', max: 100001}, | ||
}; | ||
it('in registerModel', function(done) { | ||
password: { type: 'password', max: 100001 }, | ||
}; | ||
it('in registerModel', function (done) { | ||
try { | ||
this.vm.registerModel( 'userPassMax', userModel ); | ||
this.vm.registerModel('userPassMax', userModel); | ||
} | ||
catch (err) { | ||
(err instanceof Error).should.equal(true); | ||
String(err).should.eql('Error: Key password maximal value ( 100001 ) is in excess of maximal acceptable value in Types ( 1000 )'); | ||
done(); | ||
(err instanceof Error).should.equal(true); | ||
String(err).should.eql('Error: Key password maximal value (100001) ' | ||
+ 'is in excess of maximal acceptable value in Types (1000)'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,11 +8,12 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('get example', function () { | ||
it('check types list', function(done) { | ||
it('check types list', function (done) { | ||
this.vm.getAllTypes().should.be.instanceof(Array);; | ||
@@ -22,22 +23,23 @@ this.vm.getAllTypes().should.contain('string'); | ||
done(); | ||
}); | ||
}); | ||
it('check all', function(done) { | ||
it('check all', function (done) { | ||
var types = this.vm.getAllTypes(); | ||
for(var i = 0; i < types.length; i++) { | ||
for (var i = 0; i < types.length; i++) { | ||
var example = this.vm.getExample(types[i]); | ||
this.vm.validate(types[i], example, function(err){ | ||
this.vm.validate(types[i], example, function (err) { | ||
(err === null).should.be.true; | ||
}); | ||
} | ||
done(); | ||
}); | ||
}); | ||
it('check notype', function(done) { | ||
it('check notype', function (done) { | ||
(this.vm.getExample('notype') === undefined).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,25 +8,26 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('Simple validate', function () { | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'integer', 123, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('integer', 123, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'integer', 'aaa', function(err) { | ||
it('validate failed', function (done) { | ||
this.vm.validate('integer', 'aaa', function (err) { | ||
err.should.eql({ notMatched: 'integer' }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -36,331 +37,338 @@ describe('Simple model to validate', function () { | ||
var userModel = { | ||
id: {type: 'integer', required: true}, | ||
name: {type: 'string', required: true} | ||
}; | ||
id: { type: 'integer', required: true }, | ||
name: { type: 'string', required: true }, | ||
}; | ||
it('validate passed', function(done) { | ||
this.vm.validate( userModel, | ||
it('validate passed', function (done) { | ||
this.vm.validate(userModel, | ||
{ id: 111, name: 'Max Validator' }, | ||
function(err) { | ||
function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( userModel, | ||
it('validate failed', function (done) { | ||
this.vm.validate(userModel, | ||
{ id: 'Max', secondName: 'Validator' }, | ||
function(err) { | ||
function (err) { | ||
err.should.eql({ notMatched: { '.id': 'integer' }, | ||
text: 'Field .id not matched with type integer. Field .secondName not required. Field .name not found', | ||
notRequired: [ '.secondName' ], notFound: [ '.name' ] }); | ||
text: 'Field .id not matched with type integer. Field .secondName ' | ||
+ 'not required. Field .name not found', | ||
notRequired: ['.secondName'], notFound: ['.name'], }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( userModel, | ||
it('validate failed', function (done) { | ||
this.vm.validate(userModel, | ||
{ id: 'Max', name: 123 }, | ||
function(err) { | ||
err.should.eql({ notMatched: { '.id': 'integer', '.name' : 'string' }, | ||
text: 'Field .id not matched with type integer. Field .name not matched with type string' | ||
}); | ||
function (err) { | ||
err.should.eql({ notMatched: { '.id': 'integer', '.name': 'string' }, | ||
text: 'Field .id not matched with type integer. Field .name ' | ||
+ 'not matched with type string', | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( userModel, | ||
it('validate failed', function (done) { | ||
this.vm.validate(userModel, | ||
{ secondName: 'Validator', pages: 12 }, | ||
function(err) { | ||
function (err) { | ||
err.should.eql({ | ||
text: 'Field .secondName not required. Field .pages not required. Field .id not found. Field .name not found', | ||
notRequired: [ '.secondName', '.pages' ], notFound: [ '.id', '.name' ] }); | ||
text: 'Field .secondName not required. Field .pages ' | ||
+ 'not required. Field .id not found. Field .name not found', | ||
notRequired: ['.secondName', '.pages'], notFound: ['.id', '.name'], }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate nested and required data', function () { | ||
it('register new model', function(done) { | ||
this.vm.registerModel( 'user', { | ||
it('register new model', function (done) { | ||
this.vm.registerModel('user', { | ||
id: { type: 'uuid', required: true }, | ||
name: { | ||
first : { type: 'string', min: 1, max: 256, required: true }, | ||
last : { type: 'string', min: 1, max: 256 }, | ||
}, | ||
first: { type: 'string', min: 1, max: 256, required: true }, | ||
last: { type: 'string', min: 1, max: 256 }, | ||
}, | ||
email: { type: 'email' }, | ||
metadata: { type: 'object' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { first : 'Alex', last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { first: 'Alex', last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'user', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
createdAt : new Date(), | ||
}, function(err) { | ||
err.should.eql({ notFound: [ '.name.first' ], notRequired: [ '.createdAt' ], | ||
text: 'Field .createdAt not required. Field .name.first not found' }); | ||
it('validate failed', function (done) { | ||
this.vm.validate('user', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
createdAt: new Date(), | ||
}, function (err) { | ||
err.should.eql({ notFound: ['.name.first'], notRequired: ['.createdAt'], | ||
text: 'Field .createdAt not required. Field .name.first not found', }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate not required data', function () { | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { first : 'Alex', last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}, {notRequired: 1}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { first: 'Alex', last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}, { notRequired: 1 }, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate failed', function(done) { | ||
this.vm.validate( 'user', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}, {notRequired: 1}, function(err) { | ||
it('validate failed', function (done) { | ||
this.vm.validate('user', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}, { notRequired: 1 }, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate match data', function () { | ||
it('register model with match data', function(done) { | ||
this.vm.registerModel( 'user_match', { | ||
name: { type: 'string', match : /^[A-Z]+$/ } | ||
}).should.be.false; | ||
it('register model with match data', function (done) { | ||
this.vm.registerModel('user_match', { | ||
name: { type: 'string', match: /^[A-Z]+$/ }, | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_match', { | ||
name : 'ILIKECAPS' | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_match', { | ||
name: 'ILIKECAPS', | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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' }); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate integer data', function() { | ||
describe('Model to validate integer data', function () { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_int', { | ||
it('register model with integer', function (done) { | ||
this.vm.registerModel('user_int', { | ||
id: { type: 'integer' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_int', { | ||
id : 123 | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_int', { | ||
id: 123, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate passed string', function(done) { | ||
this.vm.validate( 'user_int', { | ||
id : "123" | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.id': 'integer' }, text: 'Field .id not matched with type integer' }); | ||
it('validate passed string', function (done) { | ||
this.vm.validate('user_int', { | ||
id: '123', | ||
}, function (err) { | ||
err.should.eql({ notMatched: { '.id': 'integer' }, | ||
text: 'Field .id not matched with type integer', }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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' }); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate float data', function () { | ||
describe('Model to validate float data', function() { | ||
it('register model with float', function(done) { | ||
this.vm.registerModel( 'user_float', { | ||
it('register model with float', function (done) { | ||
this.vm.registerModel('user_float', { | ||
id: { type: 'float' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_float', { | ||
id : 123.321 | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_float', { | ||
id: 123.321, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate passed integer', function(done) { | ||
this.vm.validate( 'user_float', { | ||
id : 123 | ||
}, function(err) { | ||
it('validate passed integer', function (done) { | ||
this.vm.validate('user_float', { | ||
id: 123, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate passed string', function(done) { | ||
this.vm.validate( 'user_float', { | ||
id : "123.321" | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.id': 'float' }, text: 'Field .id not matched with type float' }); | ||
it('validate passed string', function (done) { | ||
this.vm.validate('user_float', { | ||
id: '123.321', | ||
}, function (err) { | ||
err.should.eql({ notMatched: { '.id': 'float' }, | ||
text: 'Field .id not matched with type float', }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate boolean data', function () { | ||
describe('Model to validate boolean data', function() { | ||
it('register model with boolean', function(done) { | ||
this.vm.registerModel( 'user_bool', { | ||
it('register model with boolean', function (done) { | ||
this.vm.registerModel('user_bool', { | ||
isAlive: { type: 'boolean' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_bool', { | ||
isAlive : true | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_bool', { | ||
isAlive: true, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('validate passed string', function(done) { | ||
this.vm.validate( 'user_bool', { | ||
isAlive : false | ||
}, function(err) { | ||
it('validate passed string', function (done) { | ||
this.vm.validate('user_bool', { | ||
isAlive: false, | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('check bad integer data', function(done) { | ||
this.vm.validate( 'user_bool', { | ||
isAlive : 123 | ||
}, function(err) { | ||
err.should.eql({ notMatched: { '.isAlive': 'boolean' }, text: 'Field .isAlive not matched with type boolean' }); | ||
it('check bad integer data', function (done) { | ||
this.vm.validate('user_bool', { | ||
isAlive: 123, | ||
}, function (err) { | ||
err.should.eql({ notMatched: { '.isAlive': 'boolean' }, | ||
text: 'Field .isAlive not matched with type boolean', }); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate password', function () { | ||
describe('Model to validate password', function() { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_pass', { | ||
it('register model with integer', function (done) { | ||
this.vm.registerModel('user_pass', { | ||
pass: { type: 'password' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_pass', { | ||
pass : 'R2d=' | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_pass', { | ||
pass: 'R2d=', | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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' }); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate md5', function () { | ||
describe('Model to validate md5', function() { | ||
it('register model with integer', function(done) { | ||
this.vm.registerModel( 'user_pass_md5', { | ||
it('register model with integer', function (done) { | ||
this.vm.registerModel('user_pass_md5', { | ||
pass: { type: 'md5' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('validate passed', function(done) { | ||
this.vm.validate( 'user_pass_md5', { | ||
pass : '4124bc0a9335c27f086f24ba207a4912' | ||
}, function(err) { | ||
it('validate passed', function (done) { | ||
this.vm.validate('user_pass_md5', { | ||
pass: '4124bc0a9335c27f086f24ba207a4912', | ||
}, function (err) { | ||
should.not.exist(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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' }); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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' }); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,49 +8,50 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('delete all models', function() { | ||
it('dispose', function(done) { | ||
describe('delete all models', function () { | ||
it('dispose', function (done) { | ||
this.vm.dispose().should.equal(1); | ||
this.vm.showModels().should.equal('There is no registered models'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('show all models', function() { | ||
describe('show all models', function () { | ||
it('showModels register', function(done) { | ||
this.vm.registerModel( 'userToShow', { | ||
it('showModels register', function (done) { | ||
this.vm.registerModel('userToShow', { | ||
id: { type: 'uuid', required: true }, | ||
name: { | ||
first : { type: 'string', min: 1, max: 256, required: true }, | ||
last : { type: 'string', min: 1, max: 256 }, | ||
}, | ||
first: { type: 'string', min: 1, max: 256, required: true }, | ||
last: { type: 'string', min: 1, max: 256 }, | ||
}, | ||
email: { type: 'email' }, | ||
metadata: { type: 'object' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
done(); | ||
}); | ||
}); | ||
it('showModelsExpanded', function(done) { | ||
it('showModelsExpanded', function (done) { | ||
this.vm.showModelsExpanded().should.match(/- userToShow/); | ||
this.vm.showModelsExpanded().should.match(/email/); | ||
done(); | ||
}); | ||
}); | ||
it('showModels', function(done) { | ||
it('showModels', function (done) { | ||
this.vm.showModels().should.match(/- userToShow/); | ||
done(); | ||
}); | ||
}); | ||
it('showModels with params', function(done) { | ||
this.vm.showModels({displayEverything: true}).should.match(/- userToShow/); | ||
this.vm.showModels({displayEverything: true}).should.match(/email/); | ||
it('showModels with params', function (done) { | ||
this.vm.showModels({ displayEverything: true }).should.match(/- userToShow/); | ||
this.vm.showModels({ displayEverything: true }).should.match(/email/); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
243
test/sync.js
@@ -8,17 +8,18 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('Model to validate nested and required data', function () { | ||
it('register new model', function() { | ||
this.vm.registerModel( 'user_sync', { | ||
it('register new model', function () { | ||
this.vm.registerModel('user_sync', { | ||
id: { type: 'uuid', required: true }, | ||
name: { | ||
first : { type: 'string', min: 1, max: 256, required: true }, | ||
last : { type: 'string', min: 1, max: 256 }, | ||
}, | ||
first: { type: 'string', min: 1, max: 256, required: true }, | ||
last: { type: 'string', min: 1, max: 256 }, | ||
}, | ||
email: { type: 'email' }, | ||
@@ -29,159 +30,157 @@ birthday: { type: 'date' }, | ||
metadata: { type: 'object' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
this.vm.validate( 'user_sync', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { first : 'Alex', last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}).should.eql({}); | ||
this.vm.validate('user_sync', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { first: 'Alex', last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}).should.eql({}); | ||
this.vm.validate( 'user_sync', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
email : 'max.validator@my.site', | ||
birthday : new Date('1980-04-01'), | ||
childrens : ['Maria', 'Alexandra'], | ||
this.vm.validate('user_sync', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
email: 'max.validator@my.site', | ||
birthday: new Date('1980-04-01'), | ||
childrens: ['Maria', 'Alexandra'], | ||
alive: true, | ||
createdAt : new Date(), | ||
}).should.eql({ notFound: [ '.name.first' ], notRequired: [ '.createdAt' ], | ||
text: 'Field .createdAt not required. Field .name.first not found' }); | ||
}); | ||
createdAt: new Date(), | ||
}).should.eql({ notFound: ['.name.first'], notRequired: ['.createdAt'], | ||
text: 'Field .createdAt not required. Field .name.first not found', }); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate notRequired data', function () { | ||
it('register new model', function() { | ||
this.vm.validate( 'user_sync', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { first : 'Alex', last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}, {notRequired: 1} ).should.eql({}); | ||
it('register new model', function () { | ||
this.vm.validate('user_sync', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { first: 'Alex', last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}, { notRequired: 1 }).should.eql({}); | ||
this.vm.validate( 'user_sync', { | ||
id : '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name : { last: 'Validates', }, | ||
metadata: { tt1:1, tt2:2 }, | ||
}, {notRequired: 1} ).should.eql({}); | ||
}); | ||
this.vm.validate('user_sync', { | ||
id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', | ||
name: { last: 'Validates', }, | ||
metadata: { tt1: 1, tt2: 2 }, | ||
}, { notRequired: 1 }).should.eql({}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate match data', function () { | ||
it('register model with match', function() { | ||
this.vm.registerModel( 'user_match_sync', { | ||
name: { type: 'string', match : /^[A-Z]+$/ } | ||
}).should.be.false; | ||
it('register model with match', function () { | ||
this.vm.registerModel('user_match_sync', { | ||
name: { type: 'string', match: /^[A-Z]+$/ }, | ||
}).should.be.false; | ||
this.vm.validate( 'user_match_sync', | ||
{ name : 'ILIKECAPS' } | ||
this.vm.validate('user_match_sync', | ||
{ name: 'ILIKECAPS' } | ||
).should.eql({}); | ||
this.vm.validate( 'user_match_sync', | ||
{ name : 'ILIKEcAPS' } | ||
).should.eql({ notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }); | ||
}); | ||
this.vm.validate('user_match_sync', | ||
{ name: 'ILIKEcAPS' } | ||
).should.eql({ notMatched: { '.name': 'string' }, | ||
text: 'Field .name not matched with type string', }); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate integer data', function () { | ||
describe('Model to validate integer data', function() { | ||
it('register model with integer', function() { | ||
this.vm.registerModel( 'user_int_sync', { | ||
it('register model with integer', function () { | ||
this.vm.registerModel('user_int_sync', { | ||
id: { type: 'integer' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
it('check integer data', function() { | ||
this.vm.validate( 'user_int_sync', | ||
{ id : 123 } | ||
).should.eql({}); | ||
}); | ||
it('check integer data', function () { | ||
this.vm.validate('user_int_sync', | ||
{ id: 123 } | ||
).should.eql({}); | ||
}); | ||
it('check bad integer data', function() { | ||
this.vm.validate( 'user_int_sync', | ||
{ id : 123.1 } | ||
).should.eql({ notMatched: { '.name': 'string' }, text: 'Field .name not matched with type string' }); | ||
}); | ||
}); | ||
it('check bad integer data', function () { | ||
this.vm.validate('user_int_sync', | ||
{ id: 123.1 } | ||
).should.eql({ notMatched: { '.name': 'string' }, | ||
text: 'Field .name not matched with type string', }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate integer data', function () { | ||
describe('Model to validate integer data', function() { | ||
it('register model with boolean', function() { | ||
this.vm.registerModel( 'user_bool_sync', { | ||
it('register model with boolean', function () { | ||
this.vm.registerModel('user_bool_sync', { | ||
isAlive: { type: 'integer' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
it('check boolean data', function() { | ||
this.vm.validate( 'user_bool_sync', | ||
{ isAlive : true } | ||
).should.eql({}); | ||
}); | ||
it('check boolean data', function () { | ||
this.vm.validate('user_bool_sync', | ||
{ isAlive: true } | ||
).should.eql({}); | ||
}); | ||
it('check boolean data', function() { | ||
this.vm.validate( 'user_bool_sync', | ||
{ isAlive : false } | ||
).should.eql({}); | ||
}); | ||
it('check boolean data', function () { | ||
this.vm.validate('user_bool_sync', | ||
{ isAlive: false } | ||
).should.eql({}); | ||
}); | ||
it('check bad boolean data', function() { | ||
this.vm.validate( 'user_bool_sync', | ||
{ isAlive : 123 } | ||
).should.eql({ notMatched: { '.isAlive': 'boolean' }, text: 'Field .isAlive not matched with type boolean' }); | ||
}); | ||
}); | ||
it('check bad boolean data', function () { | ||
this.vm.validate('user_bool_sync', | ||
{ isAlive: 123 } | ||
).should.eql({ notMatched: { '.isAlive': 'boolean' }, | ||
text: 'Field .isAlive not matched with type boolean', }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate password', function () { | ||
describe('Model to validate password', function() { | ||
it('register model with password', function() { | ||
this.vm.registerModel( 'user_pass_sync', { | ||
it('register model with password', function () { | ||
this.vm.registerModel('user_pass_sync', { | ||
pass: { type: 'password' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
this.vm.validate( 'user_pass_sync', | ||
{ pass : 'R2d=' } | ||
this.vm.validate('user_pass_sync', | ||
{ pass: 'R2d=' } | ||
).should.eql({}); | ||
this.vm.validate( 'user_pass_sync', | ||
{ pass : 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'password' }, text: 'Field .pass not matched with type password' }); | ||
}); | ||
this.vm.validate('user_pass_sync', | ||
{ pass: 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'password' }, | ||
text: 'Field .pass not matched with type password', }); | ||
}); | ||
}); | ||
}); | ||
describe('Model to validate md5', function () { | ||
describe('Model to validate md5', function() { | ||
it('register model with md5', function() { | ||
this.vm.registerModel( 'user_pass_md5_sync', { | ||
it('register model with md5', function () { | ||
this.vm.registerModel('user_pass_md5_sync', { | ||
pass: { type: 'md5' }, | ||
}).should.be.false; | ||
}).should.be.false; | ||
this.vm.validate( 'user_pass_md5_sync', | ||
{ pass : '4124bc0a9335c27f086f24ba207a4912' } | ||
this.vm.validate('user_pass_md5_sync', | ||
{ pass: '4124bc0a9335c27f086f24ba207a4912' } | ||
).should.eql({}); | ||
this.vm.validate( 'user_pass_md5_sync', | ||
{ pass : 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
this.vm.validate('user_pass_md5_sync', | ||
{ pass: 'r2D2' } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, | ||
text: 'Field .pass not matched with type md5', }); | ||
this.vm.validate( 'user_pass_md5_sync', | ||
{ pass : 123 } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, text: 'Field .pass not matched with type md5' }); | ||
}); | ||
this.vm.validate('user_pass_md5_sync', | ||
{ pass: 123 } | ||
).should.eql({ notMatched: { '.pass': 'md5' }, | ||
text: 'Field .pass not matched with type md5', }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,11 +8,12 @@ 'use strict'; | ||
beforeEach(function () { | ||
this.vm = require('../index') | ||
}) | ||
this.vm = require('../index'); | ||
}); | ||
afterEach(function () { | ||
this.vm = null | ||
}) | ||
this.vm = null; | ||
}); | ||
describe('Simple validate', function () { | ||
it('name', function(done) { | ||
it('name', function (done) { | ||
this.vm.validate().should.eql({}); | ||
@@ -22,10 +23,11 @@ this.vm.validate('some').should.eql({}); | ||
done(); | ||
}); | ||
it('object', function(done) { | ||
this.vm.validate({id:{type:'integer'}}).should.eql({}); | ||
}); | ||
it('object', function (done) { | ||
this.vm.validate({ id: { type: 'integer' } }).should.eql({}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
159
types.js
@@ -5,21 +5,22 @@ /**** List of types to validate ****/ | ||
string : { // string properties and methods | ||
min : 0, // string.min Minimum length of the string | ||
max : Infinity, // string.max Maximum length of the string | ||
check : function( string ){ // string.check check sting type and size | ||
string: { // string properties and methods | ||
min: 0, // string.min Minimum length of the string | ||
max: Infinity, // string.max Maximum length of the string | ||
check: function (string) { // string.check check sting type and size | ||
return (( typeof string === 'string' || string instanceof String ) | ||
return ((typeof string === 'string' || string instanceof String) | ||
&& string.length >= this.min | ||
&& string.length <= this.max | ||
&& ( !this.match || string.match( this.match ) ) | ||
&& (!this.match || string.match(this.match)) | ||
); | ||
}, | ||
example : 'Test string', | ||
}, | ||
}, | ||
integer : { // number properties and methods | ||
min : -Infinity, // number.min Minimum number value | ||
max : Infinity, // number.max Maximum number value | ||
check : function( number ){ // number.check check number type and size | ||
example: 'Test string', | ||
}, | ||
integer: { // number properties and methods | ||
min: -Infinity, // number.min Minimum number value | ||
max: Infinity, // number.max Maximum number value | ||
check: function (number) { // number.check check number type and size | ||
return typeof number === 'number' | ||
@@ -29,82 +30,92 @@ && number >= this.min | ||
&& !number.toString().match(/\./); | ||
}, | ||
example : 123, | ||
}, | ||
}, | ||
float : { // number properties and methods | ||
min : -Infinity, // number.min Minimum number value | ||
max : Infinity, // number.max Maximum number value | ||
check : function( number ){ // number.check check number type and size | ||
example: 123, | ||
}, | ||
float: { // number properties and methods | ||
min: -Infinity, // number.min Minimum number value | ||
max: Infinity, // number.max Maximum number value | ||
check: function (number) { // number.check check number type and size | ||
return typeof number === 'number' | ||
&& number >= this.min | ||
&& number >= this.min | ||
&& number <= this.max; | ||
}, | ||
example : 123.456, | ||
}, | ||
}, | ||
boolean : { | ||
check : function( bool ){ | ||
example: 123.456, | ||
}, | ||
boolean: { | ||
check: function (bool) { | ||
return typeof bool === 'boolean'; | ||
}, | ||
example : true, | ||
}, | ||
}, | ||
date : { // date methods | ||
check : function( date ){ // date.check Maximum length of the string | ||
example: true, | ||
}, | ||
date: { // date methods | ||
check: function (date) { // date.check Maximum length of the string | ||
return date instanceof Date && typeof date.getMonth === 'function'; | ||
}, | ||
example : new Date(), | ||
}, | ||
}, | ||
email : { // validate e-mail | ||
match : /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+/i, | ||
check : function( email ){ | ||
return email.toString().match( this.match ); | ||
}, | ||
example : 'news@site.com', | ||
}, | ||
example: new Date(), | ||
}, | ||
password : { | ||
min : 4, // minimum length of the password | ||
max : 1000, // maximum length of the password | ||
email: { // validate e-mail | ||
match: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+/i, | ||
check: function (email) { | ||
return email.toString().match(this.match); | ||
}, | ||
example: 'news@site.com', | ||
}, | ||
password: { | ||
min: 4, // minimum length of the password | ||
max: 1000, // maximum length of the password | ||
// at least one caps and one small letter, digit and special | ||
match : /^.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$/, | ||
check : function( password ) { // check password type and size | ||
match: /^.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).*$/, | ||
check: function (password) { // check password type and size | ||
return typeof password === 'string' | ||
&& password.length >= this.min | ||
&& password.length <= this.max | ||
&& password.match( this.match ) | ||
}, | ||
example : 'JHtG<3', | ||
}, | ||
&& password.match(this.match); | ||
}, | ||
md5 : { | ||
match : /^[\da-f]{32}$/, | ||
check : function( md5 ){ | ||
return md5 && md5.toString().match( this.match ); | ||
}, | ||
example : 'c4ca4238a0b923820dcc509a6f75849b', | ||
}, | ||
example: 'JHtG<3', | ||
}, | ||
uuid : { // uuid methods. uuid.check returns true if parameter looks like UUID, false otherwise | ||
match : /^[\da-z]{8}-[\da-z]{4}-4[\da-z]{3}-[\da-z]{4}-[\da-z]{12}$/, | ||
check : function( uuid ){ | ||
return uuid && uuid.toString().match( this.match ); | ||
}, | ||
example : '4ca0025f-9618-4328-811e-f030b9c82af9', | ||
}, | ||
md5: { | ||
match: /^[\da-f]{32}$/, | ||
check: function (md5) { | ||
return md5 && md5.toString().match(this.match); | ||
}, | ||
array : { | ||
check : function( arr ){ | ||
example: 'c4ca4238a0b923820dcc509a6f75849b', | ||
}, | ||
uuid: { // uuid methods. uuid.check returns true if parameter looks like UUID, false otherwise | ||
match: /^[\da-z]{8}-[\da-z]{4}-4[\da-z]{3}-[\da-z]{4}-[\da-z]{12}$/, | ||
check: function (uuid) { | ||
return uuid && uuid.toString().match(this.match); | ||
}, | ||
example: '4ca0025f-9618-4328-811e-f030b9c82af9', | ||
}, | ||
array: { | ||
check: function (arr) { | ||
return typeof arr === 'object' && Array.isArray(arr); | ||
}, | ||
example : [1, 'a', 'b'], | ||
}, | ||
}, | ||
object : { | ||
check : function(){ return 1 }, | ||
example : {}, | ||
}, | ||
example: [1, 'a', 'b'], | ||
}, | ||
} | ||
object: { | ||
check: function () { return 1; }, | ||
example: {}, | ||
}, | ||
}; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
49531
871
398
1