bodyFieldValidator<T extends Body> function
bodyFieldValidator run validator
on the field fieldName
if validator failed return BadRequest response
Implementation
Middleware bodyFieldValidator<T extends Body>(
String fieldName, bool Function(Object? value) validator) {
return (handler) {
return (request) {
Response? checkItem(Map<String, dynamic> item) {
final value = item[fieldName] as String;
if (!validator(value)) {
final bodyResponse = BadRequest(
'about:blank',
'Validator doesn\'t accept this value',
'Field \'$fieldName\' with value \'$value\' is not authorized by this validator',
400,
request.requestedUri.path,
field: fieldName,
);
return generateResponse(request, bodyResponse);
}
return null;
}
final body = request.get<T>();
final response = _checkData(body.data, checkItem);
if (response != null) {
return response;
}
return handler(request);
};
};
}