add validation for request json
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
1f55f39a77
commit
a3afe63aa0
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
class Helpers
|
||||
{
|
||||
static public function validJson(string $json, array $keys): bool
|
||||
{
|
||||
if (Helpers::isJson($json)) {
|
||||
if (!Helpers::expectedArrayKeys(json_decode($json, true), $keys)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static public function isJson(string $json): bool
|
||||
{
|
||||
json_decode($json);
|
||||
return json_last_error() === JSON_ERROR_NONE;
|
||||
}
|
||||
|
||||
static public function expectedArrayKeys(array $json, array $keys): bool
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
if (!array_key_exists($key, $json)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue