Array_Type is used to define array schema arguments. It supports typed items, length constraints, uniqueness, and element relationships (allOf, anyOf, oneOf).
Namespace: PinkCrab\WP_Rest_Schema\Argument\Array_Type
use PinkCrab\WP_Rest_Schema\Argument\Array_Type;
$arg = Array_Type::field( 'tags' );
// With configuration callback
$arg = Array_Type::field( 'tags', function( Array_Type $a ): Array_Type {
return $a->string_item()->unique_items();
} );The type is automatically set to 'array'.
Define the type of items the array contains. Each method creates a typed child argument. An optional callback lets you configure the item further.
$tags = Array_Type::field( 'tags' )->string_item();
// With configuration
$tags = Array_Type::field( 'tags' )
->string_item( fn( String_Type $s ) => $s->min_length( 1 ) );$ids = Array_Type::field( 'ids' )->integer_item();$scores = Array_Type::field( 'scores' )->number_item();$flags = Array_Type::field( 'flags' )->boolean_item();$nullable = Array_Type::field( 'values' )->null_item();Nested array items.
$matrix = Array_Type::field( 'matrix' )
->array_item( fn( Array_Type $a ) => $a->integer_item() );Object items.
$users = Array_Type::field( 'users' )
->object_item( fn( Object_Type $o ) => $o
->string_property( 'name' )
->string_property( 'email' )
);Add a pre-built item directly.
$custom = String_Type::field( 'value' )->min_length( 1 );
$list = Array_Type::field( 'items' )->item( $custom );WP treats items as a SINGLE schema applied to every element; JSON Schema tuple form is not honoured. The library therefore handles repeated *_item() calls as follows:
- No combinator set (default
allOf): the LAST-added item wins anditemsis emitted as a single schema. Prior item calls are discarded. - Combinator set (
one_of()/any_of()): all item schemas are kept and emitted as a list under the combinator key, producing valid WP output such as'items' => [ 'oneOf' => [ {schemaA}, {schemaB} ] ].
For heterogeneous arrays at the SCHEMA ROOT (not nested inside items), use One_Of_Type / Any_Of_Type instead.
Set the minimum number of items. Maps to minItems in the schema.
$tags = Array_Type::field( 'tags' )->min_items( 1 );Set the maximum number of items. Maps to maxItems in the schema.
$tags = Array_Type::field( 'tags' )->max_items( 10 );Require all items to be unique. Maps to uniqueItems in the schema.
$tags = Array_Type::field( 'tags' )->unique_items();Control how multiple items relate to each other when more than one item type is defined. The default relationship is allOf.
All item schemas must match (default).
$list = Array_Type::field( 'items' )
->string_item()
->integer_item()
->all_of();At least one item schema must match.
$list = Array_Type::field( 'items' )
->string_item()
->integer_item()
->any_of();Exactly one item schema must match.
$list = Array_Type::field( 'items' )
->string_item()
->integer_item()
->one_of();| Setter | Getter | Returns |
|---|---|---|
*_item() |
get_items() |
?array<int, Argument> |
| — | has_items() |
bool |
| — | item_count() |
int |
min_items() |
get_min_items() |
?int |
max_items() |
get_max_items() |
?int |
unique_items() |
get_unique_items() |
?bool |
all_of() / any_of() / one_of() |
get_relationship() |
string |
$tags = Array_Type::field( 'tags' )
->string_item()
->unique_items()
->description( 'A list of unique tag names.' );$ids = Array_Type::field( 'post_ids' )
->integer_item( fn( Integer_Type $i ) => $i->minimum( 1 ) )
->min_items( 1 )
->max_items( 50 )
->unique_items()
->required()
->description( 'A list of 1-50 unique post IDs.' );use PinkCrab\WP_Rest_Schema\Argument\Object_Type;
use PinkCrab\WP_Rest_Schema\Argument\String_Type;
$addresses = Array_Type::field( 'addresses' )
->object_item( function( Object_Type $o ) {
return $o
->string_property( 'street', fn( String_Type $s ) => $s->required() )
->string_property( 'city', fn( String_Type $s ) => $s->required() )
->string_property( 'postcode' );
} )
->max_items( 5 )
->description( 'Up to 5 addresses.' );$matrix = Array_Type::field( 'grid' )
->array_item( fn( Array_Type $a ) => $a->number_item() )
->description( 'A 2D grid of numbers.' );use PinkCrab\WP_Rest_Schema\Parser\Argument_Parser;
register_post_meta( 'post', 'related_ids', array(
'type' => 'array',
'single' => true,
'show_in_rest' => array(
'schema' => Argument_Parser::for_meta_data(
Array_Type::field( 'related_ids' )
->integer_item( fn( Integer_Type $i ) => $i->minimum( 1 ) )
->unique_items()
->description( 'Related post IDs.' )
),
),
) );