0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Create new user validator to DRY up validators

No issue
- Created NewUserValidator class to DRY up validation of a models name, email, and password
- Changed SignUpValidator to be an instance of NewUserValidator
- Changed SetUpValidator to extend NewUserValidator
This commit is contained in:
Matt Enlow 2014-07-25 13:17:16 -06:00
parent d2d25faa5c
commit 0d7033f662
3 changed files with 36 additions and 49 deletions

View file

@ -0,0 +1,28 @@
var NewUserValidator = Ember.Object.extend({
check: function (model) {
var data = model.getProperties('name', 'email', 'password'),
validationErrors = [];
if (!validator.isLength(data.name, 1)) {
validationErrors.push({
message: 'Please enter a name.'
});
}
if (!validator.isEmail(data.email)) {
validationErrors.push({
message: 'Invalid Email.'
});
}
if (!validator.isLength(data.password, 8)) {
validationErrors.push({
message: 'Password must be at least 8 characters long.'
});
}
return validationErrors;
}
});
export default NewUserValidator;

View file

@ -1,7 +1,9 @@
var SetupValidator = Ember.Object.create({ import NewUserValidator from 'ghost/validators/new-user';
var SetupValidator = NewUserValidator.extend({
check: function (model) { check: function (model) {
var data = model.getProperties('blogTitle', 'name', 'email', 'password'), var data = model.getProperties('blogTitle'),
validationErrors = []; validationErrors = this._super(model);
if (!validator.isLength(data.blogTitle, 1)) { if (!validator.isLength(data.blogTitle, 1)) {
validationErrors.push({ validationErrors.push({
@ -9,26 +11,8 @@ var SetupValidator = Ember.Object.create({
}); });
} }
if (!validator.isLength(data.name, 1)) {
validationErrors.push({
message: 'Please enter a name.'
});
}
if (!validator.isEmail(data.email)) {
validationErrors.push({
message: 'Invalid Email.'
});
}
if (!validator.isLength(data.password, 8)) {
validationErrors.push({
message: 'Password must be at least 8 characters long.'
});
}
return validationErrors; return validationErrors;
} }
}); }).create();
export default SetupValidator; export default SetupValidator;

View file

@ -1,28 +1,3 @@
var SignupValidator = Ember.Object.create({ import NewUserValidator from 'ghost/validators/new-user';
check: function (model) {
var data = model.getProperties('name', 'email', 'password'),
validationErrors = [];
if (!validator.isLength(data.name, 1)) { export default NewUserValidator.create();
validationErrors.push({
message: 'Please enter a name.'
});
}
if (!validator.isEmail(data.email)) {
validationErrors.push({
message: 'Invalid Email.'
});
}
if (!validator.isLength(data.password, 8)) {
validationErrors.push({
message: 'Password must be at least 8 characters long.'
});
}
return validationErrors;
}
});
export default SignupValidator;