bodyValidator<T extends Body> function

Middleware bodyValidator<T extends Body>(
  1. bool validator(
    1. Object? value
    )
)

bodyValidator is a flexible validator that allow you to check whatever you need on the body if validator failed return BadRequest response

Implementation

Middleware bodyValidator<T extends Body>(
    bool Function(Object? value) validator) {
  return (handler) {
    return (request) {
      final body = request.get<T>();
      if (!validator(body.data)) {
        final bodyResponse = BadRequest(
          'about:blank',
          'Validator doesn\'t accept this body',
          'Body with value \'${body.data}\' is not authorized by this validator',
          400,
          request.requestedUri.path,
        );
        return generateResponse(request, bodyResponse);
      }
      return handler(request);
    };
  };
}