From ec495c82ce7b7d02e2e952d8b0d688745cced773 Mon Sep 17 00:00:00 2001 From: Myq Larson Date: Thu, 22 Nov 2012 23:23:30 +1300 Subject: [PATCH 1/3] Bugfix: Validator/Datatype: No exception for invalid field types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * invalid field types didn't throw any errors, so errors in schemas, such as `alphanum` instead of `varchar` just caused requests to fail (found out the hard way!) * reformatted `if…else` statements to make comparisons between options more easily comparable --- Validator/Datatype.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Validator/Datatype.php b/Validator/Datatype.php index 5e4ac03..0d5064c 100755 --- a/Validator/Datatype.php +++ b/Validator/Datatype.php @@ -64,28 +64,21 @@ protected function checkLength($limit, $comparator = self::LENGTH_GT, $datatype if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; } elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; } elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; } + else throw new Jade_Exception("Field type not specified"); } // based on the comparator deduce the correct count to compare $count = null; - if($datatype === self::COMPARATOR_ARRAY) - $count = count($this->input); - else if($datatype === self::COMPARATOR_NUMERIC) - $count = $this->input; - else if($datatype === self::COMPARATOR_STRING) - $count = strlen($this->input); - else - $count = $this->input; + if ($datatype === self::COMPARATOR_ARRAY) { $count = count($this->input); } + elseif ($datatype === self::COMPARATOR_NUMERIC) { $count = $this->input; } + elseif ($datatype === self::COMPARATOR_STRING) { $count = strlen($this->input); } + else { $count = $this->input; } //if the comparator is callable (is a function), return the result of that instead, otherwise do the standard op - if(is_callable($datatype)) - return $datatype($comparator, $this->input, $limit); - else if($comparator == self::LENGTH_GT) - return $count >= $limit; - else - return $count <= $limit; - + if (is_callable($datatype)) { return $datatype($comparator, $this->input, $limit); } + elseif ($comparator == self::LENGTH_GT) { return $count >= $limit; } + else { return $count <= $limit; } } protected function checkMembership($container){ From 8ef7ab97c22b0d7d1a242bf3c4c25c71fc9dc39d Mon Sep 17 00:00:00 2001 From: Myq Larson Date: Fri, 23 Nov 2012 13:11:06 +1300 Subject: [PATCH 2/3] Typo: Jade -> Wave --- Validator/Datatype.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Validator/Datatype.php b/Validator/Datatype.php index 0d5064c..1ade3f1 100755 --- a/Validator/Datatype.php +++ b/Validator/Datatype.php @@ -64,7 +64,7 @@ protected function checkLength($limit, $comparator = self::LENGTH_GT, $datatype if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; } elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; } elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; } - else throw new Jade_Exception("Field type not specified"); + else throw new Wave_Exception("Field type not specified"); } // based on the comparator deduce the correct count to compare From 089ee445e51eed55fb5fe1db9a04c2d1a5289584 Mon Sep 17 00:00:00 2001 From: Myq Larson Date: Tue, 26 Feb 2013 13:25:48 +1300 Subject: [PATCH 3/3] Validator: Revert `aefdfb0` & add bug notice * Reverted `aefdfb0` * Documented possible design problem with custom datatypes calling `parent::validate()` due to the assumptions of `checkLength()` which is called by `validate()` --- Validator/Datatype.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Validator/Datatype.php b/Validator/Datatype.php index 1ade3f1..b5a912f 100755 --- a/Validator/Datatype.php +++ b/Validator/Datatype.php @@ -33,6 +33,10 @@ public function __construct($params, $input){ } + /* + * This will not work as intended if a custom datatype calls `parent::validadte()` due to the assumptions of the + * `checkLength()` method + */ public function validate(){ if(isset($this->params[self::MIN_LENGTH]) && !$this->checkLength($this->params[self::MIN_LENGTH])) @@ -61,13 +65,20 @@ public function sanitize(){ protected function checkLength($limit, $comparator = self::LENGTH_GT, $datatype = null){ // establish a comparator if ($datatype == null) { - if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; } - elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; } - elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; } - else throw new Wave_Exception("Field type not specified"); - } - + if (isset($this->params['type'])) { + if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; } + elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; } + elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; } + } else { + // try to guess if not specified + if (is_array( $this->input)) { $datatype = self::COMPARATOR_ARRAY; } + elseif (is_numeric($this->input)) { $datatype = self::COMPARATOR_NUMERIC; } + elseif (is_string( $this->input)) { $datatype = self::COMPARATOR_STRING; } + } + } // based on the comparator deduce the correct count to compare + // how to pass a callable comparator when most custom datatypes simply call `parent::validate()`? No datatype will + // be set in that situation, thus the comparisons below will not produce the intended outcome. $count = null; if ($datatype === self::COMPARATOR_ARRAY) { $count = count($this->input); } elseif ($datatype === self::COMPARATOR_NUMERIC) { $count = $this->input; }