Validator

This commit is contained in:
thosakwe 2017-01-20 17:53:14 -05:00
parent 43309519cd
commit 261ebea529
2 changed files with 13 additions and 12 deletions

View file

@ -1,7 +1,6 @@
import 'package:angel_common/angel_common.dart'; import 'package:angel_common/angel_common.dart';
import 'package:crypto/crypto.dart' show sha256; import 'package:crypto/crypto.dart' show sha256;
import 'package:mongo_dart/mongo_dart.dart'; import 'package:mongo_dart/mongo_dart.dart';
import 'package:validate/validate.dart';
import '../models/user.dart'; import '../models/user.dart';
export '../models/user.dart'; export '../models/user.dart';
@ -11,6 +10,19 @@ configureServer(Db db) {
HookedService service = app.service('api/users'); HookedService service = app.service('api/users');
app.container.singleton(service.inner); 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(); 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); return _inner.create(data, params);
} }

View file

@ -9,7 +9,6 @@ dependencies:
angel_configuration: ^1.0.0 angel_configuration: ^1.0.0
angel_multiserver: ^1.0.0-dev angel_multiserver: ^1.0.0-dev
mailer: ^1.1.0+4 mailer: ^1.1.0+4
validate: ^1.5.2
dev_dependencies: dev_dependencies:
build_runner: ^0.1.1 build_runner: ^0.1.1
grinder: ^0.8.0+2 grinder: ^0.8.0+2