scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Add more info to validation errors #56

Open radical-edo opened 8 years ago

radical-edo commented 8 years ago

Hi,

is there a way to get the reasons why validation did not pass? It would be nice to send some response why I did not allow to create/update the document?

I've glanced at the code but as far as I could tell all the validation errors are suppressed under the promise. No reasons are passed to the catch function when I do (for example) user.save().catch(function (err) { console.log(err) }).

the err object is just an empty object.

Have I missed something?

scottwrobinson commented 8 years ago

Hi,

Can you give me an example that doesn't work as expected? I'm sure there are some cases that don't get handled properly.

Here is an example that does show the error:

'use strict';

var connect = require('camo').connect;
var Document = require('camo').Document;

class Person extends Document {
    constructor () {
        super();

        this.fullName = Number;
    }
}

connect('nedb://memory').then(function(db) {
    var billy = Person.create({
        fullName: 'Billy Bob'
    });

    billy.save().then(function(data) {
        console.log('Saved people...');
    }).catch(function(err) {
        console.log('Error:', err.message);
    });
});

This outputs:

Error: Value assigned to persons.fullName should be Number, got string

Thanks!

radical-edo commented 8 years ago

I had the following

class User extends Document {
  constructor() {
    super();
    this.schema({
      name: {
        type: String,
        required: true
      },
      sex: {
        type: String,
        required: true
      }
      updated_at: {
        type: Date,
      },
      created_at: {
        type: Date,
      }
    });
  }
}
User.create({ name: '' }).save().catch(function (err) { res.send(err) });

And you're right. it works when you send it like you did res.send({ message: err.message }) So that would be my bad. By sending it like I did res.send(err) it was parsed to an empty object. That's my mistake. So thanks for that.

Is there a way to run the whole validation and get everything wrong with the passed params? Meaning in the example I've showed the error should be a bit more complex. name and sex required.

scottwrobinson commented 8 years ago

Yes, that would be a nice improvement for the validator. Right now Camo just throws the first error it finds.

I'll add this to the todo list.

Thanks!