Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes/validation #14

Merged
merged 10 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<?php

$finder = PhpCsFixer\Finder::create()
$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude('assets')
->exclude('var')
->exclude('bin')
->exclude('config')
->exclude('docker')
->exclude('files')
->exclude('public')
->exclude('scripts')
->exclude('templates')
->exclude('tools')
->exclude('translations')
->exclude('var')
->exclude('vendor');

return (new PhpCsFixer\Config())
Expand Down Expand Up @@ -41,6 +34,7 @@
'phpdoc_types' => false,
'increment_style' => false,
'ordered_class_elements' => true,
'fully_qualified_strict_types' => false,
'nullable_type_declaration_for_default_null_value' => false,
'nullable_type_declaration' => ['syntax' => 'union'],
'ordered_types' => ['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
Expand Down
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Breaking changes

## 2.x -> 3.0

* Variables being coerced no longer validate constraints from type properties, unless
they're being validated from `FindByX` attributes.

This behavior makes the bundle behave more in-line with how Symfony itself validates objects.

* Attribute `Valid` must now be used to validate sub-dtos as expected.

* Context in validation should now always be valid.
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Symfony DTO Bundle

![Code Coverage](https://camo.githubusercontent.com/ffe54b3b9a48d4d6bd374e2630b48e088c99858500db95ebed37184e8c1a6a3b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6465253230436f7665726167652d38342532352d737563636573733f7374796c653d666c6174)
![Code Coverage](https://camo.githubusercontent.com/e8b50014309ca69d187dae1eb8f3a522910f0c6971e42404e768a79fa2f2b505/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6465253230436f7665726167652d38342532352d737563636573733f7374796c653d666c6174)
[![Packagist Downloads](https://img.shields.io/packagist/dt/dualmedia/symfony-request-dto-bundle)](https://packagist.org/packages/dualmedia/symfony-request-dto-bundle)

This bundle aims to lower the burden of typechecking, casting, loading entities
Expand All @@ -22,6 +22,10 @@ return [
];
```

## Upgrades

See [CHANGES.md](CHANGES.md)

## Usage

1. Create a DTO class for your request
Expand All @@ -33,10 +37,10 @@ use \DualMedia\DtoRequestBundle\Model\AbstractDto;

class MyDto extends AbstractDto
{
public ?int $myVar = null;
public int|null $myVar = null;

#[Path("custom_path")]
public ?string $myString = null;
public string|null $myString = null;
}

```
Expand All @@ -54,6 +58,77 @@ class MyController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractCo
}
```

## Application wide handling of DTO issues

If you wish to automatically return a 4XX response code when a dto has failed validation you may use something like the following:

```yaml
# config/services.yaml
App\EventSubscriber\ErrorSubscriber:
decorates: exception_listener
arguments:
- '@App\EventSubscriber\ErrorSubscriber.inner'
```

```php
class ErrorSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
public function __construct(
private readonly ErrorListener $decorated
) {
}

public static function getSubscribedEvents(){
return [
\Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent::class => 'onControllerArguments',
];
}

public function onControllerArguments(
ControllerArgumentsEvent $event
): void {
$this->decorated->onControllerArguments($event);

$violationList = new ConstraintViolationList();

foreach ($event->getArguments() as $argument) {
if ($argument instanceof DtoInterface
&& !$argument->isOptional()
&& !$argument->isValid()) {
$violationList->addAll($argument->getConstraintViolationList());
}
}

if (0 !== $violationList->count()) {
throw new ValidatorException($violationList); // handle and display, or just do whatever really
}
}
}
```

If you want to map a class-wide assert to a path without having to directly modify the constraint itself you may wrap it in MappedToPath

```php

use \DualMedia\DtoRequestBundle\Constraints\MappedToPath;
use \DualMedia\DtoRequestBundle\Model\AbstractDto;
use Symfony\Component\Validator\Constraints as Assert;

#[MappedToPath(
'property',
new Assert\Expression(
'this.property != null',
message: 'This property cannot be null'
)
)]
class MyDto extends AbstractDto
{
public int|null $property = null;
}

```

## Docs


Currently no documentation is available, but will be added in the future. For the time being see [the DTO models for tests](tests/Fixtures/Model/Dto)
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"nelmio/api-doc-bundle": "^4",
"symfony/stopwatch": "^5.4|^6.2",
"symfony/expression-language": "^5.4|^6.2",
"symfony/web-profiler-bundle": "^5.4|^6.2",
"doctrine/doctrine-bundle": "^2",
"doctrine/orm": "^2|^3"
Expand Down
Loading
Loading