Last active
January 10, 2019 01:22
-
-
Save markusand/c89eb63e4f25fa3325450a19fa07cd0e to your computer and use it in GitHub Desktop.
Function to validate a data array using a schema
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
<?php | |
/* Validation schema example */ | |
$schema = [ | |
'id' => ['field' => 'id'], | |
'name' => [ | |
'field' => 'name', | |
'required' => true, | |
'validation' => ['type' => 'string'] | |
], | |
'username' => [ | |
'field' => 'username', | |
'required' => true, | |
'validation' => [ | |
'type' => 'string', | |
'length' => [6, 12] | |
] | |
], | |
'age' => [ | |
'field' => 'age', | |
'validation' => [ | |
'filter' => FILTER_VALIDATE_INT, | |
'options' => ['min_range' => 18, 'max_range' => 99] | |
] | |
], | |
'phone' => [ | |
'field' => 'phone', | |
'validation' => ['regex' => '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/'] | |
], | |
'email' => [ | |
'field' => 'email', | |
'required' => true, | |
'validation' => ['filter' => FILTER_VALIDATE_EMAIL] | |
] | |
]; | |
?> |
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
<?php | |
function validateParameters($parameters, $schema) { | |
$all_errors = []; | |
$validated = array_map(function($parameter, $options) use (&$parameters, &$all_errors) { | |
$errors = []; | |
$value = $parameters[$parameter]; | |
if ($value === null) { | |
if ($options['required']) $all_errors[] = $parameter.' is required'; | |
return null; | |
} | |
unset($parameters[$parameter]); | |
if ($options['validation']) { | |
$valid = $options['validation']; | |
if ($valid['filter'] && !filter_var($value, $valid['filter'], ['options' => $valid['options']])) $errors[] = $parameter.' value is invalid'; | |
if ($valid['type'] && gettype($value) !== $valid['type']) $errors[] = $parameter.' value is invalid'; | |
if ($valid['regex'] && !preg_match($valid['regex'], $value)) $errors[] = $parameter.' value is invalid'; | |
if ($valid['length'] && !inBetween(strlen($value), $valid['length'])) $errors[] = $parameter.' is too short/long'; | |
} | |
$all_errors = array_merge($all_errors, $errors); | |
return count($errors) ? null : $options['field']. ' = :'.$parameter; | |
}, array_keys($schema), $schema); | |
if (count($parameters)) $all_errors[] = 'invalid parameter(s) '.implode(', ', array_keys($parameters)); | |
return [ | |
'parameters' => array_values(array_filter($validated)), | |
'errors' => $all_errors | |
]; | |
} | |
function inBetween($val, $range) { | |
return is_array($range) ? $val >= $range[0] && $val <= $range[1] : $val === $range; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment