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

Feat/laravel parameter validator #6620

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter
{
$schema = $parameter->getSchema();
$required = $parameter->getRequired();

$openApi = $parameter->getOpenApi();

// When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
Expand Down Expand Up @@ -114,7 +115,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter
$maxLength = isset($schema['maxLength']);

if ($minLength && $maxLength) {
$assertions[] = \sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']);
$assertions[] = sprintf('between:%s,%s', $schema['minLength'], $schema['maxLength']);
} elseif ($minLength) {
$assertions[] = 'min:'.$schema['minLength'];
} elseif ($maxLength) {
Expand All @@ -125,7 +126,7 @@ private function addSchemaValidation(Parameter $parameter): Parameter
$maxItems = isset($schema['maxItems']);

if ($minItems && $maxItems) {
$assertions[] = \sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']);
$assertions[] = sprintf('between:%s,%s', $schema['minItems'], $schema['maxItems']);
} elseif ($minItems) {
$assertions[] = 'min:'.$schema['minItems'];
} elseif ($maxItems) {
Expand Down
7 changes: 4 additions & 3 deletions src/Laravel/workbench/app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\OpenApi\Model\Parameter;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -46,9 +47,9 @@
new GetCollection(),
]
)]
#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')]
#[QueryParameter(key: 'name', filter: PartialSearchFilter::class)]
#[QueryParameter(key: 'author', filter: EqualsFilter::class)]
#[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' => '9793877138395'], filter: PartialSearchFilter::class)]
#[QueryParameter(key: 'name', schema: ['pattern' => '/^a/'], filter: PartialSearchFilter::class)]
#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: false), filter: EqualsFilter::class, required: true)]
#[QueryParameter(key: 'publicationDate', filter: DateFilter::class, property: 'publication_date')]
#[QueryParameter(key: 'publicationDateWithNulls', filter: DateFilter::class, property: 'publication_date', filterContext: ['include_nulls' => true])]
#[QueryParameter(key: 'isbn_range', filter: RangeFilter::class, property: 'isbn')]
Expand Down
40 changes: 40 additions & 0 deletions src/Laravel/workbench/app/Models/Journal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Workbench\App\Models;

use ApiPlatform\Laravel\Eloquent\Filter\EqualsFilter;
use ApiPlatform\Laravel\Eloquent\Filter\OrFilter;
use ApiPlatform\Laravel\Eloquent\Filter\PartialSearchFilter;
use ApiPlatform\Laravel\Eloquent\Filter\RangeFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\OpenApi\Model\Parameter;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[ApiResource]
#[QueryParameter(key: 'journalName', schema: ['pattern' => '/^a/'], filter: PartialSearchFilter::class, property: 'journal_name')]
#[QueryParameter(key: 'journalNameLengthValidation', schema: ['minLength' => '3'], filter: PartialSearchFilter::class, property: 'journal_name')]
#[QueryParameter(key: 'number', schema: ['exclusiveMinimum' => '0', 'maximum' => '30'], filter: RangeFilter::class)]
#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: true), filter: EqualsFilter::class, required: true)]
#[QueryParameter(
key: 'name2',
filter: new OrFilter(new EqualsFilter()),
property: 'name',
schema: ['minItems' => 2]
)]
class Journal extends Model
{
use HasFactory;
use HasUlids;

protected $visible = ['journal_name', 'title', 'number', 'publication_date'];
protected $fillable = ['journal_name'];

public function author(): BelongsTo
{
return $this->belongsTo(Journal::class);
}
}
39 changes: 39 additions & 0 deletions src/Laravel/workbench/database/factories/JournalFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Symfony\Component\Uid\Ulid;
use Workbench\App\Models\Journal;

/**
* @template TModel of \Workbench\App\Models\Journal
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
class JournalFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<TModel>
*/
protected $model = Journal::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'journal_name' => fake()->name(),
'id' => (string) new Ulid(),
'author_id' => AuthorFactory::new(),
'title' => fake()->title(),
'number' => fake()->numberBetween(1,30),
'publication_date' => fake()->optional()->date(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('journals', function (Blueprint $table): void {
$table->ulid('id')->primary();
$table->string('journal_name');
$table->string('title');
$table->integer('number')->nullable();
$table->date('publication_date')->nullable();
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('authors');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('journals');
}
};
2 changes: 2 additions & 0 deletions src/Laravel/workbench/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;
use Workbench\Database\Factories\CommentFactory;
use Workbench\Database\Factories\JournalFactory;
use Workbench\Database\Factories\PostFactory;
use Workbench\Database\Factories\SluggableFactory;
use Workbench\Database\Factories\UserFactory;
Expand All @@ -29,6 +30,7 @@ class DatabaseSeeder extends Seeder
public function run(): void
{
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
JournalFactory::new()->has(AuthorFactory::new())->count(10)->create();
PostFactory::new()->has(CommentFactory::new()->count(10))->count(10)->create();
SluggableFactory::new()->count(10)->create();
UserFactory::new()->create();
Expand Down
Loading