Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more info to validation errors #56

Open
radical-edo opened this issue Feb 27, 2016 · 3 comments
Open

Add more info to validation errors #56

radical-edo opened this issue Feb 27, 2016 · 3 comments

Comments

@radical-edo
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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!

@scottwrobinson scottwrobinson changed the title Error messages/validation not passed Add more info to validation errors Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants