Skip to content

Commit

Permalink
Merge pull request #81 from bim-g/dev
Browse files Browse the repository at this point in the history
Refactor validation API
  • Loading branch information
bim-g committed Sep 26, 2023
2 parents fb248dd + 48923a9 commit c5042e1
Show file tree
Hide file tree
Showing 24 changed files with 365 additions and 355 deletions.
74 changes: 43 additions & 31 deletions example/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,54 @@

$schema = new Schema();
$validate = new Validate();
$source = [
"name" =>"wepesi",
"email" => "[email protected]",
"possessions" => [
"cars" => 2,
"plane" => 0,
"houses" => 4,
"children" => -3,
"children_name" => ['alfa', 3, false],
"children_age" => [12,'6.2', 'rachel'],
$data_source_pass = [
"family" => [
"children" => 3,
"children_name" => ['alfa', 'beta', 'omega'],
"children_age" => [12, 9, 3],
"location" => [
"goma" => "Q.Virunga 08",
"bukabu" => "Bagira 10",
"kinshasa" => "matadi kibala 05"
"bukabu" => "Bagira 10"
]
]
];
$data_source_fail = [
"family" => [
"children" => 3,
"children_name" => ['alfa', 3, 'blue'],
"children_age" => [12, '6.2', 'rachel'],
"location" => [
"goma" => "goma",
"bukabu" => "Bagira",
"kinshasa" => "Gombe ",
"Lubumbashi" => "lushi"
]
]
];

$rules =[
"name" => $schema->string()->min(1)->max(10)->required()->generate(),
"email" => $schema->string()->email()->required()->generate(),
"possessions" => $schema->array()->min(1)->max(20)->required()->structure([
"cars" => $schema->number()->min(1)->required()->generate(),
"plane" => $schema->number()->min(1)->required()->generate(),
"houses" => $schema->number()->min(6)->required()->generate(),
"children" => $schema->number()->positive()->required()->generate(),
'children_name' => $schema->array()->string()->generate(),
'children_age' => $schema->array()->number()->generate(),
"location" => $schema->array()->min(2)->structure([
"goma" => $schema->string()->min(20)->generate(),
"bukabu" => $schema->string()->min(20)->generate(),
"kinshasa" => $schema->string()->min(20)->generate(),
])->generate()
])->generate()
$rules = [
"family" => $schema->array()->min(1)->max(20)->required()->structure([
"children" => $schema->number()->positive()->min(1)->max(5)->required(),
'children_name' => $schema->array()->string(),
'children_age' => $schema->array()->number(),
"location" => $schema->array()->min(2)->max(3)->structure([
"goma" => $schema->string(),
"bukabu" => $schema->string(),
"kinshasa" => $schema->any(),
"Lubumbashi" => $schema->any(),
])
])
];

$validate->check($source, $rules);
//// check if the validation passed or not
include_once __DIR__ . '/vardump.php';
// check if the validation passed or not
$validate->check($data_source_pass, $rules);
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);

$validate->check($data_source_fail, $rules);
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);
9 changes: 6 additions & 3 deletions example/boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
];
//define the schema model for validation
$rules=[
"status" => $schema->boolean()->required()->max(true)->isValid('TRUE')->generate(),
"activated" => $schema->boolean()->required()->min(false)->isValid('FALSE')->generate(),
"status" => $schema->boolean()->required()->isValid('TRUE')->generate(),
"activated" => $schema->boolean()->required()->isValid('FALSE')->generate(),
];

$validate->check($data_source,$rules);
// check if the validation passed or not
include_once __DIR__ . '/vardump.php';
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);
36 changes: 27 additions & 9 deletions example/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@
* Copyright (c) 2022. Wepesi validation.
* @author Boss Ibrahim Mussa
*/
$validate = new \Wepesi\App\Validate();
$schema = new \Wepesi\App\Schema();

$data_source = [
use Wepesi\App\Schema;
use Wepesi\App\Validate;

$validate = new Validate();
$schema = new Schema();

$data_source_pass = [
'birth_day' => date('Y-m-d',strtotime('19years + now')),
'date_created' => date('Y-m-d H:i:s',strtotime('now + 1second'))
];
$data_source_fail = [
'birth_day' => '2021-05-23',
'date_created' => '2021-05-23'
];
$rules=[
"birth_day" => $schema->date()->min("-18years")->required()->generate(),
"date_created" => $schema->date()->now()->max("100years")->required()->generate()
];
$validate->check($data_source,$rules);
$rules = [
'birth_day' => $schema->date()->min("18years")->required()->generate(),
'date_created' => $schema->date()->now()->max("100years")->required()->generate()
];
$validate->check($data_source_pass, $rules);

var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);

$validate->check($data_source_fail, $rules);

include_once __DIR__ . '/vardump.php';
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);
3 changes: 3 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* @author Boss Ibrahim Mussa
*/

use Wepesi\App\Validator\StringValidator;

require_once __DIR__ . '/../vendor/autoload.php';
$data_source = [
"name" => "ibrahim",
"age" => 12,
Expand Down
46 changes: 31 additions & 15 deletions example/number.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,38 @@
* @author Boss Ibrahim Mussa
*/

$validate = new \Wepesi\App\Validate();
$schema = new \Wepesi\App\Schema();
$data_source = [
use Wepesi\App\Schema;
use Wepesi\App\Validate;

$validate = new Validate();
$schema = new Schema();
$data_source_pass = [
"age" => 10,
"length" => 3,
"height" => 10,
"level" => -10,
];

$data_source_fail = [
"age" => 20,
"length" => 0,
"height" =>"35",
"width" =>"",
"direction" => -7
"height" => -35,
"level" => 10,
];
$rules = [
"age" => $schema->number()->min(8)->max(15)->required(),
"length" => $schema->number()->min(1)->max(10),
"height" => $schema->number()->positive(),
"level" => $schema->number()->negative(),
];
$rules=[
"age" => $schema->number()->min(8)->max(15)->required()->generate(),
"length" => $schema->number()->min(1)->max(10)->required()->generate(),
"height" => $schema->number()->min(18)->max(50)->required()->generate(),
"width" => $schema->number()->min(3)->max(50)->required()->generate(),
"direction" => $schema->number()->min(3)->max(50)->positive()->required()->generate(),
];
$validate->check($data_source,$rules);
$validate->check($data_source_pass, $rules);
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);

include_once __DIR__ . '/vardump.php';
$validate->check($data_source_fail, $rules);
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);
46 changes: 33 additions & 13 deletions example/string.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,44 @@

$schema = new Schema();
$validate = new Validate();
$source = [
$source_to_pass = [
'name' => 'ibrahim',
'country' => "",
'country' => null,
'password' => '1234567',
'new_password' => 123456,
'new_password' => 1234567,
'email' => '[email protected]',
'link' => 'https://github.com/bim-g/wepesi_validation/',
'ip' => '127.0.0.1',
];
$source_to_fail = [
'name' => 'ibrahim',
'country' => "",
'password' => '1234567',
'new_password' => 123456,
'email' => '[email protected]',
'link' => 'google',
'ip' => '192.168',
];
$rules = [
"name" => $schema->string()->email()->min(35)->max(50)->required()->generate(),
"country" => $schema->string()->min(30)->max(40)->required()->generate(),
"password" => $schema->string()->min(3)->max(40)->generate(),
"new_password" => $schema->string()->min(3)->max(40)->match("password")->generate(),
"email" => $schema->string()->min(3)->max(40)->email()->generate(),
"link" => $schema->string()->min(3)->max(40)->url()->generate(),
"ip" => $schema->string()->addressIp()->generate()
"name" => $schema->string()->min(3)->max(50)->required(),
"country" => $schema->any(),
"password" => $schema->string()->min(3)->max(40),
"new_password" => $schema->string()->min(3)->max(40)->match("password"),
"email" => $schema->string()->min(10)->max(40)->email(),
"link" => $schema->string()->min(10)->max(45)->url(),
"ip" => $schema->string()->addressIp()
];
$validate->check($source, $rules);
//// check if the validation passed or not
include_once __DIR__."/vardump.php";

$validate->check($source_to_pass, $rules);
// validation pass
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);

// validation fail
$validate->check($source_to_fail, $rules);
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);
10 changes: 0 additions & 10 deletions index.php

This file was deleted.

18 changes: 16 additions & 2 deletions src/MessageErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace Wepesi\App;

use Wepesi\App\Providers\Contracts\MessageBuilderContracts;

/**
*
*/
final class MessageErrorBuilder
final class MessageErrorBuilder implements MessageBuilderContracts
{

/**
Expand Down Expand Up @@ -67,7 +69,19 @@ public function limit(string $value): MessageErrorBuilder
/**
* @return array
*/
public function generate(){
private function generate(){
return $this->items;
}

/**
* @param $method
* @param $arg
* @return mixed|void
*/
public function __call($method, $arg)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $arg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* Copyright (c) 2023. Wepesi validation.
* @author Boss Ibrahim Mussa
*/
var_dump([
'passed' => $validate->passed(),
'errors' => $validate->errors()
]);

namespace Wepesi\App\Providers\Contracts;

interface MessageBuilderContracts
{

}
Loading

0 comments on commit c5042e1

Please sign in to comment.