weew / config-schema
Schema builder for the weew/config package.
v1.4.2
2016-08-28 20:26 UTC
Requires
- weew/config: ^1.18
- weew/validator: ^3.7
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ^2.0
- phpspec/phpspec: ^2.4
- satooshi/php-coveralls: ^0.6.1
- weew/helpers-phpspec: ^1.0
README
Table of contents
Installation
composer require weew/config-schema
Introduction
This package allows easy config validation and is used in combination with the weew/config package.
Usage
You can describe your config schema like this:
$config = new Config([ 'some' => 'value', 'items' => ['foo', 'bar'], 'name' => 'John Doe', ]); $schema = new ConfigSchema($config); $schema ->hasValue('some') ->hasArray('items')->allowed(['foo', 'baz']) ->hasString('name')->min(3)->max(10) ;
After you've described your schema, you can either validate it, which will return you an instance of IValidationResult
, or assert it, which will throw an exception.
$result = $schema->check(); foreach ($result->getErrors() as $error) { echo $error->getSubject() . ' ' . $error->getMessage(); } // or try { $schema->assert(); } catch (ConfigValidationException $ex) { $result = $ex->getValidationResult(); }