Skip to content

Commit c5042e1

Browse files
authored
Merge pull request #81 from bim-g/dev
Refactor validation API
2 parents fb248dd + 48923a9 commit c5042e1

24 files changed

+365
-355
lines changed

example/arrays.php

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,54 @@
99

1010
$schema = new Schema();
1111
$validate = new Validate();
12-
$source = [
13-
"name" =>"wepesi",
14-
"email" => "info@wepesi.com",
15-
"possessions" => [
16-
"cars" => 2,
17-
"plane" => 0,
18-
"houses" => 4,
19-
"children" => -3,
20-
"children_name" => ['alfa', 3, false],
21-
"children_age" => [12,'6.2', 'rachel'],
12+
$data_source_pass = [
13+
"family" => [
14+
"children" => 3,
15+
"children_name" => ['alfa', 'beta', 'omega'],
16+
"children_age" => [12, 9, 3],
2217
"location" => [
2318
"goma" => "Q.Virunga 08",
24-
"bukabu" => "Bagira 10",
25-
"kinshasa" => "matadi kibala 05"
19+
"bukabu" => "Bagira 10"
20+
]
21+
]
22+
];
23+
$data_source_fail = [
24+
"family" => [
25+
"children" => 3,
26+
"children_name" => ['alfa', 3, 'blue'],
27+
"children_age" => [12, '6.2', 'rachel'],
28+
"location" => [
29+
"goma" => "goma",
30+
"bukabu" => "Bagira",
31+
"kinshasa" => "Gombe ",
32+
"Lubumbashi" => "lushi"
2633
]
2734
]
2835
];
2936

30-
$rules =[
31-
"name" => $schema->string()->min(1)->max(10)->required()->generate(),
32-
"email" => $schema->string()->email()->required()->generate(),
33-
"possessions" => $schema->array()->min(1)->max(20)->required()->structure([
34-
"cars" => $schema->number()->min(1)->required()->generate(),
35-
"plane" => $schema->number()->min(1)->required()->generate(),
36-
"houses" => $schema->number()->min(6)->required()->generate(),
37-
"children" => $schema->number()->positive()->required()->generate(),
38-
'children_name' => $schema->array()->string()->generate(),
39-
'children_age' => $schema->array()->number()->generate(),
40-
"location" => $schema->array()->min(2)->structure([
41-
"goma" => $schema->string()->min(20)->generate(),
42-
"bukabu" => $schema->string()->min(20)->generate(),
43-
"kinshasa" => $schema->string()->min(20)->generate(),
44-
])->generate()
45-
])->generate()
37+
$rules = [
38+
"family" => $schema->array()->min(1)->max(20)->required()->structure([
39+
"children" => $schema->number()->positive()->min(1)->max(5)->required(),
40+
'children_name' => $schema->array()->string(),
41+
'children_age' => $schema->array()->number(),
42+
"location" => $schema->array()->min(2)->max(3)->structure([
43+
"goma" => $schema->string(),
44+
"bukabu" => $schema->string(),
45+
"kinshasa" => $schema->any(),
46+
"Lubumbashi" => $schema->any(),
47+
])
48+
])
4649
];
4750

48-
$validate->check($source, $rules);
49-
//// check if the validation passed or not
50-
include_once __DIR__ . '/vardump.php';
51+
// check if the validation passed or not
52+
$validate->check($data_source_pass, $rules);
53+
var_dump([
54+
'passed' => $validate->passed(),
55+
'errors' => $validate->errors()
56+
]);
57+
58+
$validate->check($data_source_fail, $rules);
59+
var_dump([
60+
'passed' => $validate->passed(),
61+
'errors' => $validate->errors()
62+
]);

example/boolean.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
];
1313
//define the schema model for validation
1414
$rules=[
15-
"status" => $schema->boolean()->required()->max(true)->isValid('TRUE')->generate(),
16-
"activated" => $schema->boolean()->required()->min(false)->isValid('FALSE')->generate(),
15+
"status" => $schema->boolean()->required()->isValid('TRUE')->generate(),
16+
"activated" => $schema->boolean()->required()->isValid('FALSE')->generate(),
1717
];
1818

1919
$validate->check($data_source,$rules);
2020
// check if the validation passed or not
21-
include_once __DIR__ . '/vardump.php';
21+
var_dump([
22+
'passed' => $validate->passed(),
23+
'errors' => $validate->errors()
24+
]);

example/date.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
* Copyright (c) 2022. Wepesi validation.
44
* @author Boss Ibrahim Mussa
55
*/
6-
$validate = new \Wepesi\App\Validate();
7-
$schema = new \Wepesi\App\Schema();
86

9-
$data_source = [
7+
use Wepesi\App\Schema;
8+
use Wepesi\App\Validate;
9+
10+
$validate = new Validate();
11+
$schema = new Schema();
12+
13+
$data_source_pass = [
14+
'birth_day' => date('Y-m-d',strtotime('19years + now')),
15+
'date_created' => date('Y-m-d H:i:s',strtotime('now + 1second'))
16+
];
17+
$data_source_fail = [
1018
'birth_day' => '2021-05-23',
1119
'date_created' => '2021-05-23'
1220
];
13-
$rules=[
14-
"birth_day" => $schema->date()->min("-18years")->required()->generate(),
15-
"date_created" => $schema->date()->now()->max("100years")->required()->generate()
16-
];
17-
$validate->check($data_source,$rules);
21+
$rules = [
22+
'birth_day' => $schema->date()->min("18years")->required()->generate(),
23+
'date_created' => $schema->date()->now()->max("100years")->required()->generate()
24+
];
25+
$validate->check($data_source_pass, $rules);
26+
27+
var_dump([
28+
'passed' => $validate->passed(),
29+
'errors' => $validate->errors()
30+
]);
31+
32+
$validate->check($data_source_fail, $rules);
1833

19-
include_once __DIR__ . '/vardump.php';
34+
var_dump([
35+
'passed' => $validate->passed(),
36+
'errors' => $validate->errors()
37+
]);

example/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* @author Boss Ibrahim Mussa
55
*/
66

7+
use Wepesi\App\Validator\StringValidator;
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
710
$data_source = [
811
"name" => "ibrahim",
912
"age" => 12,

example/number.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,38 @@
44
* @author Boss Ibrahim Mussa
55
*/
66

7-
$validate = new \Wepesi\App\Validate();
8-
$schema = new \Wepesi\App\Schema();
9-
$data_source = [
7+
use Wepesi\App\Schema;
8+
use Wepesi\App\Validate;
9+
10+
$validate = new Validate();
11+
$schema = new Schema();
12+
$data_source_pass = [
13+
"age" => 10,
14+
"length" => 3,
15+
"height" => 10,
16+
"level" => -10,
17+
];
18+
19+
$data_source_fail = [
1020
"age" => 20,
1121
"length" => 0,
12-
"height" =>"35",
13-
"width" =>"",
14-
"direction" => -7
22+
"height" => -35,
23+
"level" => 10,
24+
];
25+
$rules = [
26+
"age" => $schema->number()->min(8)->max(15)->required(),
27+
"length" => $schema->number()->min(1)->max(10),
28+
"height" => $schema->number()->positive(),
29+
"level" => $schema->number()->negative(),
1530
];
16-
$rules=[
17-
"age" => $schema->number()->min(8)->max(15)->required()->generate(),
18-
"length" => $schema->number()->min(1)->max(10)->required()->generate(),
19-
"height" => $schema->number()->min(18)->max(50)->required()->generate(),
20-
"width" => $schema->number()->min(3)->max(50)->required()->generate(),
21-
"direction" => $schema->number()->min(3)->max(50)->positive()->required()->generate(),
22-
];
23-
$validate->check($data_source,$rules);
31+
$validate->check($data_source_pass, $rules);
32+
var_dump([
33+
'passed' => $validate->passed(),
34+
'errors' => $validate->errors()
35+
]);
2436

25-
include_once __DIR__ . '/vardump.php';
37+
$validate->check($data_source_fail, $rules);
38+
var_dump([
39+
'passed' => $validate->passed(),
40+
'errors' => $validate->errors()
41+
]);

example/string.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,44 @@
1212

1313
$schema = new Schema();
1414
$validate = new Validate();
15-
$source = [
15+
$source_to_pass = [
1616
'name' => 'ibrahim',
17-
'country' => "",
17+
'country' => null,
1818
'password' => '1234567',
19-
'new_password' => 123456,
19+
'new_password' => 1234567,
2020
'email' => 'infos@wepesi.com',
2121
'link' => 'https://github.com/bim-g/wepesi_validation/',
22+
'ip' => '127.0.0.1',
23+
];
24+
$source_to_fail = [
25+
'name' => 'ibrahim',
26+
'country' => "",
27+
'password' => '1234567',
28+
'new_password' => 123456,
29+
'email' => 'i@w.com',
30+
'link' => 'google',
2231
'ip' => '192.168',
2332
];
2433
$rules = [
25-
"name" => $schema->string()->email()->min(35)->max(50)->required()->generate(),
26-
"country" => $schema->string()->min(30)->max(40)->required()->generate(),
27-
"password" => $schema->string()->min(3)->max(40)->generate(),
28-
"new_password" => $schema->string()->min(3)->max(40)->match("password")->generate(),
29-
"email" => $schema->string()->min(3)->max(40)->email()->generate(),
30-
"link" => $schema->string()->min(3)->max(40)->url()->generate(),
31-
"ip" => $schema->string()->addressIp()->generate()
34+
"name" => $schema->string()->min(3)->max(50)->required(),
35+
"country" => $schema->any(),
36+
"password" => $schema->string()->min(3)->max(40),
37+
"new_password" => $schema->string()->min(3)->max(40)->match("password"),
38+
"email" => $schema->string()->min(10)->max(40)->email(),
39+
"link" => $schema->string()->min(10)->max(45)->url(),
40+
"ip" => $schema->string()->addressIp()
3241
];
33-
$validate->check($source, $rules);
34-
//// check if the validation passed or not
35-
include_once __DIR__."/vardump.php";
42+
43+
$validate->check($source_to_pass, $rules);
44+
// validation pass
45+
var_dump([
46+
'passed' => $validate->passed(),
47+
'errors' => $validate->errors()
48+
]);
49+
50+
// validation fail
51+
$validate->check($source_to_fail, $rules);
52+
var_dump([
53+
'passed' => $validate->passed(),
54+
'errors' => $validate->errors()
55+
]);

index.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/MessageErrorBuilder.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace Wepesi\App;
88

9+
use Wepesi\App\Providers\Contracts\MessageBuilderContracts;
10+
911
/**
1012
*
1113
*/
12-
final class MessageErrorBuilder
14+
final class MessageErrorBuilder implements MessageBuilderContracts
1315
{
1416

1517
/**
@@ -67,7 +69,19 @@ public function limit(string $value): MessageErrorBuilder
6769
/**
6870
* @return array
6971
*/
70-
public function generate(){
72+
private function generate(){
7173
return $this->items;
7274
}
75+
76+
/**
77+
* @param $method
78+
* @param $arg
79+
* @return mixed|void
80+
*/
81+
public function __call($method, $arg)
82+
{
83+
if (method_exists($this, $method)) {
84+
return call_user_func_array([$this, $method], $arg);
85+
}
86+
}
7387
}

example/vardump.php renamed to src/Providers/Contracts/MessageBuilderContracts.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
* Copyright (c) 2023. Wepesi validation.
44
* @author Boss Ibrahim Mussa
55
*/
6-
var_dump([
7-
'passed' => $validate->passed(),
8-
'errors' => $validate->errors()
9-
]);
6+
7+
namespace Wepesi\App\Providers\Contracts;
8+
9+
interface MessageBuilderContracts
10+
{
11+
12+
}

0 commit comments

Comments
 (0)