2016-12-25 21:26:55 +00:00
|
|
|
/// Support for using `angel_validate` with the Angel Framework.
|
|
|
|
library angel_validate.server;
|
|
|
|
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-06-28 15:07:04 +00:00
|
|
|
import 'src/async.dart';
|
2016-12-25 21:26:55 +00:00
|
|
|
import 'angel_validate.dart';
|
2018-06-28 15:07:04 +00:00
|
|
|
export 'src/async.dart';
|
2016-12-25 21:26:55 +00:00
|
|
|
export 'angel_validate.dart';
|
|
|
|
|
2016-12-26 13:04:42 +00:00
|
|
|
/// Auto-parses numbers in `req.body`.
|
|
|
|
RequestMiddleware autoParseBody(List<String> fields) {
|
|
|
|
return (RequestContext req, res) async {
|
2017-03-29 02:56:51 +00:00
|
|
|
(await req.lazyBody()).addAll(autoParse(req.body, fields));
|
2016-12-26 13:04:42 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Auto-parses numbers in `req.query`.
|
|
|
|
RequestMiddleware autoParseQuery(List<String> fields) {
|
|
|
|
return (RequestContext req, res) async {
|
2017-05-13 01:24:54 +00:00
|
|
|
req.query.addAll(autoParse(req.query, fields));
|
2016-12-26 13:04:42 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-25 23:03:45 +00:00
|
|
|
/// Filters unwanted data out of `req.body`.
|
|
|
|
RequestMiddleware filterBody(Iterable<String> only) {
|
|
|
|
return (RequestContext req, res) async {
|
2017-03-29 02:56:51 +00:00
|
|
|
var filtered = filter(await req.lazyBody(), only);
|
2017-01-25 23:03:45 +00:00
|
|
|
req.body
|
|
|
|
..clear()
|
|
|
|
..addAll(filtered);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Filters unwanted data out of `req.query`.
|
|
|
|
RequestMiddleware filterQuery(Iterable<String> only) {
|
|
|
|
return (RequestContext req, res) async {
|
2017-05-13 01:24:54 +00:00
|
|
|
var filtered = filter(req.query, only);
|
2017-01-25 23:03:45 +00:00
|
|
|
req.query
|
|
|
|
..clear()
|
|
|
|
..addAll(filtered);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:26:55 +00:00
|
|
|
/// Validates the data in `req.body`, and sets the body to
|
|
|
|
/// filtered data before continuing the response.
|
2016-12-25 22:46:08 +00:00
|
|
|
RequestMiddleware validate(Validator validator,
|
|
|
|
{String errorMessage: 'Invalid data.'}) {
|
|
|
|
return (RequestContext req, res) async {
|
2017-05-13 01:24:54 +00:00
|
|
|
var result = validator.check(await req.lazyBody());
|
2016-12-25 21:26:55 +00:00
|
|
|
|
2016-12-25 22:46:08 +00:00
|
|
|
if (result.errors.isNotEmpty) {
|
2017-03-29 02:56:51 +00:00
|
|
|
throw new AngelHttpException.badRequest(
|
2016-12-25 22:46:08 +00:00
|
|
|
message: errorMessage, errors: result.errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
req.body
|
|
|
|
..clear()
|
|
|
|
..addAll(result.data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2016-12-25 21:26:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 22:46:08 +00:00
|
|
|
/// Validates the data in `req.query`, and sets the query to
|
2016-12-25 21:26:55 +00:00
|
|
|
/// filtered data before continuing the response.
|
2016-12-25 22:46:08 +00:00
|
|
|
RequestMiddleware validateQuery(Validator validator,
|
|
|
|
{String errorMessage: 'Invalid data.'}) {
|
|
|
|
return (RequestContext req, res) async {
|
|
|
|
var result = validator.check(req.query);
|
|
|
|
|
|
|
|
if (result.errors.isNotEmpty) {
|
2017-03-29 02:56:51 +00:00
|
|
|
throw new AngelHttpException.badRequest(
|
2016-12-25 22:46:08 +00:00
|
|
|
message: errorMessage, errors: result.errors);
|
|
|
|
}
|
2016-12-25 21:26:55 +00:00
|
|
|
|
2016-12-25 22:46:08 +00:00
|
|
|
req.query
|
|
|
|
..clear()
|
|
|
|
..addAll(result.data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2016-12-25 21:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Validates the data in `e.data`, and sets the data to
|
|
|
|
/// filtered data before continuing the service event.
|
2016-12-25 22:46:08 +00:00
|
|
|
HookedServiceEventListener validateEvent(Validator validator,
|
|
|
|
{String errorMessage: 'Invalid data.'}) {
|
|
|
|
return (HookedServiceEvent e) {
|
2018-06-28 02:34:05 +00:00
|
|
|
var result = validator.check(e.data as Map);
|
2016-12-25 21:26:55 +00:00
|
|
|
|
2016-12-25 22:46:08 +00:00
|
|
|
if (result.errors.isNotEmpty) {
|
2017-03-29 02:56:51 +00:00
|
|
|
throw new AngelHttpException.badRequest(
|
2016-12-25 22:46:08 +00:00
|
|
|
message: errorMessage, errors: result.errors);
|
|
|
|
}
|
2016-12-25 21:26:55 +00:00
|
|
|
|
2016-12-25 22:46:08 +00:00
|
|
|
e.data
|
|
|
|
..clear()
|
|
|
|
..addAll(result.data);
|
|
|
|
};
|
|
|
|
}
|