Skip to content

Commit 49c4454

Browse files
committed
improved Validation with more tests
1 parent 7326b7d commit 49c4454

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

composer.lock

+11-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RESTFul/Validation.php

+11
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ function required(){
8787
return $this;
8888
}
8989

90+
function ifPresent(){
91+
if($this->continue){
92+
if(!isset($this->data)){
93+
$this->continue = false;
94+
unset($this->parentValidation->data[$this->key]);
95+
}
96+
}
97+
return $this;
98+
}
99+
90100
function defaultValue($value){
91101
if($this->continue){
92102
if(!isset($this->data)){
@@ -128,6 +138,7 @@ function inRange($min = null,$max = null){
128138
$this->continue = false;
129139
}
130140
}
141+
return $this;
131142
}
132143

133144
function isTimestamp(){

tests/RESTFul/TestValidation.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,27 @@ function($validation){
229229
},
230230
['a'=>'invalid']
231231
],
232-
232+
'ifPresent if present and valid'=>[
233+
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'],
234+
function($validation){
235+
$validation->key('a')->ifPresent()->isString()->hasLength(1,100);
236+
},
237+
[]
238+
],
239+
'ifPresent if present and invalid'=>[
240+
['a'=>'Raghavendragmail.com','b'=>true,'c'=>'z'],
241+
function($validation){
242+
$validation->key('a')->ifPresent()->isString()->hasLength(1,10);
243+
},
244+
['a'=>'invalid']
245+
],
246+
'ifPresent if not present'=>[
247+
['b'=>true,'c'=>'z'],
248+
function($validation){
249+
$validation->key('a')->ifPresent()->isString()->hasLength(1,100);
250+
},
251+
[]
252+
],
233253
];
234254
}
235255

0 commit comments

Comments
 (0)