This is a hacked together prototype for an idea I had, do not rely on this implementation for anything other than research purposes.
Extends the standard Symfony validator to add methods that allow you to generate a valid constraint list from a given data class. This can then be used to validate a data structure such as a request payload without having to map that data onto a concrete instance of the data class (such as an entity or model).
<?php declare(strict_types=1);
use Symfony\Component\Validator\Constraints as Assert;
class Address
{
/**
* @Assert\NotBlank
* @Assert\Type(type="string")
*/
public $postcode;
}
class User
{
/**
* @Assert\NotBlank
* @Assert\Type(type="string")
*/
public $username;
/**
* @Assert\NotBlank
* @Assert\Date
*/
public $dob;
/**
* @Assert\NotNull
* @Assert\Valid
* @Assert\Type(type="Address")
*/
public $address;
}
/** @var ConstraintStructureGeneratingValidator $validator */
$constraintList = $validator->getConstraintStructureFor(User::class);
$validator->validate($request->request->all(), $constraintList);