From 0d7033f662be17693e7df41e05c7bd5224f7a9c4 Mon Sep 17 00:00:00 2001 From: Matt Enlow Date: Fri, 25 Jul 2014 13:17:16 -0600 Subject: [PATCH] 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 --- core/client/validators/new-user.js | 28 ++++++++++++++++++++++++++++ core/client/validators/setup.js | 28 ++++++---------------------- core/client/validators/signup.js | 29 ++--------------------------- 3 files changed, 36 insertions(+), 49 deletions(-) create mode 100644 core/client/validators/new-user.js diff --git a/core/client/validators/new-user.js b/core/client/validators/new-user.js new file mode 100644 index 0000000000..faaf3029ca --- /dev/null +++ b/core/client/validators/new-user.js @@ -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; diff --git a/core/client/validators/setup.js b/core/client/validators/setup.js index 9f7de2af5c..ab5c74fd75 100644 --- a/core/client/validators/setup.js +++ b/core/client/validators/setup.js @@ -1,7 +1,9 @@ -var SetupValidator = Ember.Object.create({ +import NewUserValidator from 'ghost/validators/new-user'; + +var SetupValidator = NewUserValidator.extend({ check: function (model) { - var data = model.getProperties('blogTitle', 'name', 'email', 'password'), - validationErrors = []; + var data = model.getProperties('blogTitle'), + validationErrors = this._super(model); if (!validator.isLength(data.blogTitle, 1)) { 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; } -}); +}).create(); export default SetupValidator; diff --git a/core/client/validators/signup.js b/core/client/validators/signup.js index 27745e8a0e..0e0a537719 100644 --- a/core/client/validators/signup.js +++ b/core/client/validators/signup.js @@ -1,28 +1,3 @@ -var SignupValidator = Ember.Object.create({ - check: function (model) { - var data = model.getProperties('name', 'email', 'password'), - validationErrors = []; +import NewUserValidator from 'ghost/validators/new-user'; - 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 SignupValidator; +export default NewUserValidator.create();