Validator
This commit is contained in:
parent
43309519cd
commit
261ebea529
2 changed files with 13 additions and 12 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue