bodyFieldIsType<B extends Body, T> function
bodyFieldIsType check if the fieldName
is type T
if check failed return BadRequest response
Implementation
Middleware bodyFieldIsType<B extends Body, T>(String fieldName) {
return (handler) {
return (request) {
Response? checkItem(Map<String, dynamic> item) {
if (item[fieldName] is! T) {
final bodyResponse = BadRequest(
'about:blank',
'Type of value not authorized',
'The field \'$fieldName\' is type \'${item[fieldName].runtimeType}\' but must be \'$T\'',
400,
request.requestedUri.path,
field: fieldName,
);
return generateResponse(request, bodyResponse);
}
return null;
}
final body = request.get<B>();
final response = _checkData(body.data, checkItem);
if (response != null) {
return response;
}
return handler(request);
};
};
}