composer-config-plugin icon indicating copy to clipboard operation
composer-config-plugin copied to clipboard

Config validation

Open xepozz opened this issue 5 years ago • 2 comments

Would be nice to have an ability to check all config before error will be caught in runtime. I think we can implement it with console command (or add after rebuild process) with usage http://github.com/yiisoft/validator.

Action plan:

  • [ ] Decide about format
  • [ ] Implement feature
  • [ ] Make documentation
  • [ ] Cover all yii-* packages configs

Welcome to discussion.

xepozz avatar Aug 08 '20 18:08 xepozz

https://github.com/yiisoft/composer-config-plugin#known-issues

I've imagined something like this: every package can define own config validators:

"extra": {
    "config-plugin": {
        "params": "config/params.php",
        "common": "config/common.php"
   },
   "config-plugin-validators": {
     "SomePackage\\MyConfigValidator": {
        "configNames": ["common", "web"],
        "otherOption": "value"
     },
   },
},

use Yiisoft\Composer\Config\Config\ValidatorInterface;

class MyConfigValidator implements ValidatorInterface
{
  public function __construct(array $options);

  public function isApplicable($configName): boolean
  {
     return in_array($configName, $this->configNames, true);
  }

  public function validate($configName, $configArray): void
  {
     if (empty($configArray['importantOption'])) {
        throw Exception('importantOption must be set');
     }
  }
}

Not sure about configNames. Just first thoughts...

hiqsol avatar Aug 15 '20 19:08 hiqsol

I think after switching to .php configs it could be like this:

return [
  'configs' => [
    'params' => [
      'params.php',

      [
        'name' => 'params-local.php',
        'validator' => ParamsLocalValidator::class,
      ],
      // or
      'params-local.php' => [
        'validator' => ParamsLocalValidator::class,
      ],
    ]
  ]
]

So json format can be similar

xepozz avatar Aug 16 '20 08:08 xepozz