display custom errors on validate insert and fix tests

This commit is contained in:
Eduardo humberto 2017-12-01 11:09:08 -02:00
parent f3ce4a4221
commit 420e4a311f
6 changed files with 28 additions and 8 deletions

View File

@ -137,19 +137,20 @@ class Entity {
) {
$validation = $mapped['validation'];
$prop_value = $this->get_mapped_property($prop);
$message = ( isset( $mapped['on_error'] ) ) ? $mapped['on_error'] : $prop. __(' is invalid', 'tainacan');
if (is_array($prop_value)) {
foreach ($prop_value as $val) {
if (!$validation->validate($val)) {
//
$this->add_error('invalid', $prop . ' is invalid');
$this->add_error('invalid', $message);
$is_valid = false;
}
}
} else {
if (!$validation->validate($prop_value)) {
//
$this->add_error('invalid', $prop . ' is invalid');
$this->add_error('invalid', $message);
$is_valid = false;
}
}

View File

@ -20,8 +20,8 @@ class Metadatas extends Repository {
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
'on_error' => __('The ID should be numeric', 'tainacan'),
'validation' => v::numeric(),
// 'on_error' => __('The ID should be numeric', 'tainacan'),
// 'validation' => v::numeric(),
],
'name' => [
'map' => 'post_title',
@ -44,8 +44,8 @@ class Metadatas extends Repository {
'title' => __('Parent', 'tainacan'),
'type' => 'integer',
'description'=> __('Parent metadata', 'tainacan'),
'on_error' => __('The Parent should be numeric value', 'tainacan'),
'validation' => v::numeric(),
//'on_error' => __('The Parent should be numeric value', 'tainacan'),
//'validation' => v::numeric(),
],
'description' => [
'map' => 'post_content',
@ -60,7 +60,7 @@ class Metadatas extends Repository {
'title' => __('Type', 'tainacan'),
'type' => 'string',
'description'=> __('The metadata type', 'tainacan'),
'on_error' => __('Metadata type is invalid', 'tainacan'),
'on_error' => __('Metadata type is empty', 'tainacan'),
'validation' => v::stringType()->notEmpty(),
],
'required' => [

View File

@ -56,7 +56,13 @@ abstract class Repository {
public function insert($obj) {
// validate
if (!$obj->get_validated()){
throw new \Exception('Entities must be validated before you can save them');
foreach ($obj->get_errors() as $error) {
$type = key($error);
throw new \Exception('Entities must be validated before you can save them'.PHP_EOL.
' Type:'.$type.PHP_EOL.' Message: '.$error[$type] );
}
//throw new \Exception('Entities must be validated before you can save them: '. implode(PHP_EOL,$obj->get_errors()));
// TODO: Throw Warning saying you must validate object before insert()
}

View File

@ -22,6 +22,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection->set_name('teste');
$collection->validate();
@ -31,6 +32,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();
@ -70,6 +72,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection->set_name('teste');
$collection->validate();
@ -80,6 +83,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_required( 'yes' );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();
@ -118,6 +122,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection->set_name('teste');
$collection->validate();
@ -128,6 +133,7 @@ class Item_Metadata extends \WP_UnitTestCase {
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_collection_key( 'yes' );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();

View File

@ -19,6 +19,8 @@ class Items extends \WP_UnitTestCase {
function teste_query(){
global $Tainacan_Collections, $Tainacan_Metadatas, $Tainacan_Item_Metadata;
$type = new \Tainacan\Field_Types\Text();
$collection = new \Tainacan\Entities\Collection();
$collection2 = new \Tainacan\Entities\Collection();
@ -38,16 +40,19 @@ class Items extends \WP_UnitTestCase {
$metadata->set_name('metadado');
$metadata->set_collection( $collection );
$metadata->set_status('publish');
$metadata->set_field_type_object( $type );
$metadata->validate();
$metadata = $Tainacan_Metadatas->insert($metadata);
$metadata2->set_name('metadado2');
$metadata2->set_collection( $collection2 );
$metadata2->set_status('publish');
$metadata2->set_field_type_object( $type );
$metadata2->validate();
$metadata2 = $Tainacan_Metadatas->insert($metadata2);
$metadata3->set_name('metadado3');
$metadata3->set_collection( $collection2 );
$metadata3->set_status('publish');
$metadata3->set_field_type_object( $type );
$metadata3->validate();
$metadata3 = $Tainacan_Metadatas->insert($metadata3);

View File

@ -21,6 +21,7 @@ class Metadata extends \WP_UnitTestCase {
$collection = new \Tainacan\Entities\Collection();
$metadata = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
$collection->set_name('teste');
$collection->validate();
@ -30,6 +31,7 @@ class Metadata extends \WP_UnitTestCase {
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( $collection );
$metadata->set_field_type_object( $type );
//inserindo o metadado
$metadata->validate();