Server done
This commit is contained in:
parent
e6c0771e88
commit
b5a5d54157
1 changed files with 44 additions and 5 deletions
|
@ -7,19 +7,58 @@ export 'angel_validate.dart';
|
|||
|
||||
/// Validates the data in `req.body`, and sets the body to
|
||||
/// filtered data before continuing the response.
|
||||
RequestMiddleware validate(Validator validator, {String errorMessage}) {
|
||||
RequestMiddleware validate(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (RequestContext req, res) async {
|
||||
var result = validator.check(req.body);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.BadRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
req.body
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Validates the data in `req.body`, and sets the query to
|
||||
/// Validates the data in `req.query`, and sets the query to
|
||||
/// filtered data before continuing the response.
|
||||
RequestMiddleware validateQuery(Validator validator, {String errorMessage}) {
|
||||
RequestMiddleware validateQuery(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (RequestContext req, res) async {
|
||||
var result = validator.check(req.query);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.BadRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
req.query
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// Validates the data in `e.data`, and sets the data to
|
||||
/// filtered data before continuing the service event.
|
||||
HookedServiceEventListener validateEvent(Validator validator) {
|
||||
HookedServiceEventListener validateEvent(Validator validator,
|
||||
{String errorMessage: 'Invalid data.'}) {
|
||||
return (HookedServiceEvent e) {
|
||||
var result = validator.check(e.data);
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
throw new AngelHttpException.BadRequest(
|
||||
message: errorMessage, errors: result.errors);
|
||||
}
|
||||
|
||||
e.data
|
||||
..clear()
|
||||
..addAll(result.data);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue