Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Fixed #2 Validation issues with types/required. Added new validation …
Browse files Browse the repository at this point in the history
…tests, and changed the name of validations to valid.type, valid.required
  • Loading branch information
timothymarois committed Jul 29, 2017
1 parent 443a182 commit 59f9742
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 24 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ $db = new \Filebase\Database([
'pretty' => true,
'validate' => [
'name' => [
'type' => 'string',
'required' => true
'valid.type' => 'string',
'valid.required' => true
]
]
]);
Expand Down Expand Up @@ -227,21 +227,21 @@ $db = new \Filebase\Database([
'dir' => '/path/to/database/dir',
'validate' => [
'name' => [
'type' => 'string',
'required' => true
'valid.type' => 'string',
'valid.required' => true
],
'description' => [
'type' => 'string',
'required' => false
'valid.type' => 'string',
'valid.required' => false
],
'emails' => [
'type' => 'array',
'required' => true
'valid.type' => 'array',
'valid.required' => true
],
'config' => [
'settings' => [
'type' => 'array',
'required' => true
'valid.type' => 'array',
'valid.required' => true
]
]
]
Expand All @@ -254,8 +254,8 @@ In the above example `name`, `description`, `emails` and `config` array keys wou

|Name |Allowed Values |Description |
|--- |--- |--- |
|`type` |`string`, `str`, `integer`, `int`, `array` |Checks if the property is the current type |
|`required` |`true`, `false` |Checks if the property is on the document |
|`valid.type` |`string`, `str`, `integer`, `int`, `array` |Checks if the property is the current type |
|`valid.required` |`true`, `false` |Checks if the property is on the document |


## (7) Custom Filters
Expand Down
8 changes: 7 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ public function __construct($config)
*/
protected function validateFormatClass()
{
if (!class_exists($this->format))
{
throw new \Exception('Filebase Error: Missing format class in config.');
}

$format_class = new $this->format;

if (!$format_class instanceof Format\FormatInterface)
{
throw new \Exception('Format Class must be an instance of Filebase\Format\FormatInterface');
throw new \Exception('Filebase Error: Format Class must be an instance of Filebase\Format\FormatInterface');
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function validateLoop($document,$object,$rules)
{
foreach($rules as $key => $rule)
{
if (!isset($rule['type'],$rule['required']) && isset($document[$key]))
if ( (!isset($rule['valid.type']) ) && isset($document[$key]))
{
self::validateLoop($document[$key],$object,$rules[$key]);

Expand All @@ -62,16 +62,16 @@ public static function validateLoop($document,$object,$rules)
public static function validateRules($document,$key,$rules,$object)
{
// checks variable type
if (isset($document[$key],$rules['type']))
if (isset($document[$key],$rules['valid.type']))
{
if (!self::checkType($document[$key],$rules['type']))
if (!self::checkType($document[$key],$rules['valid.type']))
{
throw new \Exception('Validation Failed setting variable on '.$object->getId().' - ['.$key.'] does not match type '.$rules['type']);
throw new \Exception('Validation Failed setting variable on '.$object->getId().' - ['.$key.'] does not match type '.$rules['valid.type']);
}
}

// check if variable is required
if (isset($rules['required']) && $rules['required']===true)
if (isset($rules['valid.required']) && $rules['valid.required']===true)
{
if (!isset($document[$key]))
{
Expand Down
200 changes: 194 additions & 6 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
class ValidationTest extends \PHPUnit\Framework\TestCase
{

public function testValidatingStringRequired()
public function testValidatingStringRequiredGood()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'name' => [
'type' => 'string',
'required' => true
'valid.type' => 'string',
'valid.required' => true
]
]
]);
Expand All @@ -24,16 +24,77 @@ public function testValidatingStringRequired()
}


public function testValidatingNestedString()
public function testStringRequiredBad()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'name' => [
'valid.type' => 'string',
'valid.required' => true
]
]
]);

$db->get('test')->set(['name'=>123])->save();

$db->flush(true);
}


public function testOnlyRequiredGood()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'name' => [
'valid.required' => true
]
]
]);

$db->get('test')->set(['name'=>'value'])->save();

$this->assertEquals(true, true);

$db->flush(true);
}


public function testOnlyRequiredBad()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'this_is_new' => [
'valid.required' => true
]
]
]);

$db->get('test')->set(['name'=>'value'])->save();

$this->assertEquals(true, true);

$db->flush(true);
}



public function testNestedString()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'type' => 'array',
'aboutme' => [
'type' => 'string',
'required' => true
'valid.type' => 'string',
'valid.required' => true
]
]
]
Expand All @@ -54,4 +115,131 @@ public function testValidatingNestedString()
$db->flush(true);
}


public function testNestedBad()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'type' => 'array',
'aboutme' => [
'valid.type' => 'string',
'valid.required' => true
]
]
]
]);


$array = [
'profile' => [
'aboutme' => 321456
]
];


$db->get('test')->set($array)->save();

$db->flush(true);
}


public function testArrayGood()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'valid.type' => 'array'
]
]
]);


$array = [
'profile' => [
"1","2","3"
]
];

$db->get('test')->set($array)->save();

$this->assertEquals(true, true);

$db->flush(true);
}


public function testArrayBad()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'valid.type' => 'array'
]
]
]);


$array = [
'profile' => 123
];

$db->get('test')->set($array)->save();

$db->flush(true);
}

public function testIntBad()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'valid.type' => 'int'
]
]
]);


$array = [
'profile' => '123'
];

$db->get('test')->set($array)->save();

$db->flush(true);
}

public function testIntGood()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'validate' => [
'profile' => [
'valid.type' => 'int'
]
]
]);


$array = [
'profile' => 123
];

$db->get('test')->set($array)->save();

$this->assertEquals(true, true);

$db->flush(true);
}

}

0 comments on commit 59f9742

Please sign in to comment.