Skip to content

Commit

Permalink
Schema bugfixes (#356)
Browse files Browse the repository at this point in the history
* Bugfix: Markdown type uses json_path type

* Improvement: Allow numeric values to be stringified in validation errors

* Bugfix: Require button_text and id to be non-empty

* Improve tests
  • Loading branch information
chriszarate authored Feb 12, 2025
1 parent 243d6ea commit b4732ef
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 103 deletions.
2 changes: 1 addition & 1 deletion inc/Validation/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function json_path(): array {
}

public static function markdown(): array {
return self::generate_primitive_type( 'json_path' );
return self::generate_primitive_type( 'markdown' );
}

public static function url(): array {
Expand Down
8 changes: 5 additions & 3 deletions inc/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ private function check_primitive_type( string $type_name, mixed $value ): bool {
case 'email_address':
return false !== is_email( $value );

case 'button_text':
case 'html':
case 'id':
case 'image_alt':
case 'markdown':
return is_string( $value );

case 'button_text':
case 'id':
return is_string( $value ) && ! empty( $value );

case 'json_path':
return is_string( $value ) && str_starts_with( $value, '$' );

Expand All @@ -229,7 +231,7 @@ public static function check_iterable_object( mixed $value ): bool {
}

private function create_error( string $message, mixed $value, ?WP_Error $child_error = null ): WP_Error {
$serialized_value = is_string( $value ) ? $value : wp_json_encode( $value );
$serialized_value = is_string( $value ) || is_numeric( $value ) ? strval( $value ) : wp_json_encode( $value );
$message = sprintf( '%s: %s', esc_html( $message ), $serialized_value );
return new WP_Error( 'invalid_type', $message, [ 'child' => $child_error ] );
}
Expand Down
Loading

0 comments on commit b4732ef

Please sign in to comment.