krazydanny/laravel-validator

An Extended Laravel Validator with many useful additional rules!

v1.0.4 2020-12-12 13:22 UTC

This package is auto-updated.

Last update: 2024-05-24 21:28:33 UTC


README

Latest Stable Version Donate License

This package provides an Extended Laravel Validator with many useful additional rules!


Main Advantages

Time-Saving

Extends Laravel's great built-in validator with commonly used validation rules avoiding you to search the internet for "the golden regex" and extending validator yourself :)

Just register the service provider and rules are ready to use!

Excusive validation rules

Some hard-to-find validation rules available for you as if they where included in Laravel.


Installation

The only requirement is to have a Laravel or Lumen project. I asume your are already familiar with one of them, otherwise here are some docs about this great framework:

Laravel version Compatibility

Laravel Package
5.6.x 1.0.4
5.7.x 1.0.4
5.8.x 1.0.4
6.x 1.0.4
7.x 1.0.4

Lumen version Compatibility

Lumen Package
5.6.x 1.0.4
5.7.x 1.0.4
5.8.x 1.0.4
6.x 1.0.4
7.x 1.0.4

Install the package via Composer

$ composer require krazydanny/laravel-validator

Usage

We only have to register the service provider in our project and all new rules are ready to use :)

$app->register( KrazyDanny\Laravel\Validation\ServiceProvider::class );

For example:

$myValidator = Validator::make(
	$values,
	[
		'lat' => 'latitude',
		'lng' => 'longitude',
		'mac' => 'mac_address',
	],
	$messages
);

Rules

Here's a list of all available validation rules:


array_of_regex

Iterates array values verifying each element matches a given regular expression.

Synthax:

array_of_regex

$myValidator = Validator::make(
	$values,
	[
		'attributes' => 'array_of_regex:/(some_regex)/',
	],
	[
		'attributes.array_of_regex' => 'Some message',
	]
);

MATCH examples:

[ 'value A', 'value B', 'value C' ]
[]

NO MATCH examples:

'value A, value B, value C'
"[ 'value A', 'value B', 'value C' ]"
"[]"

boolean_strict

Checks if value's data type is strictly bool.

Synthax:

boolean_strict

$myValidator = Validator::make(
	$values,
	[
		'active' => 'boolean_strict',
	],
	[
		'active.boolean_strict' => 'Some message',
	]
);

MATCH examples:

true
false

NO MATCH examples:

1
0
'true'
'false'

camel_case

Checks if value is a string formatted using camelCase notation.

Synthax:

camel_case

$myValidator = Validator::make(
	$values,
	[
		'var_name' => 'camel_case',
	],
	[
		'var_name.camel_case' => 'Some message',
	]
);

MATCH examples:

'camel',
'camelCase'
'camelCaseN'
'camelCaseNotation'

NO MATCH examples:

'Camel',
'CamelCase',
'camelcase',
'NCamelCase'

color_hex

Checks if a string represents a hexadecimal color code.

Synthax:

color_hex

$myValidator = Validator::make(
	$values,
	[
		'color'  => 'color_hex',
		'colors' => 'color_hex', // also works with an array of values
	],
	[
		'color.color_hex' => 'Some message',
	]
);

MATCH examples:

'#FFFFFF'
'#FF3333'
'#ffffff'
'#5AD66A'

NO MATCH examples:

'FFFFFF'
'#FF33'
'#FF3333A'
'#'

date_gt_min

Checks if a date (value) is greater than another date (first parameter) at least by the given seconds (second parameter).

Third parameter (optional) could be date's (value) format.

Synthax:

date_gt_min:lower_date,diff_in_seconds,format

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_gt_min:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_gt_min' => 'Some message',
	]
);

date_gt_max

Checks if a date (value) is greater than another date (first parameter) by the given seconds as much (second parameter).

Third parameter (optional) could be date's (value) format.

Synthax:

date_gt_max:lower_date,diff_in_seconds,format

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_gt_max:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_gt_max' => 'Some message',
	]
);

date_lt_min

Checks if a date (value) is lower than another date (first parameter) at least by the given seconds (second parameter).

Third parameter (optional) could be date's (value) format.

Synthax:

date_lt_min:greater_date,diff_in_seconds,format

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_lt_min:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_lt_min' => 'Some message',
	]
);

date_lt_max

Checks if a date (value) is lower than another date (first parameter) by the given seconds as much (second parameter).

Third parameter (optional) could be date's (value) format.

Synthax:

date_lt_max:greater_date,diff_in_seconds,format

$myValidator = Validator::make(
	$values,
	[
		'start_at' => 'date_lt_max:2020-03-01 00:00:00,86400',
	],
	[
		'start_at.date_lt_max' => 'Some message',
	]
);

document_number

Checks if value is a valid reference number for any kind of document or paper.

Synthax:

$myValidator = Validator::make(
	$values,
	[
		'id_doc'       => 'document_number',
		'license_num'  => 'document_number',
		'passport_num' => 'document_number',
	],
	[
		'passport_num.document_number' => 'Some message',
	]
);

MATCH examples:

'123123123123'
'abcabcabcabc'
'a1b2c3d4e5f6'
'20-30764053-0'
'20/30764053/0'
'20 30764053 0'
'20/3076 405-30'
'20/3076.405-30'
'a1-b2c3d4e5f-6'

NO MATCH examples:

'20--30764053-0'
'20//30764053/0'
'20  30764053 0'
'20/3076..405-30'
'N° 23123123312'
'n° 23123123312'
'n°23123123312'

float

Checks if value is a floating point number. With the 'strict' parameter also checks the data type.

Synthax:

$myValidator = Validator::make(
	$values,
	[
		'price' => 'float',
		'rate'  => 'float:strict',
	],
	[
		'price.float' => 'Some message',
		'rate.float'  => 'Some message',
	]
);

MATCH examples:

0
100
0.00
0.01
100.00
100.01
'0'
'100'
'0.00'
'0.01'
'100.00'
'100.01'

NO MATCH examples:

'0,00'
'100,00'

geo_distance

Checks if value is an integer representing "earth distance". Value can specify the following units at the end of the value: "km" or "m"

Synthax:

$myValidator = Validator::make(
	$values,
	[
		'distance' => 'geo_distance',
		'radius'   => 'geo_distance',
	],
	[
		'distance.geo_distance' => 'Some message',
		'radius.geo_distance'   => 'Some message',
	]
);

MATCH examples:

0
100
1000
'0'
'1m'
'100km'

NO MATCH examples:

-1
'-1m'
'100k'

kebab_case

Checks if value is a string formatted using kebab-case notation.

Synthax:

kebab_case

$myValidator = Validator::make(
	$values,
	[
		'category' => 'kebab_case',
	],
	[
		'category.kebab_case' => 'Some message',
	]
);

MATCH examples:

'kebabcase',
'kebab-case'
'kebab-case-notation'

NO MATCH examples:

'kebabCase',
'kebab-Case',
'Kebab-case',

latitude

Checks if value is valid latitude (between 90 and -90 degrees).

Synthax:

latitude

$myValidator = Validator::make(
	$values,
	[
		'lat' => 'latitude',
	],
	[
		'lat.latitude' => 'Some message',
	]
);

MATCH examples:

1
90
-90
1.00,
30.010203
-67.50685
90.000000
-80.9999900000999

NO MATCH examples:

91
-91
-100
180.00
90.00000000000001
-90.00000000000001

longitude

Checks if value is valid longitude (between 180 and -180 degrees).

Synthax:

longitude

$myValidator = Validator::make(
	$values,
	[
		'lng' => 'longitude',
	],
	[
		'lng.longitude' => 'Some message',
	]
);

MATCH examples:

1
-180
180.00,
30.010203
-67.50685
90.000000
-179.9999900000999

NO MATCH examples:

181
-181
188.99
200.000
180.00000000000001
-180.00000000000001

mac_address

Checks if value is a valid IEEE 802 MAC Address.

Synthax:

mac_address

$myValidator = Validator::make(
	$values,
	[
		'mac_addr' => 'mac_address',
	],
	[
		'mac_addr.mac_address' => 'Some message',
	]
);

MATCH examples:

'00:00:00:00:00:00'
'EE:EE:EE:EE:EE:EE'
'A1:01:A2:02:A3:03'

NO MATCH examples:

000000000000
'A1:01:A2:02'
'A1.01.A2.02.A3.03'
'A1 01 A2 02 A3 03'
'A1:01:A2:02:A3:03:'

object

Checks if value is an object or an array representing an object.

Synthax:

object

$myValidator = Validator::make(
	$values,
	[
		'attributes' => 'object',
	],
	[
		'attributes.object' => 'Some message',
	]
);

MATCH examples:

[ 'fieldA' => 'value A', 'fieldB' => "value B" ]
new stdClass
(object)[ 'field' => 'value' ]

NO MATCH examples:

[ 'value A', 'value B' ]
"{}"

pascal_case

Checks if value is a string formatted using PascalCase notation.

Synthax:

pascal_case

$myValidator = Validator::make(
	$values,
	[
		'class_name' => 'pascal_case',
	],
	[
		'class_name.pascal_case' => 'Some message',
	]
);

MATCH examples:

'Pascal'
'Pascalcase'
'PascalCase'
'PascalCaseN'
'PascalCaseNotation'

NO MATCH examples:

'pascal'
'pascalCase'
'pascalcase'
'nPascalCase'

snake_case

Checks if value is a string formatted using snake_case notation.

Synthax:

snake_case

$myValidator = Validator::make(
	$values,
	[
		'param_name' => 'snake_case',
	],
	[
		'param_name.snake_case' => 'Some message',
	]
);

MATCH examples:

'snakecase'
'snake_case'
'snake_case_n'
'n_snake_case'
'snake_case_notation'

NO MATCH examples:

'Snake_case'
'snake_Case'
'snake_CaseNotation'
'N_snake_case'
'snake_caseN'
'snake_case_N'