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

Adds valid_json which is using json_last_error() === JSON_ERROR_NONE #1187

Merged
merged 3 commits into from
Sep 4, 2018
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
15 changes: 15 additions & 0 deletions system/Validation/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ public function valid_base64(string $str = null): bool

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

/**
* Valid JSON
*
* @param string
*
* @return bool
*/
public function valid_json(string $str = null): bool
{
json_decode($str);
return json_last_error() === JSON_ERROR_NONE;
}

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

/**
* Checks for a correctly formatted email address
*
Expand Down
38 changes: 38 additions & 0 deletions tests/system/Validation/FormatRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,44 @@ public function base64Provider()

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

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

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

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

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

public function jsonProvider()
{
return [
['null', true],
['"null"', true],
['600100825', true],
['{"A":"Yay.", "B":[0,5]}', true],
['[0,"2",2.2,"3.3"]', true],
[null, false],
['600-Nope. Should not pass.', false],
['{"A":SHOULD_NOT_PASS}', false],
['[0,"2",2.2 "3.3"]', false]
];
}

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

/**
* @dataProvider timezoneProvider
*
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ is_unique Yes Checks if this field value exists in the dat
column and value to ignore, useful when updating records to ignore itself.
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.
valid_email No Fails if field does not contain a valid email address.
valid_emails No Fails if any value provided in a comma separated list is not a valid email.
valid_ip No Fails if the supplied IP is not valid. Accepts an optional parameter of ‘ipv4’ or valid_ip[ipv6]
Expand Down