Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bool endswith(mixed $input, string $match)
bool contains(mixed $input, string $match)
bool regex(mixed $input, string $regex)
bool inlist(mixed $input, array $list)
bool json(mixed $input)
```

### Validation Engine
Expand Down
11 changes: 11 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,15 @@ public static function inlist($input = null, $list = [])

return in_array($input, $list);
}

public static function json($input = null)
{
if (empty($input)) {
return true;
}

$attempted_parsed_json = json_decode($input, true);

return ( $attempted_parsed_json !== null );
}
}
20 changes: 20 additions & 0 deletions tests/ValidatorTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,24 @@ public function testShouldPassInlist()
$this->assertTrue(Validator::inlist(null, ['ABC','DEF']));
$this->assertTrue(Validator::inlist('ABC', ['ABC','DEF']));
}

public function testShouldFailJson()
{
$this->assertFalse(Validator::json('asd;flkj'));
}

public function testShouldPassJson()
{
$this->assertTrue(Validator::json('{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
}}}'
));

$this->assertTrue(Validator::json('{}'));
}
}