diff --git a/lib/src/services/user.dart b/lib/src/services/user.dart index e1e1ad3..2ac2316 100644 --- a/lib/src/services/user.dart +++ b/lib/src/services/user.dart @@ -1,7 +1,6 @@ import 'package:angel_common/angel_common.dart'; import 'package:crypto/crypto.dart' show sha256; import 'package:mongo_dart/mongo_dart.dart'; -import 'package:validate/validate.dart'; import '../models/user.dart'; export '../models/user.dart'; @@ -11,6 +10,19 @@ configureServer(Db db) { HookedService service = app.service('api/users'); app.container.singleton(service.inner); + + service.beforeCreated + ..listen(validateEvent( + new Validator({ + 'username*': [isString, isNotEmpty], + 'password*': [isString, isNotEmpty], + 'email*': [isString, isNotEmpty, isEmail], + }), + errorMessage: + 'User must have a username, e-mail address and password.')) + ..listen((e) { + e.data['password'] = hashPassword(e.data['password']); + }); }; } @@ -46,16 +58,6 @@ class UserService extends Service { throw new AngelHttpException.forbidden(); } - try { - Validate.isKeyInMap('username', data); - Validate.isKeyInMap('password', data); - Validate.isEmail(data['email']); - data['password'] = hashPassword(data['password']); - } catch (e) { - throw new AngelHttpException.badRequest( - message: 'User must have a username, e-mail address and password.'); - } - return _inner.create(data, params); } diff --git a/pubspec.yaml b/pubspec.yaml index 9f212e5..3886f17 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,6 @@ dependencies: angel_configuration: ^1.0.0 angel_multiserver: ^1.0.0-dev mailer: ^1.1.0+4 - validate: ^1.5.2 dev_dependencies: build_runner: ^0.1.1 grinder: ^0.8.0+2