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

New generic string validation rule. #1957

Merged
merged 2 commits into from
Apr 20, 2019
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
17 changes: 17 additions & 0 deletions system/Validation/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ public function alpha_numeric_space(string $str = null): bool

//--------------------------------------------------------------------

/**
* Any type of string
*
* Note: we specifically do NOT type hint $str here so that
* it doesn't convert numbers into strings.
*
* @param string|null $str
*
* @return boolean
*/
public function string($str = null): bool
{
return is_string($str);
}

//--------------------------------------------------------------------

/**
* Decimal number
*
Expand Down
41 changes: 41 additions & 0 deletions tests/system/Validation/FormatRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,47 @@ public function ipProvider()

//--------------------------------------------------------------------

/**
* @dataProvider stringProvider
*
* @param $str
* @param $expected
*/
public function testString($str, $expected)
{
$data = [
'foo' => $str,
];

$this->validation->setRules([
'foo' => 'string',
]);

$this->assertEquals($expected, $this->validation->run($data));
}

//--------------------------------------------------------------------

public function stringProvider()
{
return [
[
'123',
true,
],
[
123,
false,
],
[
'hello',
true,
],
];
}

//--------------------------------------------------------------------

/**
* @dataProvider alphaProvider
*
Expand Down
37 changes: 18 additions & 19 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,31 +660,30 @@ alpha_dash No Fails if field contains anything other than
alpha_numeric No Fails if field contains anything other than alpha-numeric characters or numbers.
alpha_numeric_space No Fails if field contains anything other than alpha-numeric characters, numbers or space.
decimal No Fails if field contains anything other than a decimal number.
differs Yes Fails if field does not differ from the one in the parameter. differs[field_name]
equals Yes Fails if field is not exactly the parameter value.
exact_length Yes Fails if field is not exactly the parameter value in length. One or more comma-separated values. exact_length[5] or exact_length[5,8,12]
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5]
in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green]
differs Yes Fails if field does not differ from the one in the parameter. differs[field_name]
exact_length Yes Fails if field is not exactly the parameter value. One or more comma-separated values. exact_length[5] or exact_length[5,8,12]
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5]
if_exist No If this rule is present, validation will only return possible errors if the field key exists,
regardless of its value.
in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green]
integer No Fails if field contains anything other than an integer.
is_natural No Fails if field contains anything other than a natural number: 0, 1, 2, 3, etc.
is_natural_no_zero No Fails if field contains anything other than a natural number, except zero: 1, 2, 3, etc.
less_than Yes Fails if field is greater than or equal to the parameter value or not numeric. less_than[8]
less_then_equal_to Yes Fails if field is greater than the parameter value or not numeric. less_than_equal_to[8]
matches Yes The value must match the value of the field in the parameter. matches[field]
max_length Yes Fails if field is longer than the parameter value. max_length[8]
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
not_equals Yes Fails if field is exactly the parameter value.
is_unique Yes Checks if this field value exists in the database. Optionally set a is_unique[table.field,ignore_field,ignore_value]
column and value to ignore, useful when updating records to ignore itself.
less_than Yes Fails if field is greater than or equal to the parameter value or not numeric. less_than[8]
less_then_equal_to Yes Fails if field is greater than the parameter value or not numeric. less_than_equal_to[8]
matches Yes The value must match the value of the field in the parameter. matches[field]
max_length Yes Fails if field is longer than the parameter value. max_length[8]
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
numeric No Fails if field contains anything other than numeric characters.
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
if_exist No If this rule is present, validation will only return possible errors if the field key exists,
regardless of its value.
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
permit_empty No Allows the field to receive an empty array, empty string, null or false.
required No Fails if the field is an empty array, empty string, null or false.
required_with Yes The field is required when any of the other required fields are present in the data. required_with[field1,field2]
required_without Yes The field is required when all of the other fields are present in the data but not required. required_without[field1,field2]
is_unique Yes Checks if this field value exists in the database. Optionally set a is_unique[table.field,ignore_field,ignore_value]
column and value to ignore, useful when updating records to ignore itself.
required_with Yes The field is required when any of the other required fields are present in the data. required_with[field1,field2]
required_without Yes The field is required when all of the other fields are present in the data but not required. required_without[field1,field2]
string No A generic alternative to the alpha* rules that confirms the element is a string
timezone No Fails if field does match a timezone per ``timezone_identifiers_list``
valid_base64 No Fails if field contains anything other than valid Base64 characters.
valid_json No Fails if field does not contain a valid JSON string.
Expand Down