meteor/validation-error

Name: validation-error

Owner: Meteor

Description: Standardized validation error format for Meteor

Created: 2015-11-17 05:22:19.0

Updated: 2017-03-23 20:16:33.0

Pushed: 2017-02-22 17:50:23.0

Homepage: https://atmospherejs.com/mdg/validation-error

Size: 16

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ValidationError

or 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.

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

errors must be an array with keys of the form:



// Name of the field this error is about.
name: String,

// Type of error, can be mapped to a nice message on the client.
type: String,

// Any kind of details that depends on the type of error can be added as
// an extra object's property (eg. `message` with a per field error message
// or `value` that contains the invalid value passed from the client).
...

.

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”.

ValidationError.is(error: Meteor.Error)

The static ValidationError.is method is a helper for checking if an error thrown by a server and catched on the client is an instance of ValidationError.

or.call('method', (err) => {
 (ValidationError.is(err)) {
...


Usage example
nside a method definition
Product({ name, cost, category }) {
 (cost > 1000) {
throw new ValidationError([
  {
    name: 'cost',
    type: 'out-of-range',
    value: cost,
    min: 0,
    max: 100
  }
]);


 ... the rest of the method

You might catch the error returned by a method call and display it in the UI:

late.foo.events({
ubmit': (event, instance) => {
Meteor.call('method', (err) => {
  if (ValidationError.is(err)) {
    err.details.forEach((fieldError) => {
      instance.state.set(`error-${fieldError.name}`: fieldError.type);
    });
  }
});


Works out of the box with mdg:method

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

Running tests
or test-packages --driver-package practicalmeteor:mocha ./

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.