From 0cc5056297dfc0d7bd2d5da509ee533daa970564 Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Thu, 6 Jun 2024 17:30:46 +0000 Subject: [PATCH 1/9] ignore test cache --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 83c9b9f..ffbe1ac 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpstan.neon testbench.yaml vendor node_modules +.phpunit.cache From bf160eedc81a67aca58c83f1ea71575bbc13d163 Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Thu, 6 Jun 2024 17:31:01 +0000 Subject: [PATCH 2/9] fix typo's --- config/schema-rules.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/schema-rules.php b/config/schema-rules.php index da0df50..ef15494 100644 --- a/config/schema-rules.php +++ b/config/schema-rules.php @@ -2,7 +2,7 @@ return [ /** - * In MySQL for instance there is no nativ boolean data type. + * In MySQL for instance there is no native boolean data type. * Laravel creates a tinyint(1) if you migrate a boolean. * Switch this off if you want an actual tinyint * validation rule to be generated... @@ -16,7 +16,7 @@ 'string_min_length' => env('SCHEMA_RULES_STRING_MIN_LENGTH', 1), /** - * Always skip this columns + * Always skip these columns */ 'skip_columns' => ['created_at', 'updated_at', 'deleted_at'], From 3136e88b1d39b3ae0ffbcbc48415c026a4e17113 Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Fri, 7 Jun 2024 00:10:06 +0200 Subject: [PATCH 3/9] test laravel 11 --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index abfd087..303ca40 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "brick/varexporter": "^0.3.8", - "doctrine/dbal": "^3.6 || ^4.0", - "illuminate/contracts": "^8.0 || ^9.0 || ^10.0", - "illuminate/database": "^8.0 || ^9.0 || ^10.0", - "illuminate/support": "^8.0 || ^9.0 || ^10.0", - "illuminate/testing": "^8.0 || ^9.0 || ^10.0", + "php": "^7.4 || ^8.0 || ^8.1", + "brick/varexporter": "^0.3.8 || ^4.19.1", + "doctrine/dbal": "^3.6 || ^4.0.2", + "illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/testing": "^8.0 || ^9.0 || ^10.0 || ^11.0", "spatie/laravel-package-tools": "^1.12 || ^1.14" }, "require-dev": { @@ -64,4 +64,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file From 35cdfca322b11bb5784800c8fb39924357d1ea3e Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 14:22:10 +0200 Subject: [PATCH 4/9] Update README - autoformatting +(ToC) - add info on how to run tests --- README.md | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5c157bc..c184088 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![Total Downloads](https://img.shields.io/packagist/dt/laracraft-tech/laravel-schema-rules.svg?style=flat-square)](https://packagist.org/packages/laracraft-tech/laravel-schema-rules) Automatically generate basic Laravel validation rules based on your database table schema! -Use these as a starting point to fine-tune and optimize your validation rules as needed. +Use these as a starting point to fine-tune and optimize your validation rules as needed. Here you can use the web version, if you like: [https://validationforlaravel.com](https://validationforlaravel.com) @@ -27,15 +27,27 @@ php artisan vendor:publish --tag="schema-rules-config" ## ToC -- [`Generate rules for a whole table`](#generate-rules-for-a-whole-table) -- [`Generate rules for specific columns`](#generate-rules-for-specific-columns) -- [`Generate Form Request Class`](#generate-form-request-class) +- [Laravel Schema Rules](#laravel-schema-rules) + - [Installation](#installation) + - [ToC](#toc) + - [Usage](#usage) + - [Generate rules for a whole table](#generate-rules-for-a-whole-table) + - [Generate rules for specific columns](#generate-rules-for-specific-columns) + - [Generate Form Request Class](#generate-form-request-class) + - [Always skip columns](#always-skip-columns) + - [Supported Drivers](#supported-drivers) + - [Testing](#testing) + - [Changelog](#changelog) + - [Contributing](#contributing) + - [Security Vulnerabilities](#security-vulnerabilities) + - [Credits](#credits) + - [License](#license) ## Usage Let's say you've migrated this fictional table: -````php +```php Schema::create('persons', function (Blueprint $table) { $table->id(); $table->string('first_name', 100); @@ -52,7 +64,7 @@ Schema::create('persons', function (Blueprint $table) { $table->unsignedInteger('net_income'); $table->boolean('send_newsletter')->nullable(); }); -```` +``` ### Generate rules for a whole table @@ -61,6 +73,7 @@ Now if you run: `php artisan schema:generate-rules persons` You'll get: + ``` Schema-based validation rules for table "persons" have been generated! Copy & paste these to your controller validation or form request or where ever your validation takes place: @@ -82,7 +95,7 @@ Copy & paste these to your controller validation or form request or where ever y ``` As you may have noticed the float-column `body_size`, just gets generated to `['required', 'numeric']`. -Proper rules for `float`, `decimal` and `double`, are not yet implemented! +Proper rules for `float`, `decimal` and `double`, are not yet implemented! ### Generate rules for specific columns @@ -91,7 +104,8 @@ You can also explicitly specify the columns: `php artisan schema:generate-rules persons --columns first_name,last_name,email` Which gives you: -```` + +``` Schema-based validation rules for table "persons" have been generated! Copy & paste these to your controller validation or form request or where ever your validation takes place: [ @@ -99,20 +113,20 @@ Copy & paste these to your controller validation or form request or where ever y 'last_name' => ['required', 'string', 'min:1', 'max:100'], 'email' => ['required', 'string', 'min:1', 'max:255'] ] -```` +``` ### Generate Form Request Class Optionally, you can add a `--create-request` or `-c` flag, which will create a form request class with the generated rules for you! -```` bash +```bash # creates app/Http/Requests/StorePersonRequest.php (store request is the default) -php artisan schema:generate-rules persons --create-request +php artisan schema:generate-rules persons --create-request # creates/overwrites app/Http/Requests/StorePersonRequest.php php artisan schema:generate-rules persons --create-request --force - + # creates app/Http/Requests/UpdatePersonRequest.php php artisan schema:generate-rules persons --create-request --file UpdatePersonRequest @@ -121,7 +135,7 @@ php artisan schema:generate-rules persons --create-request --file Api\\V1\\Store # creates/overwrites app/Http/Requests/Api/V1/StorePersonRequest.php (using shortcuts) php artisan schema:generate-rules persons -cf --file Api\\V1\\StorePersonRequest -```` +``` ### Always skip columns @@ -131,7 +145,6 @@ To always skip columns add it in the config file, under `skip_columns` parameter 'skip_columns' => ['whatever', 'some_other_column'], ``` - ## Supported Drivers Currently, the supported database drivers are `MySQL`, `PostgreSQL`, and `SQLite`. @@ -141,6 +154,8 @@ the validation rules generated by this package may vary depending on the databas ## Testing +Before running tests, you need to set up a local MySQL database named `laravel_schema_rules` and update its _username_ and _password_ in the `phpunit.xml.dist` file. + ```bash composer test ``` @@ -159,8 +174,8 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [Zacharias Creutznacher](https://github.com/laracraft-tech) -- [All Contributors](../../contributors) +- [Zacharias Creutznacher](https://github.com/laracraft-tech) +- [All Contributors](../../contributors) ## License From aaf59343b9da83f9bccc17118dd465693989892f Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 14:22:40 +0200 Subject: [PATCH 5/9] update composer dependencies for laravel 11 support --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 303ca40..7c99cbc 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^7.4 || ^8.0 || ^8.1", - "brick/varexporter": "^0.3.8 || ^4.19.1", + "brick/varexporter": "^0.3.8 || ^0.5.0", "doctrine/dbal": "^3.6 || ^4.0.2", "illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0", "illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0", @@ -28,7 +28,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", "nunomaduro/larastan": "^1.0 || ^2.5", - "orchestra/testbench": "^6.27 || ^7.0 || ^8.0 ", + "orchestra/testbench": "^6.27 || ^7.0 || ^8.0 || ^9.0 ", "pestphp/pest": "^1.22 || ^2.0", "pestphp/pest-plugin-laravel": "^1.22 || ^2.0", "spatie/laravel-ray": "^1.32" From ec24b39495fae6164829742a2c68f2feffd77717 Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 14:23:33 +0200 Subject: [PATCH 6/9] Update automated tests --- .github/workflows/run-tests.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b90f204..64cea0a 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,9 +14,12 @@ jobs: matrix: os: [ubuntu-latest] php: [8.3, 8.2, 8.1, 8.0, 7.4] - laravel: [10.*, 9.*, 8.*] + laravel: [11.*, 10.*, 9.*, 8.*] stability: [prefer-lowest, prefer-stable] include: + - laravel: 11.* + testbench: 9.* + carbon: ^2.63 - laravel: 10.* testbench: 8.* carbon: ^2.63 @@ -27,6 +30,8 @@ jobs: testbench: 6.27 carbon: ^2.63 exclude: + - laravel: 11.* + php: 8.1 - laravel: 10.* php: 8.0 - laravel: 10.* From 395fb6de2aa4b93424ff8b51e5d5f6beee45a96e Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 14:33:38 +0200 Subject: [PATCH 7/9] Update GitHub Actions workflow to support laravel 11 --- .github/workflows/run-tests.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 64cea0a..7b08e27 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,7 +8,8 @@ on: jobs: test: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest + strategy: fail-fast: true matrix: @@ -32,6 +33,10 @@ jobs: exclude: - laravel: 11.* php: 8.1 + - laravel: 11.* + php: 8.0 + - laravel: 11.* + php: 7.4 - laravel: 10.* php: 8.0 - laravel: 10.* @@ -49,9 +54,11 @@ jobs: MYSQL_PASSWORD: secret MYSQL_DATABASE: laravel_schema_rules MYSQL_ROOT_PASSWORD: secretroot - ports: - - 3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + options: >- + --health-cmd="mysqladmin ping" + --health-interval=10s + --health-timeout=5s + --health-retries=3 steps: - name: Checkout code @@ -62,7 +69,6 @@ jobs: with: php-version: ${{ matrix.php }} extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo - coverage: none - name: Setup problem matchers run: | @@ -82,4 +88,4 @@ jobs: env: DB_USERNAME: user DB_PASSWORD: secret - DB_PORT: ${{ job.services.mysql.ports[3306] }} + DB_PORT: ${{ job.services.mysql.ports['3306'] }} From ca5185c90693ab14ef2a2192c1eccadc68389b4b Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 14:37:58 +0200 Subject: [PATCH 8/9] undo changes to Github test actions --- .github/workflows/run-tests.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7b08e27..ffd4673 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -8,8 +8,7 @@ on: jobs: test: - runs-on: ubuntu-latest - + runs-on: ${{ matrix.os }} strategy: fail-fast: true matrix: @@ -54,11 +53,9 @@ jobs: MYSQL_PASSWORD: secret MYSQL_DATABASE: laravel_schema_rules MYSQL_ROOT_PASSWORD: secretroot - options: >- - --health-cmd="mysqladmin ping" - --health-interval=10s - --health-timeout=5s - --health-retries=3 + ports: + - 3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 steps: - name: Checkout code @@ -69,6 +66,7 @@ jobs: with: php-version: ${{ matrix.php }} extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none - name: Setup problem matchers run: | @@ -88,4 +86,4 @@ jobs: env: DB_USERNAME: user DB_PASSWORD: secret - DB_PORT: ${{ job.services.mysql.ports['3306'] }} + DB_PORT: ${{ job.services.mysql.ports[3306] }} From 2844553db48fc172afc808614c8bc753d06ea0c2 Mon Sep 17 00:00:00 2001 From: Diederik van Remoortere Date: Sat, 8 Jun 2024 15:02:49 +0200 Subject: [PATCH 9/9] rewrite test to be compatible with laravel 11 floating-point-types --- tests/SchemaRulesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SchemaRulesTest.php b/tests/SchemaRulesTest.php index 2370fe4..49e7fec 100644 --- a/tests/SchemaRulesTest.php +++ b/tests/SchemaRulesTest.php @@ -258,11 +258,11 @@ $decimalNullableColumnName ) { $table->float($floatColumnName); - $table->unsignedFloat($floatUnsignedColumnName); + $table->float($floatUnsignedColumnName)->unsigned(); $table->double($doubleColumnName); - $table->unsignedDouble($doubleUnsignedColumnName); + $table->double($doubleUnsignedColumnName)->unsigned(); $table->decimal($decimalColumnName); - $table->unsignedDecimal($decimalUnsignedColumnName); + $table->decimal($decimalUnsignedColumnName)->unsigned(); $table->decimal($decimalNullableColumnName)->nullable(); });