Browse files

The module is Synchronous from now on!

  • Loading branch information...
1 parent 371de2a commit dc128cb6a27272c96a883404167cd78ccfbc9651 @simov committed Apr 11, 2013
Showing with 44 additions and 64 deletions.
  1. +15 −23 README.md
  2. +2 −2 lib/data-type.js
  3. +4 −5 lib/validator.js
  4. +1 −1 package.json
  5. +5 −11 test/data-type.js
  6. +17 −22 test/index.js
View
38 README.md
@@ -11,11 +11,10 @@ Validates web forms input against mysql database.
```js
var validator = require('mysql-validator');
-validator.check('doh winning!', 'varchar(45)', function (err) {
- if (err) {
- console.log(err.message);
- }
-});
+var err = validator.check('doh winning!', 'varchar(45)');
+if (err) {
+ console.log(err.message);
+}
```
The first parameter is the posted input data and the second is the mysql data type of the field in your database.
@@ -53,33 +52,26 @@ Then your router may look like this.
```js
app.post('/save', function (req, res) {
- // all submitted field names
- var keys = Object.keys(req.body);
// we'll store all validation errors here
var errors = [];
- // our handy field-type mapping (this may be the result of 'describe table')
+ // field-type mapping (this may be the result of 'describe table')
var types = {
name: 'varchar(10)',
cache: 'decimal(6,2) unsigned',
date: 'datetime'
}
// loop through the submitted fields and validate them
- function loop (i, cb) {
- if (i == keys.length) return cb();
- validator.check(req.body[keys[i]], type[keys[i]], function (err) {
- // store the error's message and the field name
- if (err) errors.push({ name: keys[i], error: err.message });
- loop(++i, cb);
- });
+ for (var key in req.body) {
+ var err = validator.check(req.body[key], types[key]);
+ // store the error's message and the field name
+ if (err) errors.push({ name: key, error: err.message });
+ }
+ if (errors.length) {
+ // notify the user about the errors
+ res.render('template', { err: errors, other: 'params...' });
+ } else {
+ // safely store the user's input into the database
}
- loop(0, function () {
- if (errors.length) {
- // now that we have all errors we can notify the user about them
- res.render('template', { err: errors, other: 'params...' });
- } else {
- // we can safely store the user's input into the database
- }
- });
});
```
View
4 lib/data-type.js
@@ -12,7 +12,7 @@ var types = [
{regex: /(enum|set)\s*\(\s*(.+)\s*\)(?:\s+character\s+set\s+(\w+))?(?:\s+collate\s+(\w+))?/i, options: {values: 2, characterSet: 3, collate: 4}}
];
-exports.get = function (sql, cb) {
+exports.get = function (sql) {
for (var i=0; i < types.length; i++) {
var type = types[i],
match = sql.match(type.regex);
@@ -31,7 +31,7 @@ exports.get = function (sql, cb) {
dataType[key] = value;
}
}
- return cb(dataType);
+ return dataType;
}
}
}
View
9 lib/validator.js
@@ -4,11 +4,10 @@ var moment = require('moment'),
var dataType = require('./data-type');
-exports.check = function (value, type, cb) {
- dataType.get(type, function (type) {
- var func = name(type.name);
- cb(func(value, type));
- });
+exports.check = function (value, type) {
+ var type = dataType.get(type),
+ func = name(type.name);
+ return func(value, type);
}
// internal api
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "mysql-validator",
- "version": "0.1.2",
+ "version": "0.1.3",
"description": "MySql data type validation.",
"keywords": [
"mysql",
View
16 test/data-type.js
@@ -49,19 +49,13 @@ describe('mysql database', function () {
});
it('should test data types', function (done) {
- var keys = Object.keys(table);
console.log('');
- function loop (index) {
- if (index == keys.length) {
- return done();
- }
- var column = table[keys[index]];
- dataType.get(column.type, function (type) {
- console.log(column.type.yellow, type);
- loop(++index);
- });
+ for (var key in table) {
+ var column = table[key],
+ type = dataType.get(column.type);
+ console.log(column.type.yellow, type);
}
- loop(0);
+ done();
});
after(function (done) {
View
39 test/index.js
@@ -32,29 +32,24 @@ function runTest (tests, type, valid, cb) {
if (err) return cb(err);
if (valid) console.log('\t'+type.blue.bold);
console.log(valid ? '\tvalid'.magenta : '\tinvalid'.magenta);
- function loop (i, cb) {
- if (i == tests.length) {
- return cb();
- }
- validator.check(tests[i], type, function (err) {
- // print
- console.log('\t'+
- (!err ? rows[i][type].green : rows[i][type].red),
- tests[i].yellow,
- (!err ? 'valid'.green : err.message.red)
- );
- // test
- valid
- ? should.equal(err, null)
- : err.should.be.an.instanceOf(Error);
- loop(++i, cb);
- });
+
+ for (var i=0; i < tests.length; i++) {
+ var err = validator.check(tests[i], type);
+ // print
+ console.log('\t'+
+ (!err ? rows[i][type].green : rows[i][type].red),
+ tests[i].yellow,
+ (!err ? 'valid'.green : err.message.red)
+ );
+ // test
+ valid
+ ? should.equal(err, null)
+ : err.should.be.an.instanceOf(Error);
}
- loop(0, function () {
- c.query('delete from `datatypes`;', function (err, result) {
- if (err) return cb(err);
- cb();
- });
+
+ c.query('delete from `datatypes`;', function (err, result) {
+ if (err) return cb(err);
+ cb();
});
});
});

0 comments on commit dc128cb

Please sign in to comment.