bodyFieldMinLength<B extends Body> function
bodyFieldMinLength check if the fieldName
length is at least minLength
if check failed return BadRequest response
Implementation
Middleware bodyFieldMinLength<B extends Body>(String fieldName, int minLength) {
return (handler) {
return (request) {
Response? checkItem(Map<String, dynamic> item) {
final value = item[fieldName] as String;
if (value.length < minLength) {
final bodyResponse = BadRequest(
'about:blank',
'Value is too short',
'The field \'$fieldName\' is too short, length is \'${value.length}\' but min length must be \'$minLength\'',
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);
};
};
}