From 804106090cc87cb5ab15c9c9474837ef60cc46a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowacki?= Date: Thu, 30 Aug 2018 13:09:38 +0200 Subject: [PATCH 1/3] Adds valid_json which is using json_last_error() === JSON_ERROR_NONE it will return false if json is null, similar to existing funcs , i.e. valid_url --- system/Validation/FormatRules.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index c662768392be..971366c969c1 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -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 * From 9ef2d95d8af237ac1d0fff596fac16f97189ec4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowacki?= Date: Fri, 31 Aug 2018 13:54:32 +0200 Subject: [PATCH 2/3] doscs - valid_json --- user_guide_src/source/libraries/validation.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index d9540ce9d5bb..495747bbb3e3 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -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] From 7378a213c28318d78984fcdf0765c9f7ef916f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowacki?= Date: Fri, 31 Aug 2018 13:56:38 +0200 Subject: [PATCH 3/3] valid_json tests --- tests/system/Validation/FormatRulesTest.php | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/system/Validation/FormatRulesTest.php b/tests/system/Validation/FormatRulesTest.php index 8ca93ad874f9..82ebf86b3955 100644 --- a/tests/system/Validation/FormatRulesTest.php +++ b/tests/system/Validation/FormatRulesTest.php @@ -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 *