BestBuy / api-playground

The Best Buy API Playground is an API training tool for students, educators and other learners to explore the possibilities of a fully functional RESTful API in a simple, non-production environment.
Other
260 stars 104 forks source link

POST'ing an array of categories, products,... is not supported. #11

Open roelvan opened 7 years ago

roelvan commented 7 years ago

Often it is quite handy to post an array of categories in one request instead of eg. 5 seperate requests. This valdiation prevents this: https://github.com/BestBuy/api-playground/blob/master/src/hooks/validate-schema.js#L17-L26

This is possible in a standard feathers app:

const players = _map(this.players, player => {
              return {
                name: player.name,
                teamId: team.id
              }
            })
            http.post('/players', players)
              .then((result) => {
                this.$router.push({name: 'play', params: {teamId: team.id}})
              })
roelvan commented 7 years ago

This change might work:

const _isArray = require('lodash').isArray;

module.exports = function (schema) {
  return function validateSchema (hook) {
    let validator = new Ajv({allErrors: true});
    let isValid = false;

    if (_isArray(hook.data)) {
      for (var dataItem of hook.data) {
        isValid = validator.validate(schema, dataItem);
      }
    } else {
      isValid = validator.validate(schema, hook.data);
    }

    if (!isValid) {
      let errorMessages = validator.errors.map(formatErrorMessage);
      let validationErrors = new errors.BadRequest('Invalid Parameters', { errors: errorMessages });
      throw validationErrors;
    }
  };
};