naz / swagger-express-validator

Lightweight requrest/response validation middleware based on swagger/OpenAPI schema
MIT License
56 stars 24 forks source link

Request: throw 400 error on invalid response #53

Open shahtr opened 4 years ago

shahtr commented 4 years ago

Need validateResponse to throw an error for invalid responses, if no responseValidationFn is passed.


            res.set('Content-Type', ''); // Reset content-type since it is no longer valid
          }
          err.failedValidation = true;
          err.message = 'Value expected to be an array/object but is not';
          if (options.responseValidationFn) {
            options.responseValidationFn(req, data, [err]);
            sendData(res, data, encoding);
            return;
          }
          const resultError = {
            message: `Response schema validation failed for ${req.method}${req.originalUrl}`,
          };
          if (options.returnResponseErrors) {
            err.errors = [{ message: 'Invalid response format' }];
          }
          next(resultError);
          return;```

Need this to behave like validateRequest, where if there's no `options.requestValidationFn` and the request is invalid, the method sends 400 status with the error.