         queue.next()
 
     ->
-      # creating a random username with first letters of group slug in front
-      username or= "#{apiToken.group.substring(0, 4)}#{hat(32)}"
+      # if username is not provided and suggestedUsername is too long return error
+      unless username
+        unless isSuggestedUsernameLengthValid suggestedUsername
+          return callback apiErrors.outOfRangeSuggestedUsername
+
+      # if username is not set generate a username by suggestedUsername with a suffix
+      username or= "#{suggestedUsername}#{hat(32)}"
+
+      # checking if username has valid length
+      unless isUsernameLengthValid username
+        return callback apiErrors.outOfRangeUsername
+
       # checking if username is available
       JUser.usernameAvailable username, (err, { kodingUser, forbidden }) ->
         return callback apiErrors.internalError          if err

validateRequest = (req) ->

  token = null
  { username, email, firstName, lastName } = req.body

  unless email
    return { error : apiErrors.invalidInput }

  unless token = checkAuthorizationBearerHeader req
    return { error : apiErrors.unauthorizedRequest }

  return { error : null, token, username, email, firstName, lastName }


  checkAuthorizationBearerHeader } = require '../../helpers'
{ sendApiError
  sendApiResponse
  isUsernameLengthValid
  checkApiTokenAvailability
  isSuggestedUsernameLengthValid } = require './helpers'


module.exports = createUser = (req, res, next) ->
