Skip to content

Commit b4732ef

Browse files
authored
Schema bugfixes (#356)
* 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
1 parent 243d6ea commit b4732ef

File tree

3 files changed

+279
-103
lines changed

3 files changed

+279
-103
lines changed

inc/Validation/Types.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function json_path(): array {
9797
}
9898

9999
public static function markdown(): array {
100-
return self::generate_primitive_type( 'json_path' );
100+
return self::generate_primitive_type( 'markdown' );
101101
}
102102

103103
public static function url(): array {

inc/Validation/Validator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,15 @@ private function check_primitive_type( string $type_name, mixed $value ): bool {
196196
case 'email_address':
197197
return false !== is_email( $value );
198198

199-
case 'button_text':
200199
case 'html':
201-
case 'id':
202200
case 'image_alt':
203201
case 'markdown':
204202
return is_string( $value );
205203

204+
case 'button_text':
205+
case 'id':
206+
return is_string( $value ) && ! empty( $value );
207+
206208
case 'json_path':
207209
return is_string( $value ) && str_starts_with( $value, '$' );
208210

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

231233
private function create_error( string $message, mixed $value, ?WP_Error $child_error = null ): WP_Error {
232-
$serialized_value = is_string( $value ) ? $value : wp_json_encode( $value );
234+
$serialized_value = is_string( $value ) || is_numeric( $value ) ? strval( $value ) : wp_json_encode( $value );
233235
$message = sprintf( '%s: %s', esc_html( $message ), $serialized_value );
234236
return new WP_Error( 'invalid_type', $message, [ 'child' => $child_error ] );
235237
}

0 commit comments

Comments
 (0)