This package is in pre-release. The API may change before v1.0.

Validation Rules

The toolkit ships with two validation rules designed for JSON:API query parameters. Both implement Laravel's ValidationRule interface and can be used in any form request.

ValidInclude

Validates that the include parameter only contains allowed relationship names. Accepts both comma-separated strings (category,tags) and arrays.

use BlueBeetle\ApiToolkit\Rules\ValidInclude;

new ValidInclude(['category', 'tags', 'brand'])

Parameters

ParameterTypeDescription
$validIncludesarrayRequired. List of allowed include names.

Behavior

  • Accepts comma-separated strings and splits them automatically
  • Trims whitespace from each value
  • Fails if any requested include is not in the allowed list

Error Message

The [invalid_relation] is not a valid include value.

Example

use BlueBeetle\ApiToolkit\Http\Requests\FormRequest;
use BlueBeetle\ApiToolkit\Rules\ValidInclude;

class ListProductsRequest extends FormRequest
{
    public function queryParamRules(): array
    {
        return [
            'include' => ['sometimes', new ValidInclude(['category', 'tags'])],
        ];
    }
}
GET /api/products?include=category        // passes
GET /api/products?include=category,tags   // passes
GET /api/products?include=secret          // fails

ValidPageSize

Validates that the page[size] parameter is one of the allowed sizes.

use BlueBeetle\ApiToolkit\Rules\ValidPageSize;

new ValidPageSize()               // uses sizes from config
new ValidPageSize([5, 10, 25])    // uses custom sizes

Parameters

ParameterTypeDefaultDescription
$validSizesarrayconfig('api-toolkit.pagination.valid_sizes')Allowed page sizes. Falls back to [10, 20, 40, 80, 100] if config is not set.

Behavior

  • Casts the value to integer before checking
  • When no sizes are passed to the constructor, reads from config('api-toolkit.pagination.valid_sizes')
  • Explicit constructor values always take precedence over config

Error Message

The [15] is not a valid page size value.

Example

use BlueBeetle\ApiToolkit\Http\Requests\FormRequest;
use BlueBeetle\ApiToolkit\Rules\ValidPageSize;

class ListProductsRequest extends FormRequest
{
    public function queryParamRules(): array
    {
        return [
            'page.size' => ['sometimes', new ValidPageSize()],
        ];
    }
}
GET /api/products?page[size]=20    // passes (default sizes)
GET /api/products?page[size]=15    // fails (not in allowed list)

Configuration

The default valid sizes are configured in config/api-toolkit.php:

'pagination' => [
    'valid_sizes' => [10, 20, 40, 80, 100],
],