validateEvent function

HookedServiceEventListener<dynamic, dynamic, Service<Null, Null>> validateEvent (
  1. Validator validator,
  2. {String errorMessage: 'Invalid data.'}
)

Validates the data in e.data, and sets the data to filtered data before continuing the service event.

Implementation

HookedServiceEventListener validateEvent(Validator validator,
    {String errorMessage = 'Invalid data.'}) {
  return (HookedServiceEvent e) async {
    var result = await asyncApplyValidator(
        validator, e.data as Map, (e.request?.app ?? e.service.app));

    if (result.errors.isNotEmpty) {
      throw AngelHttpException.badRequest(
          message: errorMessage, errors: result.errors);
    }

    e.data
      ..clear()
      ..addAll(result.data);
  };
}