Refactor validation to a single place

This commit is contained in:
Juan Cruz Viotti 2015-10-21 09:37:25 -04:00
parent ec72f93480
commit c1e6a28640
8 changed files with 61 additions and 49 deletions

View File

@ -1,5 +1,5 @@
(function() { (function() {
var Promise, _, events, form, helpers, resin, visuals; var Promise, _, events, form, resin, validation, visuals;
Promise = require('bluebird'); Promise = require('bluebird');
@ -13,7 +13,7 @@
events = require('resin-cli-events'); events = require('resin-cli-events');
helpers = require('../utils/helpers'); validation = require('../utils/validation');
exports.login = { exports.login = {
signature: 'login', signature: 'login',
@ -39,7 +39,7 @@
message: 'Email:', message: 'Email:',
name: 'email', name: 'email',
type: 'input', type: 'input',
validate: helpers.validateEmail validate: validation.validateEmail
}, { }, {
message: 'Password:', message: 'Password:',
name: 'password', name: 'password',
@ -89,7 +89,7 @@
message: 'Email:', message: 'Email:',
name: 'email', name: 'email',
type: 'input', type: 'input',
validate: helpers.validateEmail validate: validation.validateEmail
}, { }, {
message: 'Username:', message: 'Username:',
name: 'username', name: 'username',
@ -98,12 +98,7 @@
message: 'Password:', message: 'Password:',
name: 'password', name: 'password',
type: 'password', type: 'password',
validate: function(input) { validate: validation.validatePassword
if (input.length < 8) {
return 'Password should be 8 characters long';
}
return true;
}
} }
]).then(resin.auth.register).then(resin.auth.loginWithToken).tap(function() { ]).then(resin.auth.register).then(resin.auth.loginWithToken).tap(function() {
return events.send('user.signup'); return events.send('user.signup');

View File

@ -1,5 +1,5 @@
(function() { (function() {
var Promise, _, capitano, chalk, child_process, os, validEmail; var Promise, _, capitano, chalk, child_process, os;
Promise = require('bluebird'); Promise = require('bluebird');
@ -15,21 +15,12 @@
chalk = require('chalk'); chalk = require('chalk');
validEmail = require('valid-email');
exports.getGroupDefaults = function(group) { exports.getGroupDefaults = function(group) {
return _.chain(group).get('options').map(function(question) { return _.chain(group).get('options').map(function(question) {
return [question.name, question["default"]]; return [question.name, question["default"]];
}).object().value(); }).object().value();
}; };
exports.validateEmail = function(input) {
if (!validEmail(input)) {
return 'Email is not valid';
}
return true;
};
exports.getOperatingSystem = function() { exports.getOperatingSystem = function() {
var platform; var platform;
platform = os.platform(); platform = os.platform();

View File

@ -1,5 +1,5 @@
(function() { (function() {
var Promise, _, chalk, form, helpers, resin, visuals; var Promise, _, chalk, form, resin, validations, visuals;
_ = require('lodash'); _ = require('lodash');
@ -13,7 +13,7 @@
chalk = require('chalk'); chalk = require('chalk');
helpers = require('./helpers'); validations = require('./validations');
exports.selectDeviceType = function() { exports.selectDeviceType = function() {
return resin.models.device.getSupportedDeviceTypes().then(function(deviceTypes) { return resin.models.device.getSupportedDeviceTypes().then(function(deviceTypes) {
@ -81,12 +81,7 @@
return form.ask({ return form.ask({
message: 'Choose a Name for your new application', message: 'Choose a Name for your new application',
type: 'input', type: 'input',
validate: function(input) { validate: validation.validateApplicationName
if (input.length < 4) {
return 'The application name should be at least 4 characters';
}
return true;
}
}); });
}); });
}; };

27
build/utils/validation.js Normal file
View File

@ -0,0 +1,27 @@
(function() {
var validEmail;
validEmail = require('valid-email');
exports.validateEmail = function(input) {
if (!validEmail(input)) {
return 'Email is not valid';
}
return true;
};
exports.validatePassword = function(input) {
if (input.length < 8) {
return 'Password should be 8 characters long';
}
return true;
};
exports.validateApplicationName = function(input) {
if (input.length < 4) {
return 'The application name should be at least 4 characters';
}
return true;
};
}).call(this);

View File

@ -4,7 +4,7 @@ resin = require('resin-sdk')
form = require('resin-cli-form') form = require('resin-cli-form')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
events = require('resin-cli-events') events = require('resin-cli-events')
helpers = require('../utils/helpers') validation = require('../utils/validation')
exports.login = exports.login =
signature: 'login' signature: 'login'
@ -36,7 +36,7 @@ exports.login =
message: 'Email:' message: 'Email:'
name: 'email' name: 'email'
type: 'input' type: 'input'
validate: helpers.validateEmail validate: validation.validateEmail
, ,
message: 'Password:' message: 'Password:'
name: 'password' name: 'password'
@ -100,7 +100,7 @@ exports.signup =
message: 'Email:' message: 'Email:'
name: 'email' name: 'email'
type: 'input' type: 'input'
validate: helpers.validateEmail validate: validation.validateEmail
, ,
message: 'Username:' message: 'Username:'
name: 'username' name: 'username'
@ -109,11 +109,7 @@ exports.signup =
message: 'Password:' message: 'Password:'
name: 'password' name: 'password'
type: 'password', type: 'password',
validate: (input) -> validate: validation.validatePassword
if input.length < 8
return 'Password should be 8 characters long'
return true
] ]
.then(resin.auth.register) .then(resin.auth.register)

View File

@ -5,7 +5,6 @@ _.str = require('underscore.string')
child_process = require('child_process') child_process = require('child_process')
os = require('os') os = require('os')
chalk = require('chalk') chalk = require('chalk')
validEmail = require('valid-email')
exports.getGroupDefaults = (group) -> exports.getGroupDefaults = (group) ->
return _.chain(group) return _.chain(group)
@ -15,12 +14,6 @@ exports.getGroupDefaults = (group) ->
.object() .object()
.value() .value()
exports.validateEmail = (input) ->
if not validEmail(input)
return 'Email is not valid'
return true
exports.getOperatingSystem = -> exports.getOperatingSystem = ->
platform = os.platform() platform = os.platform()
platform = 'osx' if platform is 'darwin' platform = 'osx' if platform is 'darwin'

View File

@ -4,7 +4,7 @@ form = require('resin-cli-form')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
resin = require('resin-sdk') resin = require('resin-sdk')
chalk = require('chalk') chalk = require('chalk')
helpers = require('./helpers') validations = require('./validations')
exports.selectDeviceType = -> exports.selectDeviceType = ->
resin.models.device.getSupportedDeviceTypes().then (deviceTypes) -> resin.models.device.getSupportedDeviceTypes().then (deviceTypes) ->
@ -53,11 +53,7 @@ exports.selectOrCreateApplication = ->
form.ask form.ask
message: 'Choose a Name for your new application' message: 'Choose a Name for your new application'
type: 'input' type: 'input'
validate: (input) -> validate: validation.validateApplicationName
if input.length < 4
return 'The application name should be at least 4 characters'
return true
exports.selectProjectDirectory = -> exports.selectProjectDirectory = ->
resin.settings.get('projectsDirectory').then (projectsDirectory) -> resin.settings.get('projectsDirectory').then (projectsDirectory) ->

View File

@ -0,0 +1,19 @@
validEmail = require('valid-email')
exports.validateEmail = (input) ->
if not validEmail(input)
return 'Email is not valid'
return true
exports.validatePassword = (input) ->
if input.length < 8
return 'Password should be 8 characters long'
return true
exports.validateApplicationName = (input) ->
if input.length < 4
return 'The application name should be at least 4 characters'
return true