1. <?php
  2. /**
  3. * Extends API payload validation with memory efficiency.
  4. *
  5. * This function iterates through the payload data, performing validation
  6. * checks on individual elements without loading the entire payload into memory.
  7. * Suitable for large JSON responses.
  8. *
  9. * @param string $json_string The JSON string to validate.
  10. * @param array $validation_rules An array of validation rules. Each rule should be an associative array with keys: 'field', 'type', 'validate_function', 'message'.
  11. * @return array|false An array of validation errors if any, or false on failure to decode JSON.
  12. */
  13. function validateApiPayload(string $json_string, array $validation_rules): array|false
  14. {
  15. $errors = [];
  16. $data = json_decode($json_string, true);
  17. if ($data === null) {
  18. return false; // JSON decode failed
  19. }
  20. foreach ($validation_rules as $rule) {
  21. $field = $rule['field'];
  22. $type = $rule['type'];
  23. $validate_function = $rule['validate_function'];
  24. $message = $rule['message'];
  25. // Check if the field exists in the data
  26. if (!isset($data[$field])) {
  27. $errors[] = ['field' => $field, 'message' => $message];
  28. continue;
  29. }
  30. // Validate the field based on the specified type
  31. $validation_result = call_user_func($validate_function, $data[$field]);
  32. if ($validation_result === false) {
  33. $errors[] = ['field' => $field, 'message' => $validation_result];
  34. }
  35. }
  36. return $errors;
  37. }
  38. /**
  39. * Example validation functions. Can be extended as needed.
  40. *
  41. * @param mixed $value The value to validate.
  42. * @return bool|string Returns true on success, or an error message on failure.
  43. */
  44. function validateString(string $value)
  45. {
  46. return !empty($value);
  47. }
  48. function validateInt(int $value)
  49. {
  50. return is_int($value);
  51. }
  52. function validateEmail(string $value)
  53. {
  54. return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
  55. }
  56. function validateArray(array $value) {
  57. return is_array($value);
  58. }
  59. // Example Usage:
  60. /*
  61. $json = '[
  62. {"name": "John Doe", "email": "john.doe@example.com", "age": 30},
  63. {"name": "Jane Smith", "email": "invalid-email", "age": "abc"}
  64. ]';
  65. $rules = [
  66. ['field' => 'name', 'type' => 'validateString', 'validate_function' => 'validateString', 'message' => 'Name cannot be empty.'],
  67. ['field' => 'email', 'type' => 'validateEmail', 'validate_function' => 'validateEmail', 'message' => 'Invalid email address.'],
  68. ['field' => 'age', 'type' => 'validateInt', 'validate_function' => 'validateInt', 'message' => 'Age must be an integer.'],
  69. ['field' => 'age', 'type' => 'validateInt', 'validate_function' => 'validateInt', 'message' => 'Age must be an integer.'],
  70. ['field' => 'address', 'type' => 'validateArray', 'validate_function' => 'validateArray', 'message' => 'Address must be an array.'],
  71. ];
  72. $errors = validateApiPayload($json, $rules);
  73. if (empty($errors)) {
  74. echo "Payload is valid.\n";
  75. } else {
  76. echo "Validation errors:\n";
  77. foreach ($errors as $error) {
  78. echo " Field: " . $error['field'] . ", Message: " . $error['message'] . "\n";
  79. }
  80. }
  81. */
  82. ?>

Add your comment