Last active
April 29, 2021 06:43
-
-
Save danrspencer/9753d0d25e07da01b47cb50f60a76958 to your computer and use it in GitHub Desktop.
Validate a PHP array against a schema to ensure all desired keys are present (supports nested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateAgainstSchema(array $schema, $data) { | |
$valid = true; | |
$done = false; | |
$path = ""; | |
$current = each($schema); | |
while($valid !== false && $current !== false) { | |
list($key, $value) = $current; | |
$index = is_string($key) ? $key : $value; | |
$valid = isset($data[$index]); | |
$path = $index; | |
if ($valid && is_array($value)) { | |
list($valid, $index) = validateAgainstSchema($value, $data[$key]); | |
$path .= ".$index"; | |
} | |
if ($valid) $current = each($schema); | |
} | |
return [ $valid, $valid ? "" : $path ]; | |
} |
And writing a class for every small data structure is awesome if you're paid hourly.
Nice gist but I'll just leave here an array to add some complexity to the matter:
[
"list_number" => 1,
"items" => [[ "value" => 1552.22 ], [ "value" => 155.22 ]],
"total" => 33
]
The items
should be more JSON, like {[],[],...,[]}
but I think my point is clear.
Thanks for reading ^~^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
someone once invented classes ...