Skip to content

Commit

Permalink
add has_validator method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon committed Apr 11, 2024
1 parent 6ce1114 commit a15ae93
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1950,4 +1950,15 @@ protected function validate_valid_array_size_equal($field, array $input, array $
{
return !(!is_array($input[$field]) || count($input[$field]) != $params[0]);
}

/**
* Checks if a validator method exists for a given rule.
*
* @param string $rule
* @return bool
*/
public static function has_validator(string $rule): bool
{
return method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule]);
}
}
66 changes: 66 additions & 0 deletions tests/StaticHasValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests;

use GUMP;

class StaticHasValidatorTest extends BaseTestCase
{
public function testHasValidatorWhenExists(): void
{
$validationRules = [
'required',
'contains',
'contains_list',
'doesnt_contain_list',
'boolean',
'valid_email',
'max_len',
'min_len',
'exact_len',
'between_len',
'alpha',
'alpha_numeric',
'alpha_dash',
'alpha_numeric_dash',
'alpha_numeric_space',
'alpha_space',
'numeric',
'integer',
'float',
'valid_url',
'url_exists',
'valid_ip',
'valid_ipv4',
'valid_ipv6',
'valid_cc',
'valid_name',
'street_address',
'iban',
'date',
'min_age',
'max_numeric',
'min_numeric',
'starts',
'required_file',
'extension',
'equalsfield',
'guidv4',
'phone_number',
'regex',
'valid_json_string',
'valid_array_size_greater',
'valid_array_size_lesser',
'valid_array_size_equal',
];

foreach ($validationRules as $rule) {
$this->assertTrue(GUMP::has_validator($rule));
}
}

public function testHasValidatorWhenNotExists(): void
{
$this->assertFalse(GUMP::has_validator('custom_rule'));
}
}

0 comments on commit a15ae93

Please sign in to comment.