meteor/validation-error

ValidationError.is(error) failing when using mdg:validated-method (err.error = 400)

Siyfion opened this issue · 2 comments

In my front-end code I have the following check:

  const onNameChange = (name) => {
    StaticText.methods.updateName.call({
      staticTextId: fixedText._id,
      newName: name,
    }, (error) => {
      if (ValidationError.is(error)) { // This line doesn't pass the logic-test
        error.details.forEach((fieldError) => {
          console.log(fieldError.type);
        });
      }
    });
  };

And the following method, using mdg:validated-method:

StaticText.methods.updateName = new ValidatedMethod({
  name: 'StaticText.methods.updateName',

  validate: new SimpleSchema({
    staticTextId: { type: String },
    newName: { type: String, trim: false },
  }).validator(),

  run({ staticTextId, newName }) {
    const staticText = StaticText.findOne(staticTextId);
    if (!staticText) {
      return throwError('not-found', 'The specified static text does not exist');
    }
    checkUserIsOwner(staticText);

    return StaticText.update(staticTextId, {
      $set: { name: newName },
    }, {
      autoConvert: false,
    });
  }
});

The line that fails the check is this, in the validation-error package, err.error seems to be set to 400 rather than the expected ValidationError.ERROR_CODE for some reason.

  // Static method checking if a given Meteor.Error is an instance of
  // ValidationError.
  static is(err) {
    return err instanceof Meteor.Error && err.error === ValidationError.ERROR_CODE;
  };

Am I missing something!? I am using Simple Schema v1.5.3

I can confirm I have the same issue. It's not a good sign that nobody had an answer to that.

In case if someone will face the same issue.
You need to change error code for validation errors like:

SimpleSchema.defineValidationErrorTransform(error => {
  const ddpError = new Meteor.Error(error.message);
  ddpError.error = 'validation-error';
  ddpError.details = error.details;
  return ddpError;
});