Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Unified ConstraintViolationList Normalizer

License

Notifications You must be signed in to change notification settings

easir/error-normalizer-bundle

Repository files navigation

Unified Error Normalizer Bundle

Symfony bundle which provides Unified Error Normalizer allowing to easily normalize/denormalize ConstraintViolationList from Symfony's Validator component.

Status

This package is currently in the active development.

Requirements

Example

Normalization example:

use Easir\ErrorNormalizerBundle\NameConverter\FieldNameConverter;
use Easir\ErrorNormalizerBundle\Normalizer\UnifiedConstraintViolationListNormalizer;
use Symfony\Component\Validator\ConstraintViolationList;

$normalizer = new UnifiedConstraintViolationListNormalizer(new FieldNameConverter());

/** @var Symfony\Component\Validator\Validator\ValidatorInterface $validator */
$constraintViolationList = $validator->validate($data);
$errors = $this->normalizer->normalize($data, ConstraintViolationList::class);

Normalization process will return an array in format like below:

$errors = [
    'errors' => [
        [
            'message' => 'Error message here',
            'code' => 'validation.email',
            'field' => 'contact.email',
        ],
    ],
];

Note! This bundle provides Symfony's DependencyInjection service definition which means it should be possible to use Symfony's Serializer component without explicitly creating the normalizer.

Installation

This bundle is being served only from private repository therefore it is required to add proper repository into your composer.json file.

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/easir/error-normalizer-bundle.git"
        }
    ]
}

Require the bundle implementation with Composer:

composer require easir/error-normalizer-bundle

Development

Install all the needed packages required to develop the project:

composer install

Note! This bundle is developed against different versions of Symfony, please remember to run the tests with lowest dependencies. You can easily achieve that by running dependency installation with --prefer-lowest.

Testing

You can run the test suite using the following command:

vendor/bin/phpunit

Code Style

This bundle enforces the Easir Code Standards during development using the PHP CS Fixer utility. Before committing any code, you can run the utility so it can fix any potential rule violations for you:

vendor/bin/phpcs

Reporting issues

Use the issue tracker to report any issues you might have.

License

See the LICENSE file for license rights and limitations (MIT).