Tagged union FormRequest Rule Validation #54880
-
As the title suggests, the feature would implement currently, the way I've found to do something ""Similar"", but Isn't right, is the following: <?php
namespace App\Http\Requests;
use App\Util\OpenAPIGenerator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use OpenAPI\Client\Model\ModelServices;
use OpenAPI\Client\Model\Params;
class StoreWorkflowServices extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
/** @var Params[] */
$paramsRules = [];
$modelServiceValues = array_map(fn($case) => $case->value, ModelServices::cases());
foreach ($modelServiceValues as $modelService) {
$modelClass = '\\OpenAPI\\Client\\Model\\' . $modelService . 'Params';
/** @var Params */
$params = new $modelClass();
$paramServices = $params->getServiceAllowableValues();
$formats = $params->openAPIFormats();
foreach ($params->openAPITypes() as $key => $type) {
$paramsRules["params.$modelService.$key"] = match ($key) {
Params::DISCRIMINATOR => ['required', Rule::in($paramServices)],
default => OpenAPIGenerator::mapTypeToRule($type, $formats[$key])
};
}
}
return array_merge([
Params::DISCRIMINATOR => ['required', Rule::enum(\OpenAPI\Client\Model\ModelServices::class)]
], $paramsRules);
}
} Using the PHPNextGen generator from OpenAPIGenerator Example: OpenAPITools/openapi-generator#20343
generatorName: php-nextgen
outputDir: core/client
globalProperties:
models: ""
apis: ""
supportingFiles: "README.md,ApiException.php,Configuration.php,HeaderSelector.php,ObjectSerializer.php,ModelInterface.php" openapi-generator-cli generate -i Untitled-1.json -c openapitools.config.yml |
Beta Was this translation helpful? Give feedback.
Answered by
brianferri
Mar 14, 2025
Replies: 1 comment
Answer selected by
brianferri
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#55191