Validation

Throw error or return rejected promise at any step. There are some useful libraries for data validation:

import ALL_USERS_FIXTURE form 'fixtures/all-users';

class UserDataSource extends DataSource {

  static steps =  ['validation', 'api'];

  static validation = {
    create: user => {
      if (!user.id) {
        // throw new Error('"id" is required');
        return Promise.reject('"id" is required');
      }

      return user;
    }
  }

  static api = {
    loadAll: api('GET /users'),
    create: api('POST /users')
  };

  loadAll(params) {
    return this.invoke('loadAll', params);
  }

  create(user) {
    return this.invoke('create', user);
  }
}

results matching ""

    No results matching ""