weew/config-schema

Schema builder for the weew/config package.

v1.4.2 2016-08-28 20:26 UTC

This package is not auto-updated.

Last update: 2024-12-21 20:52:19 UTC


README

Build Status Code Quality Test Coverage Version Licence

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();
}