diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b90f204..ffd4673 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,12 @@ jobs: testbench: 6.27 carbon: ^2.63 exclude: + - laravel: 11.* + php: 8.1 + - laravel: 11.* + php: 8.0 + - laravel: 11.* + php: 7.4 - laravel: 10.* php: 8.0 - laravel: 10.* 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 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 diff --git a/composer.json b/composer.json index abfd087..7c99cbc 100644 --- a/composer.json +++ b/composer.json @@ -16,19 +16,19 @@ } ], "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 || ^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", + "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": { "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" @@ -64,4 +64,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file 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'], 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(); });