Skip to content

Commit

Permalink
add new validator (#562)
Browse files Browse the repository at this point in the history
* add new validator

* add tests

* add tests

* de language key

* update docs

* Text updates

* German translation

* add missing translation files

* add changelog

Co-authored-by: Roland Schaub <roland.schaub@zephir.ch>
  • Loading branch information
nadar and rolandschaub authored Sep 15, 2020
1 parent 735dd72 commit 1d5faa8
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE
+ [#533](https://github.com/luyadev/luya-module-admin/pull/553) Use new `Yii::$app->getAdminModulesMenus()`, `Yii::$app->getAdminModulesJsTranslationMessages()` and `Yii::$app->getAdminModulesAssets()` method in order to retrieve module data. This fixes a bug with admin modules which does not have an `admin` in the module name f.e. `'usertoken' => 'luya\admin\usertoken\Module'`.
+ [#561](https://github.com/luyadev/luya-module-admin/pull/561) Disable LUYA admin auth checks when cors is enabled and request method is options.
+ [#562](https://github.com/luyadev/luya-module-admin/pull/562) New `luya\admin\validators\I18nRequiredValidator` validator in order to validate i18n attributes an its content. The validator checks if all language short codes exist and if the corresponding value is empty.

## 3.5.2 (26. August 2020)

Expand Down
6 changes: 6 additions & 0 deletions src/messages/cn/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/de/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'Die angegebene E-Mail-Adresse wird bereits für ein gelöschtes Konto verwendet.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'Das Attribut \"{attribute}\" muss vom Typ Array sein.',
'i18n_required_validator_missing_key' => 'Der Sprach-Key \"{lang}\" ist erforderlich.',
'i18n_required_validator_invalid_empty_value' => 'Der Wert für die Sprache \"{lang}\" kann nicht leer sein.',
];
6 changes: 6 additions & 0 deletions src/messages/el/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/es/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/fa/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/fr/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/it/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/nl/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/pl/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/pt/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/ru/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/th/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/tr/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/ua/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
6 changes: 6 additions & 0 deletions src/messages/vi/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,10 @@
// 3.4.0

'user_model_email_deleted_account_exists' => 'The provided email address is already in use by a deleted account.',

// 3.6.0

'i18n_required_validator_invalid_format' => 'The given attribute \"{attribute}\" must be type of array.',
'i18n_required_validator_missing_key' => 'The language key \"{lang}\" is missing and is required.',
'i18n_required_validator_invalid_empty_value' => 'The value for language \"{lang}\" can not be empty.',
];
74 changes: 74 additions & 0 deletions src/validators/I18nRequiredValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace luya\admin\validators;

use luya\admin\models\Lang;
use luya\admin\Module;
use luya\helpers\Json;
use yii\validators\Validator;

/**
* Validate Required i18n Attributes.
*
* Ensure the i18n attribute is correctly and also checks if all language keys are given and the values for each language is not empty.
*
* Example usage:
*
* ```php
* [['title', 'location'], I18nRequiredValidator::class],
* ```
*
* In {{luya\admin\ngrest\base\NgRestModel}} scenarios it might be common to use
*
* ```php
* [$this->i18n, I18nRequiredValidator::class],
* ```
*
* The validator will only validate if the attribute is available, therfore set the required validator if the attribute is required.
*
* @author Basil Suter <git@nadar.io>
* @since 3.6.0
*/
class I18nRequiredValidator extends Validator
{
/**
* @var string Message if the input format is invalid. This message will be passed trough Yii::t.
*/
public $invalidFormatMessage = "i18n_required_validator_invalid_format";

/**
* @var string Message if an language key does not exists in the array. This message will be passed trough Yii::t.
*/
public $missingKeyMessage = "i18n_required_validator_missing_key";

/**
* @var string Message if a given value is empty. This message will be passed trough Yii::t.
*/
public $emptyValueMessage = "i18n_required_validator_invalid_empty_value";

/**
* {@inheritDoc}
*/
public function validateAttribute($model, $attribute)
{
$array = $model->{$attribute};

// As due to the ngrest plugin concept the value is already parsed from array to json.
if (Json::isJson($array)) {
$array = Json::decode($array);
}

if (!is_array($array)) {
return $this->addError($model, $attribute, Module::t($this->invalidFormatMessage, ['attribute' => $attribute]));
}

/** @var $langShortCode The language short code */
foreach (Lang::find()->select(['short_code'])->asArray()->column() as $langShortCode) {
if (!array_key_exists($langShortCode, $array)) {
$this->addError($model, $attribute, Module::t($this->missingKeyMessage, ['lang' => $langShortCode]));
} elseif (empty($array[$langShortCode])) {
$this->addError($model, $attribute, Module::t($this->emptyValueMessage, ['lang' => $langShortCode]));
}
}
}
}
63 changes: 63 additions & 0 deletions tests/admin/validators/I18nRequiredValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace luya\admin\tests\admin\validators;

use admintests\AdminModelTestCase;
use luya\admin\validators\I18nRequiredValidator;
use luya\base\DynamicModel;

class I18nRequiredValidatorTest extends AdminModelTestCase
{
public function testValidatorInvalidFormat()
{
$model = new DynamicModel(['i18n' => null]);
$validator = new I18nRequiredValidator();
$validator->validateAttribute($model, 'i18n');
$this->assertSame('The given attribute \"i18n\" must be type of array.', $model->getFirstError('i18n'));
}

public function testValidatorMissingLanguage()
{
$this->createAdminLangFixture([
1 => [
'id' => 1,
'short_code' => 'en',
'is_deleted' => 0,
]
]);
$model = new DynamicModel(['i18n' => []]);
$validator = new I18nRequiredValidator();
$validator->validateAttribute($model, 'i18n');
$this->assertSame('The language key \"en\" is missing and is required.', $model->getFirstError('i18n'));
}

public function testValidatorMissingLanguageAsJson()
{
$this->createAdminLangFixture([
1 => [
'id' => 1,
'short_code' => 'en',
'is_deleted' => 0,
]
]);
$model = new DynamicModel(['i18n' => '{}']);
$validator = new I18nRequiredValidator();
$validator->validateAttribute($model, 'i18n');
$this->assertSame('The language key \"en\" is missing and is required.', $model->getFirstError('i18n'));
}

public function testValidatorEmptyLanguage()
{
$this->createAdminLangFixture([
1 => [
'id' => 1,
'short_code' => 'en',
'is_deleted' => 0,
]
]);
$model = new DynamicModel(['i18n' => ['en' => '']]);
$validator = new I18nRequiredValidator();
$validator->validateAttribute($model, 'i18n');
$this->assertSame('The value for language \"en\" can not be empty.', $model->getFirstError('i18n'));
}
}

0 comments on commit 1d5faa8

Please sign in to comment.