Open roelvan opened 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;
}
};
};
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: