validateQuery function

RequestHandler validateQuery(
  1. Validator validator,
  2. {String errorMessage = 'Invalid data.'}
)

Validates the data in req.queryParameters, and sets the query to filtered data before continuing the response.

Implementation

RequestHandler validateQuery(Validator validator, {String errorMessage = 'Invalid data.'}) {
  return (RequestContext req, res) async {
    var result = await asyncApplyValidator(validator, req.queryParameters, req.app);

    if (result.errors.isNotEmpty) {
      throw GalileoHttpException.badRequest(message: errorMessage, errors: result.errors);
    }

    req.queryParameters
      ..clear()
      ..addAll(result.data);

    return true;
  };
}