v0.5.1Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.


meteor add mdg:validation-error

Use a validation error to indicate that a method call has failed and the client can fix it by changing specific arguments. Examples:

  1. An argument isn't of the right type
  2. A number argument isn't in the right range
  3. A username wasn't found in the database

This kind of error is tied to a specific field so that you can display it next to an input in a form currently being filled by the user.

This error format is based on the error output of aldeed:simple-schema and is also compatible with the error output of jagi:astronomy.


new ValidationError(errors: Array, [message: String])

errors must be an array with keys of the form:

2  {
3    // Name of the field this error is about.
4    name: String,
6    // Type of error, can be mapped to a nice message on the client.
7    type: String,
9    // Any kind of details that depends on the type of error can be added as
10    // an extra object's property (eg. `message` with a per field error message
11    // or `value` that contains the invalid value passed from the client).
12    ...
13  }
14  ...

message is an optional string to use for the error message so that the text printed at the top of the stack trace when the error is thrown is more useful. For example, if you pass in the error {name: 'name', type: 'required'}, you may want to also pass in the message "Name is required". Meteor.Error)

The static method is a helper for checking if an error thrown by a server and catched on the client is an instance of ValidationError.'method', (err) => {
2  if ( {
3    ...
4  }

Usage example

1// Inside a method definition
2saveProduct({ name, cost, category }) {
3  if (cost > 1000) {
4    throw new ValidationError([
5      {
6        name: 'cost',
7        type: 'out-of-range',
8        value: cost,
9        min: 0,
10        max: 100
11      }
12    ]);
13  }
15  // ... the rest of the method

You might catch the error returned by a method call and display it in the UI:{
2  'submit': (event, instance) => {
3'method', (err) => {
4      if ( {
5        err.details.forEach((fieldError) => {
6          instance.state.set(`error-${}`: fieldError.type);
7        });
8      }
9    });
10  }

Works out of the box with mdg:method

This type of error is automatically thrown for invalid arguments if you use the mdg:method package, where you can specify a schema for the arguments of your method.

Running tests

meteor test-packages --driver-package practicalmeteor:mocha ./