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

Add support for Laravel 11 #26

Merged
merged 9 commits into from
Jun 10, 2024
Merged
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
11 changes: 10 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
.phpunit.cache
47 changes: 31 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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);
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -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

Expand All @@ -91,28 +104,29 @@ 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:
[
'first_name' => ['required', 'string', 'min:1', 'max:100'],
'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

Expand All @@ -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

Expand All @@ -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`.
Expand All @@ -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
```
Expand All @@ -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

Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -64,4 +64,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
4 changes: 2 additions & 2 deletions config/schema-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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'],

Expand Down
6 changes: 3 additions & 3 deletions tests/SchemaRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down