soyhuce/laravel-validation-rules

A set of useful validation rules for Laravel

1.6.0 2024-09-04 11:50 UTC

This package is auto-updated.

Last update: 2024-11-09 01:02:29 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

Main objective of this package is to provide a set of validation rules for Laravel in order to make it easier to write validation.

Validator::make($data, rules([
	'email' => ['required', DbRules::string()], // ['required', 'string', 'max:255']
	'smaller' => ['nullable', DbRules::string(20)], // ['nullable', 'string', 'max:20']
	'birthday' => [DbRules::date()], // ['date_format:Y-m-d']
	'tiny' => [DbRules::tinyInteger()], // ['integer', 'min:-128', 'max:127']
	// ...
]));

Installation

You can install the package via composer:

composer require soyhuce/laravel-validation-rules

Usage

Available Rules

Database related rules :

  • DbRules::string
  • DbRules::boolean
  • DbRules::enum
  • DbRules::date
  • DbRules::dateTime
  • DbRules::tinyInteger
  • DbRules::unsignedTinyInteger
  • DbRules::smallInteger
  • DbRules::unsignedSmallInteger
  • DbRules::mediumInteger
  • DbRules::unsignedMediumInteger
  • DbRules::integer
  • DbRules::unsignedInteger
  • DbRules::bigInteger
  • DbRules::unsignedBigInteger
  • DbRules::smallIncrements
  • DbRules::mediumIncrements
  • DbRules::increments
  • DbRules::bigIncrements
  • DbRules::float
  • DbRules::double

Miscellaneous rules :

  • MisRules::safePassword : GDPR/CNIL compatible password (at least 12 characters)
  • MisRules::mediumPassword : GDPR/CNIL compatible password (at least 8 characters)

Using in conditional Rule::when

Rules extending CompoundRule cannot be used directly in Rule::when.

You will need to use ... in this case :

'commission_account' => Rule::when(
    fn(Fluent $data) => $data->get('commission') !== null,
-   ['required', DbRules::string()],
+   ['required', ...DbRules::string()],
    'exclude'
),

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.