From e97cf73d26684d7dc521e9ada221b1a18f02ff90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dzieko=C5=84ski?= Date: Sat, 19 Mar 2022 02:37:28 +0100 Subject: [PATCH] GH-150 Validate galaxy no --- .../validators/validateInputs.validators.php | 27 +++++++++++++++++++ reg_ajax.php | 22 ++++++--------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/modules/registration/validators/validateInputs.validators.php b/modules/registration/validators/validateInputs.validators.php index 5b2d8b2e..938aab69 100644 --- a/modules/registration/validators/validateInputs.validators.php +++ b/modules/registration/validators/validateInputs.validators.php @@ -109,6 +109,32 @@ function _validateEmail($normalizedInput) { return _createFuncWithResultHelpers($validator)($normalizedInput); } +function _validateGalaxyNo($normalizedInput) { + $validator = function ($input, $resultHelpers) { + $value = $input['galaxyNo']; + + $minGalaxyNo = 1; + $maxGalaxyNo = MAX_GALAXY_IN_WORLD; + + if ($value < $minGalaxyNo) { + return $resultHelpers['createFailure']([ + 'code' => 'GALAXY_NO_TOO_LOW', + 'minLength' => $minGalaxyNo, + ]); + } + if ($value > $maxGalaxyNo) { + return $resultHelpers['createFailure']([ + 'code' => 'GALAXY_NO_TOO_HIGH', + 'maxLength' => $maxGalaxyNo, + ]); + } + + return $resultHelpers['createSuccess']([]); + }; + + return _createFuncWithResultHelpers($validator)($normalizedInput); +} + // Arguments // - $normalizedInput (Object) // @@ -117,6 +143,7 @@ function validateInputs($normalizedInput) { 'username' => _validateUsername($normalizedInput), 'password' => _validatePassword($normalizedInput), 'email' => _validateEmail($normalizedInput), + 'galaxyNo' => _validateGalaxyNo($normalizedInput), ]; } diff --git a/reg_ajax.php b/reg_ajax.php index b5f2cdcf..109cf406 100644 --- a/reg_ajax.php +++ b/reg_ajax.php @@ -73,23 +73,17 @@ $JSONResponse['Errors'][] = 8; $JSONResponse['BadFields'][] = 'email'; break; + case 'GALAXY_NO_TOO_LOW': + $JSONResponse['Errors'][] = 13; + $JSONResponse['BadFields'][] = 'galaxy'; + break; + case 'GALAXY_NO_TOO_HIGH': + $JSONResponse['Errors'][] = 14; + $JSONResponse['BadFields'][] = 'galaxy'; + break; } } - // PreCheck Galaxy - if($GalaxyNo < 1) - { - // Galaxy not given - $JSONResponse['Errors'][] = 13; - $JSONResponse['BadFields'][] = 'galaxy'; - } - else if($GalaxyNo > MAX_GALAXY_IN_WORLD) - { - // GalaxyNo is too high - $JSONResponse['Errors'][] = 14; - $JSONResponse['BadFields'][] = 'galaxy'; - } - // Check if valid language has been selected if(empty($LangCode)) {