validate function

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

Validates the data in req.bodyAsMap, and sets the body to filtered data before continuing the response.

Implementation

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

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

    req.bodyAsMap
      ..clear()
      ..addAll(result.data);
    return true;
  };
}