Skip to content

Commit

Permalink
Add ushahidi/kohana-validation package and use that
Browse files Browse the repository at this point in the history
This replaces the dependencies on Kohana Core.

- Update Validators to refer to namespaced Valid and Validation class
- Fix DI for ValidationEngineTrait
- Set up translations
- Move error messages to laravel language files
  • Loading branch information
rjmackay committed Jun 8, 2017
1 parent e96cee8 commit a2a99a7
Show file tree
Hide file tree
Showing 36 changed files with 194 additions and 93 deletions.
2 changes: 0 additions & 2 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public static function init()
return Kohana::$config->load('site.intercomAppToken');
});

$di->set('tool.validation', $di->lazyNew('Ushahidi_ValidationEngine'));

$di->set('tool.mailer', $di->lazyNew('Ushahidi_Mailer', [
'siteConfig' => $di->lazyGet('site.config'),
'clientUrl' => $di->lazyGet('clienturl')
Expand Down
52 changes: 0 additions & 52 deletions application/classes/Ushahidi/ValidationEngine.php

This file was deleted.

7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
{
"type": "vcs",
"url": "https://github.com/ushahidi/ohanzee-db"
},
{
"type": "vcs",
"url": "https://github.com/ushahidi/kohana-validation"
}
],
"require": {
Expand Down Expand Up @@ -48,7 +52,8 @@
"ramsey/uuid": "^3.4.1",
"sentry/sentry": "~1.5",
"dusterio/lumen-passport": "^0.1.9",
"barryvdh/laravel-cors": "^0.9.2"
"barryvdh/laravel-cors": "^0.9.2",
"ushahidi/kohana-validation": "dev-master"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
40 changes: 38 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'providerNotAvailable' => 'One or more of the enabled providers is not available :param1',
Expand Down
2 changes: 1 addition & 1 deletion application/messages/csv.php → resources/lang/en/csv.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'range' => 'The file size should be less than :param3 KB',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'invalidFormStageId' => 'form_stage_id :value does not exist',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'not_empty' => ':field must not be empty',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'roles' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return array(
'dataUrlOrMediaRequired' => 'One of data_url or media_id is required',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'size_error' => 'The file size should be less than :param1 MB',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'contact' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'email' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return [
'publishedPostsLimitReached' => 'limit::posts',
Expand Down
3 changes: 1 addition & 2 deletions application/messages/tag.php → resources/lang/en/tag.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return array(
'isSlugAvailable' => ':field :value is already in use',
'description.regex' => 'The description must contain only letters, numbers, spaces and punctuation',
'tag.regex' => 'The category name must contain only letters, numbers, spaces and punctuation',
);

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php defined('SYSPATH') OR die('No direct script access.');
<?php

return array(
'isUniqueEmail' => ':field is already in use',
Expand Down
29 changes: 29 additions & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php defined('SYSPATH') or die('No direct script access.');

return array(

'alpha' => ':field must contain only letters',
'alpha_dash' => ':field must contain only numbers, letters and dashes',
'alpha_numeric' => ':field must contain only letters and numbers',
'color' => ':field must be a color',
'credit_card' => ':field must be a credit card number',
'date' => ':field must be a date',
'decimal' => ':field must be a decimal with :param2 places',
'digit' => ':field must be a digit',
'email' => ':field must be an email address',
'email_domain' => ':field must contain a valid email domain',
'equals' => ':field must equal :param2',
'exact_length' => ':field must be exactly :param2 characters long',
'in_array' => ':field must be one of the available options',
'ip' => ':field must be an ip address',
'matches' => ':field must be the same as :param3',
'min_length' => ':field must be at least :param2 characters long',
'max_length' => ':field must not exceed :param2 characters long',
'not_empty' => ':field must not be empty',
'numeric' => ':field must be numeric',
'phone' => ':field must be a phone number',
'range' => ':field must be within the range of :param2 to :param3',
'regex' => ':field does not match the required format',
'url' => ':field must be a url',

);
12 changes: 8 additions & 4 deletions src/App/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
})
];

// ValidationEngine
$di->set('tool.validation', $di->lazyNew(Ushahidi\App\Validator\KohanaValidationEngine::class, [
// Inject laravel translator
'translator' => $di->lazy(function () {
return app('translator');
})
]));

// Defined memcached
$di->set('memcached', $di->lazy(function () use ($di) {
$config = $di->get('ratelimiter.config');
Expand Down Expand Up @@ -205,10 +213,6 @@
'update' => $di->lazyNew(Ushahidi\App\Validator\Permission\Update::class),
];

// Validation Trait
$di->setter['Ushahidi\Core\Tool\ValidationEngineTrait']['setValidation'] = $di->newFactory('Ushahidi_ValidationEngine');
$di->params['Ushahidi_ValidationEngine']['array'] = [];

// Formatter mapping
$di->params['Ushahidi\Factory\FormatterFactory']['map'] = [
'config' => $di->lazyNew(Ushahidi\App\Formatter\Config::class),
Expand Down
2 changes: 1 addition & 1 deletion src/App/Validator/Contact/Receive.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function validContact($contact, $data, $validation)
// Valid Email?
if (isset($data['type']) and
$data['type'] == Contact::EMAIL and
! \Valid::email($contact) ) {
! \Kohana\Validation\Valid::email($contact) ) {
return $validation->error('contact', 'invalid_email', [$contact]);
} elseif (isset($data['type']) and
$data['type'] == Contact::PHONE ) {
Expand Down
2 changes: 1 addition & 1 deletion src/App/Validator/Contact/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function validContact($contact, $data, $validation)
// Valid Email?
if (isset($data['type']) and
$data['type'] == Contact::EMAIL and
! \Valid::email($contact) ) {
! \Kohana\Validation\Valid::email($contact) ) {
return $validation->error('contact', 'invalid_email', [$contact]);
} // Valid Phone?
// @todo Look at using libphonenumber to validate international numbers
Expand Down
2 changes: 1 addition & 1 deletion src/App/Validator/Form/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function getRules()
];
}

public function checkPostTypeLimit(\Validation $validation)
public function checkPostTypeLimit(\Kohana\Validation\Validation $validation)
{
if ($this->limits['forms'] !== true) {
$total_forms = $this->repo->getTotalCount();
Expand Down
81 changes: 81 additions & 0 deletions src/App/Validator/KohanaValidationEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Ushahidi\App\Validator;

/**
* Ushahidi Core Validation Tool
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Core
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html
* GNU Affero General Public License Version 3 (AGPL3)
*/

use Kohana\Validation\Validation as KohanaValidation;
use Ushahidi\Core\Tool\ValidationEngine;
use Illuminate\Contracts\Translation\Translator;

class KohanaValidationEngine extends KohanaValidation implements ValidationEngine
{
/**
* @var TranslatorContract
*/
private $translator;

public function __construct(Translator $translator)
{
$this->translator = $translator;

parent::__construct([], $this->getTranslationCallback());
}

public function getTranslationCallback()
{
return function ($file, $field, $error = null) {
if ($error) {
return $this->translator->has("$file.$field.$error") ?
$this->translator->trans("$file.$field.$error") : false;
} else {
return $this->translator->has("$file.$field") ?
$this->translator->trans("$file.$field") : false;
}
};
}

public function setData(array $data)
{
$this->_data = $data;
}

public function getData($key = null)
{
if ($key === null) {
return $this->_data;
}

if (array_key_exists($key, $this->_data)) {
return $this->_data[$key];
}

return null;
}

public function setFullData(array $fullData)
{
$this->bind(':fulldata', $fullData);
}

public function getFullData($key = null)
{
if ($key === null) {
return $this->_bound[':fulldata'];
}

if (array_key_exists($key, $this->_bound[':fulldata'])) {
return $this->_bound[':fulldata'][$key];
}

return null;
}
}
Loading

0 comments on commit a2a99a7

Please sign in to comment.