validateEvent<Id, Data, T extends Service<Id, Data> > function
HookedServiceEventListener<Id, Data, T>
validateEvent<Id, Data, T extends Service<Id, Data> >(})
Validates the data in e.data, and sets the data to
filtered data before continuing the service event.
Data that is not a Map, such as that of a read or remove, is a 400.
Implementation
HookedServiceEventListener<Id, Data, T> validateEvent<
Id,
Data,
T extends Service<Id, Data>
>(Validator validator, {String errorMessage = 'Invalid data.'}) {
return (HookedServiceEvent<Id, Data, T> e) async {
var data = e.data;
if (data is! Map) {
throw AngelHttpException.badRequest(
message: errorMessage,
errors: ['The data must be an object.'],
);
}
var app = e.request?.app ?? e.service.app;
var result = await asyncApplyValidator(validator, data, app);
if (result.errors.isNotEmpty) {
throw AngelHttpException.badRequest(
message: errorMessage,
errors: result.errors,
);
}
data
..clear()
..addAll(result.data);
};
}