| 1 |
<?php |
| 2 |
/** |
| 3 |
* Validator - Centralized schema-based validation and sanitation |
| 4 |
* |
| 5 |
* @package zip-ai |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ZipAI\MCP\Classes\Core; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Validator |
| 17 |
*/ |
| 18 |
class Validator { |
| 19 |
|
| 20 |
/** |
| 21 |
* Validate and sanitize input against a JSON schema. |
| 22 |
* |
| 23 |
* @param array $schema The JSON schema to validate against. |
| 24 |
* @param array $input The raw input data. |
| 25 |
* @return array|\WP_Error Validated and sanitized data on success, WP_Error on failure. |
| 26 |
*/ |
| 27 |
public static function validate( $schema, $input ) { |
| 28 |
$valid_data = array(); |
| 29 |
$properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
| 30 |
$required = isset( $schema['required'] ) ? $schema['required'] : array(); |
| 31 |
|
| 32 |
// Check for missing required fields. |
| 33 |
foreach ( $required as $field ) { |
| 34 |
if ( ! isset( $input[ $field ] ) ) { |
| 35 |
return new \WP_Error( |
| 36 |
'missing_required_field', |
| 37 |
sprintf( 'Missing required field: %s', $field ) |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Validate and sanitize each property. |
| 43 |
foreach ( $properties as $field => $config ) { |
| 44 |
$value = isset( $input[ $field ] ) ? $input[ $field ] : ( isset( $config['default'] ) ? $config['default'] : null ); |
| 45 |
|
| 46 |
if ( null === $value ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$type = isset( $config['type'] ) ? $config['type'] : 'string'; |
| 51 |
|
| 52 |
switch ( $type ) { |
| 53 |
case 'integer': |
| 54 |
$value = (int) $value; |
| 55 |
if ( isset( $config['minimum'] ) && $value < $config['minimum'] ) { |
| 56 |
return new \WP_Error( 'invalid_range', sprintf( '%s is below minimum %d', $field, $config['minimum'] ) ); |
| 57 |
} |
| 58 |
if ( isset( $config['maximum'] ) && $value > $config['maximum'] ) { |
| 59 |
return new \WP_Error( 'invalid_range', sprintf( '%s is above maximum %d', $field, $config['maximum'] ) ); |
| 60 |
} |
| 61 |
break; |
| 62 |
|
| 63 |
case 'boolean': |
| 64 |
$value = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 65 |
break; |
| 66 |
|
| 67 |
case 'string': |
| 68 |
if ( isset( $config['enum'] ) && ! in_array( $value, $config['enum'], true ) ) { |
| 69 |
return new \WP_Error( 'invalid_enum', sprintf( 'Invalid value for %s. Expected one of: %s', $field, implode( ', ', $config['enum'] ) ) ); |
| 70 |
} |
| 71 |
// Default string sanitation if not explicitly handled by the tool. |
| 72 |
// Note: Tools can still do their own more specific sanitation. |
| 73 |
break; |
| 74 |
|
| 75 |
case 'array': |
| 76 |
if ( ! is_array( $value ) ) { |
| 77 |
return new \WP_Error( 'invalid_type', sprintf( '%s must be an array', $field ) ); |
| 78 |
} |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
$valid_data[ $field ] = $value; |
| 83 |
} |
| 84 |
|
| 85 |
return $valid_data; |
| 86 |
} |
| 87 |
} |
| 88 |
|