From f71ba1b3f261cd5365f7c31e82af7e56ed2eb83e Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 12 Sep 2024 16:59:05 +0200 Subject: [PATCH 1/5] feat(laravel): validations rules filters --- .../ParameterValidationResourceMetadataCollectionFactory.php | 1 + src/Laravel/workbench/app/Models/Book.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index a92fd20535..b19b621813 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -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, diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index c458cb68c4..353218dd60 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -46,8 +46,8 @@ new GetCollection(), ] )] -#[QueryParameter(key: 'isbn', filter: PartialSearchFilter::class, constraints: 'min:2')] -#[QueryParameter(key: 'name', filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' =>'9793877138395'], filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'name', schema: ['minLength' => '4'], filter: PartialSearchFilter::class)] #[QueryParameter(key: 'author', filter: EqualsFilter::class)] #[QueryParameter(key: 'publicationDate', filter: DateFilter::class, property: 'publication_date')] #[QueryParameter(key: 'publicationDateWithNulls', filter: DateFilter::class, property: 'publication_date', filterContext: ['include_nulls' => true])] From 98687dd0cd31b8a1ddc0290e4893ee271e6b35af Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 13 Sep 2024 16:13:48 +0200 Subject: [PATCH 2/5] cs fixes --- src/Laravel/workbench/app/Models/Book.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index 353218dd60..5d4f25aff0 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -46,7 +46,7 @@ new GetCollection(), ] )] -#[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' =>'9793877138395'], filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' => '9793877138395'], filter: PartialSearchFilter::class)] #[QueryParameter(key: 'name', schema: ['minLength' => '4'], filter: PartialSearchFilter::class)] #[QueryParameter(key: 'author', filter: EqualsFilter::class)] #[QueryParameter(key: 'publicationDate', filter: DateFilter::class, property: 'publication_date')] From b16547e7cc8445c7ffda6c6f2b6c675bbf0f7536 Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 16 Sep 2024 14:05:18 +0200 Subject: [PATCH 3/5] fix(laravel): eloquent filters validation --- ...rameterValidationResourceMetadataCollectionFactory.php | 8 ++++---- src/Laravel/workbench/app/Models/Book.php | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index b19b621813..fd82353c63 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -107,15 +107,15 @@ private function addSchemaValidation(Parameter $parameter): Parameter $assertions[] = 'lte:'.$schema['maximum']; } - if (isset($schema['pattern'])) { - $assertions[] = 'regex:'.$schema['pattern']; + if (isset($schema['regexPattern'])) { + $assertions[] = 'regex:'.$schema['regexPattern']; } $minLength = isset($schema['minLength']); $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) { @@ -126,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) { diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index 5d4f25aff0..2cee1251b9 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -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; @@ -47,8 +48,8 @@ ] )] #[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' => '9793877138395'], filter: PartialSearchFilter::class)] -#[QueryParameter(key: 'name', schema: ['minLength' => '4'], filter: PartialSearchFilter::class)] -#[QueryParameter(key: 'author', filter: EqualsFilter::class)] +#[QueryParameter(key: 'name', schema: ['regexPattern' => '/^a/'], filter: PartialSearchFilter::class)] +#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: true), 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')] From afed672d40338bf7ade362539f51dbfcaf5097ca Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 16 Sep 2024 14:32:16 +0200 Subject: [PATCH 4/5] fix(laravel): eloquent filters --- .../ParameterValidationResourceMetadataCollectionFactory.php | 4 ++-- src/Laravel/workbench/app/Models/Book.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php index fd82353c63..0395aec730 100644 --- a/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Laravel/Metadata/ParameterValidationResourceMetadataCollectionFactory.php @@ -107,8 +107,8 @@ private function addSchemaValidation(Parameter $parameter): Parameter $assertions[] = 'lte:'.$schema['maximum']; } - if (isset($schema['regexPattern'])) { - $assertions[] = 'regex:'.$schema['regexPattern']; + if (isset($schema['pattern'])) { + $assertions[] = 'regex:'.$schema['pattern']; } $minLength = isset($schema['minLength']); diff --git a/src/Laravel/workbench/app/Models/Book.php b/src/Laravel/workbench/app/Models/Book.php index 2cee1251b9..5a57211368 100644 --- a/src/Laravel/workbench/app/Models/Book.php +++ b/src/Laravel/workbench/app/Models/Book.php @@ -48,8 +48,8 @@ ] )] #[QueryParameter(key: 'isbn', schema: ['minimum' => '9783877138395', 'maximum' => '9793877138395'], filter: PartialSearchFilter::class)] -#[QueryParameter(key: 'name', schema: ['regexPattern' => '/^a/'], filter: PartialSearchFilter::class)] -#[QueryParameter(key: 'author', openApi: new Parameter(name: 'author', in: 'query', allowEmptyValue: true), filter: EqualsFilter::class, required: true)] +#[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')] From 36788c51f75d09cd7c110870dfa4d8363fabc21e Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 17 Sep 2024 10:29:35 +0200 Subject: [PATCH 5/5] test(laravel): journal model validation --- src/Laravel/workbench/app/Models/Journal.php | 40 +++++++++++++++++++ .../database/factories/JournalFactory.php | 39 ++++++++++++++++++ ...024_09_16_142930_create_journals_table.php | 34 ++++++++++++++++ .../database/seeders/DatabaseSeeder.php | 2 + 4 files changed, 115 insertions(+) create mode 100644 src/Laravel/workbench/app/Models/Journal.php create mode 100644 src/Laravel/workbench/database/factories/JournalFactory.php create mode 100644 src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php diff --git a/src/Laravel/workbench/app/Models/Journal.php b/src/Laravel/workbench/app/Models/Journal.php new file mode 100644 index 0000000000..ce3b9b04ce --- /dev/null +++ b/src/Laravel/workbench/app/Models/Journal.php @@ -0,0 +1,40 @@ + '/^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); + } +} diff --git a/src/Laravel/workbench/database/factories/JournalFactory.php b/src/Laravel/workbench/database/factories/JournalFactory.php new file mode 100644 index 0000000000..bbe444f05b --- /dev/null +++ b/src/Laravel/workbench/database/factories/JournalFactory.php @@ -0,0 +1,39 @@ + + */ +class JournalFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = Journal::class; + + /** + * Define the model's default state. + * + * @return array + */ + 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(), + ]; + } +} diff --git a/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php b/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php new file mode 100644 index 0000000000..2079bf3761 --- /dev/null +++ b/src/Laravel/workbench/database/migrations/2024_09_16_142930_create_journals_table.php @@ -0,0 +1,34 @@ +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'); + } +}; diff --git a/src/Laravel/workbench/database/seeders/DatabaseSeeder.php b/src/Laravel/workbench/database/seeders/DatabaseSeeder.php index 2bd7066968..0591f7e5cf 100644 --- a/src/Laravel/workbench/database/seeders/DatabaseSeeder.php +++ b/src/Laravel/workbench/database/seeders/DatabaseSeeder.php @@ -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; @@ -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();