Meteor collection2 - all validation messages

535 views Asked by At

I am looking for a way to retrieve all validation errors. (I'm using Collection2 and SimpleSchema)

Consider this code:

Foo.insert({
    title: '',
    description: ''
}, function(error, result) {
    console.error(error);
});

output:

{
    message: 'Title may not be empty.',
    invalidKeys: [
        0: {
            name: 'title',
            type: 'required',
            value: ''
        },
        1: {
            name: 'description',
            type: 'required',
            value: ''
        }
    ]
}

I would like to have all the error messages that are related to validation. Unfortunately I couldn't find any solution for this.

SOLUTION:

I've found a satisfiable solution

Foo.simpleSchema().namedContext().keyErrorMessage('title');
1

There are 1 answers

0
artisin On

I ran into the same problem and my solution was to insert said errors into a client mongo error collection which would then display the errors to the user. The following is what I came up with:

Schema

Schema.newUser = new SimpleSchema({....}); 

Client Side Validation

function tokenRegistration (newUser) {
  var valContext = Schema.newUser.namedContext('tokenRegForm');
  if (!valContext.validate(newUser)) {
    var keys = valContext.invalidKeys();
    _.each(keys, function (value) {
      var error = value.name,
          message = valContext.keyErrorMessage(error);
      return ErrorMessage.insert({errormessage: message}) 
    });
  }
}