tgriesser / checkit

simple, flexible validations for node and the browser
MIT License
223 stars 53 forks source link

async version (0.7.0) doesn't run #74

Closed AlexZeitler closed 8 years ago

AlexZeitler commented 8 years ago

I like checkit pretty much, but I can't get it working using the async call in version 0.7.0 working (Node.js 6.2.2).

This is my test code:

'use strict';
console.log(global.Promise);
const Checkit = require('checkit');
const createCheckit = new Checkit({
  name: 'required'
});

createCheckit.run({}).then(validated => {
  console.log('async: ', validated);
}).catch(Checkit.Error, err => {
  console.log('async errors: ', err.errors);
});

const [err, validated] = createCheckit.validateSync({});

console.log('sync errors: ', err.errors);

This is the console output:

[Function: Promise]
sync errors: { name: 
   { Error
       at module.exports (/home/alexzeitler/src/hellocheckit/node_modules/checkit/core.js:448:24)
       at Object.<anonymous> (/home/alexzeitler/src/hellocheckit/node_modules/checkit/index.js:2:35)
       at Module._compile (module.js:541:32)
       at Object.Module._extensions..js (module.js:550:10)
       at Module.load (module.js:458:32)
       at tryModuleLoad (module.js:417:12)
       at Function.Module._load (module.js:409:3)
       at Module.require (module.js:468:17)
       at require (internal/module.js:20:19)
       at Object.<anonymous> (/home/alexzeitler/src/hellocheckit/index.js:3:17)
     message: 'The name is required',
     errors: [ [Object] ],
     key: 'name' } } null

As you can see, there is a global Promise and the sync version works as expected but the async version doesn't throw.

If I pass in a valid object, validated is passed in and the console looks like async: { name: 'alex' }

AlexZeitler commented 8 years ago

BTW if I clone the checkit repository from here and run the tests, everything is fine... :worried:

AlexZeitler commented 8 years ago

Found the solution:

var checkit = new Checkit({
  firstName: 'required',
  lastName: 'required',
  email: ['required', 'email']
});

var body = {
  firstName: 'Tim',
  lastName: 'Griesser',
  githubUsername: 'tgriesser'
};

checkit.run(body).then(function (validated) {
  console.log(validated);
}).catch(err => {
  console.log(err.toJSON());
});

The difference is in the .catch() statement:

Sample from website: .catch(Checkit.Error, function(err) {})

Tests / working solution: .catch(function(err) {})