diff --git a/build/api/core/Request.php b/build/api/core/Request.php index 2389d7e..ba2bf44 100755 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -327,13 +327,24 @@ if( isset($config['rename']) && is_string($config['rename']) && preg_match('@^\w+$@', $config['rename']) ) $rename = $config['rename']; - /* (2) Gestion du paramètre DEFAULT */ + /* (2) On récupère si le paramètre est optionnel ou pas */ + $optional = isset($config['optional']) && $config['optional'] === true; + + /* (3) Gestion du paramètre DEFAULT */ $default = null; - if( isset($config['default']) && is_string($config['default']) ) + + /* (3.1) Check if default NOT (NULL || FILE) -> matches TYPE */ + if( isset($config['default']) ){ + + /* (3.1.1) Set default value from config */ $default = $config['default']; - /* (3) On récupère si le paramètre est optionnel ou pas */ - $optional = isset($config['optional']) && $config['optional'] === true; + /* (3.1.2) If FILE and not null -> Check type */ + if( $config['type'] != 'FILE' || $default != null ) + if( !Checker::run($config['type'], $default) ) + return $this->error->set(Err::WrongDefaultParam, $rename, $config['type']); + + } /* (4) Si de type 'FILE' + fichier existe => on enregistre la ref. */ if( $config['type'] == 'FILE' && isset($_FILES[$name]) ) @@ -341,7 +352,7 @@ /* (4) Si param obligatoire et manquant -> erreur */ if( !isset($this->raw_params[$name]) && !$optional ) - return $this->error->set(Err::MissingParam, $name); + return $this->error->set(Err::MissingParam, $rename); /* (2.3) Gestion des valeurs @@ -357,7 +368,7 @@ // Si la verification est fausse, on retourne faux if( !Checker::run($config['type'], $this->raw_params[$name]) ) - return $this->error->set(Err::WrongParam, $name, $config['type']); + return $this->error->set(Err::WrongParam, $rename, $config['type']); // Sinon, on ajoute aux params qu'on enverra à l'appel else diff --git a/build/error/core/Err.php b/build/error/core/Err.php index 720190b..d8077d9 100755 --- a/build/error/core/Err.php +++ b/build/error/core/Err.php @@ -61,10 +61,12 @@ const MissingParam = 16; /* (11) Paramètre incorrect */ const WrongParam = 17; - /* (12) Erreur dans le traitement */ - const ModuleError = 18; - /* (13) URI Invalide */ - const InvalidURI = 19; + /* (12) Valeur par défaut incorrecte */ + const WrongDefaultParam = 18; + /* (13) Erreur dans le traitement */ + const ModuleError = 19; + /* (14) URI Invalide */ + const InvalidURI = 20; /* [5] Database @@ -72,50 +74,50 @@ /* (1) Base de données ---------------------------------------------------------*/ /* (1) Erreur lors de la creation d'un objet PDO (connection) */ - const PDOConnection = 20; + const PDOConnection = 21; /* (2) Repositories ---------------------------------------------------------*/ /* (1) Verification de la coherence du chemin (existe dans la conf) */ - const WrongPathRepo = 21; + const WrongPathRepo = 22; /* (2) Module non specifie dans la conf */ - const UnknownRepo = 22; + const UnknownRepo = 23; /* (3) Erreur dans le traitement */ - const RepoError = 23; + const RepoError = 24; /* (3) ORM ---------------------------------------------------------*/ /* (1) Table n'existe pas */ - const UnknownTable = 24; + const UnknownTable = 25; /* (2) Pas permissions de lire le schéma */ - const NotAllowedSchema = 25; + const NotAllowedSchema = 26; /* [6] Erreurs diverses =========================================================*/ /* (1) Aucune donnée trouvée */ - const NoMatchFound = 26; + const NoMatchFound = 27; /* (2) Mauvais chemin de template */ - const UnknownTemplate = 27; + const UnknownTemplate = 28; /* (3) géolocalisation échouée */ - const UnknownAddress = 28; + const UnknownAddress = 29; /* (4) Erreur inconnue */ - const UnknownError = 29; + const UnknownError = 30; /* (5) Entrée existante */ - const AlreadyExists = 30; + const AlreadyExists = 31; /* (6) Corps manquant */ - const MissingBody = 31; + const MissingBody = 32; /* (7) Header manquant */ - const MissingHeaders = 32; + const MissingHeaders = 33; } ?> diff --git a/build/error/core/Error.php b/build/error/core/Error.php index 8f597ff..70cdc65 100755 --- a/build/error/core/Error.php +++ b/build/error/core/Error.php @@ -74,9 +74,10 @@ case Err::UncallableModule: return $this->UncallableModule(); break; case Err::UncallableMethod: return $this->UncallableMethod(); break; case Err::UnknownHttpMethod: return $this->UnknownHttpMethod(); break; - case Err::ConfigError: return $this->ConfigError(); break; + case Err::ConfigError: return $this->ConfigError(); break; case Err::MissingParam: return $this->MissingParam(); break; case Err::WrongParam: return $this->WrongParam(); break; + case Err::WrongDefaultParam: return $this->WrongDefaultParam(); break; case Err::ModuleError: return $this->ModuleError(); break; case Err::InvalidURI: return $this->InvalidURI(); break; case Err::PDOConnection: return $this->PDOConnection(); break; @@ -161,6 +162,14 @@ return 'wrong param \''.$this->arguments[0].'\''; else return 'wrong param'; + }private function WrongDefaultParam(){ + if( count($this->arguments) > 0 ) + if( count($this->arguments) > 1 ) + return 'wrong default param \''.$this->arguments[0].'\' expected to be of type \''.$this->arguments[1].'\''; + else + return 'wrong default param \''.$this->arguments[0].'\''; + else + return 'wrong default param'; }private function ModuleError(){ if( count($this->arguments) > 0 ) return 'module error: \''.$this->arguments[0].'\''; diff --git a/config/modules.json b/config/modules.json index de152cf..26339ba 100755 --- a/config/modules.json +++ b/config/modules.json @@ -3,7 +3,7 @@ "description": "Returns the API documentation", "permissions": [], "parameters": { - "URL0": { "description": "Name of the project to release", "type": "alphanumeric", "rename": "var1", "optional": true, "default": "default_value" } + "URL0": { "description": "Method name", "type": "varchar(1,30)", "rename": "method_name", "optional": true, "default": null, "default": null } } },