Skip to content

Commit

Permalink
improved Validation with more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raaghu committed Apr 18, 2018
1 parent 7326b7d commit 49c4454
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
20 changes: 11 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/RESTFul/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ function required(){
return $this;
}

function ifPresent(){
if($this->continue){
if(!isset($this->data)){
$this->continue = false;
unset($this->parentValidation->data[$this->key]);
}
}
return $this;
}

function defaultValue($value){
if($this->continue){
if(!isset($this->data)){
Expand Down Expand Up @@ -128,6 +138,7 @@ function inRange($min = null,$max = null){
$this->continue = false;
}
}
return $this;
}

function isTimestamp(){
Expand Down
22 changes: 21 additions & 1 deletion tests/RESTFul/TestValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,27 @@ function($validation){
},
['a'=>'invalid']
],

'ifPresent if present and valid'=>[
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'],
function($validation){
$validation->key('a')->ifPresent()->isString()->hasLength(1,100);
},
[]
],
'ifPresent if present and invalid'=>[
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'],
function($validation){
$validation->key('a')->ifPresent()->isString()->hasLength(1,10);
},
['a'=>'invalid']
],
'ifPresent if not present'=>[
['b'=>true,'c'=>'z'],
function($validation){
$validation->key('a')->ifPresent()->isString()->hasLength(1,100);
},
[]
],
];
}

Expand Down

0 comments on commit 49c4454

Please sign in to comment.