Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roelvan opened this issue Dec 13, 2016 · 1 comment
Open

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

roelvan opened this issue Dec 13, 2016 · 1 comment

Comments

@roelvan
Copy link

roelvan commented Dec 13, 2016

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
Copy link
Author

roelvan commented Dec 13, 2016

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;
    }
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant