Download the PHP package phpdevcommunity/php-requestkit without Composer
On this page you can find all versions of the php package phpdevcommunity/php-requestkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpdevcommunity/php-requestkit
More information about phpdevcommunity/php-requestkit
Files in phpdevcommunity/php-requestkit
Package php-requestkit
Short Description A lightweight and efficient PHP library for handling, validating, and transforming incoming HTTP requests, supporting both form submissions and JSON API payloads.
License MIT
Informations about the package php-requestkit
php-requestkit
Lightweight and efficient PHP library for robust request data validation and transformation.
Simplify your request processing with php-requestkit
. This library allows you to define schemas for your incoming HTTP requests (both form submissions and JSON payloads) and effortlessly validate and transform the data. Ensure data integrity and streamline your application logic with schema-based validation, input sanitization, and flexible transformation methods.
Key Features
- Schema-based validation: Define clear and concise validation rules for your request data.
- Data transformation: Automatically transform and sanitize input data based on your schema.
- Multiple data types: Supports strings, integers, booleans, dates, date-times, and numeric types with various constraints.
- Nested data and collections: Validate complex data structures, including nested objects and arrays.
- Error handling: Provides detailed error messages for easy debugging and user feedback.
- Extensible: Easily extend schemas and create reusable validation logic.
Installation
Basic Usage
-
Create a Schema: Define your data structure and validation rules using
Schema::create()
andType
classes. - Process Request Data: Use the
process()
method of your schema to validate and transform incoming data.
Usage Examples
Validating REST API Request Body (JSON or Form Data)
This example demonstrates validating data from a REST API endpoint (e.g., POST, PUT, PATCH requests).
Validating URL Parameters (Query String)
Validate parameters passed in the URL's query string.
Validating Collections of Data (Arrays)
Validate arrays of data, especially useful for batch operations or list endpoints.
Error Handling with InvalidDataException
When validation fails, the Schema::process()
method throws an InvalidDataException
. This exception provides methods to access detailed error information.
Retrieving All Errors
Use getErrors()
to get an associative array where keys are field paths and values are error messages.
Retrieving a Specific Error
Use getError(string $key)
to get the error message for a specific field path. Returns null
if no error exists for that path.
Formatting Error Response with toResponse()
Use toResponse()
to get a pre-formatted associative array suitable for returning as an API error response. This includes status, a general error message, and detailed validation errors.
Accessing Exception message
and code
InvalidDataException
extends PHP's base \Exception
, allowing access to standard exception properties.
Available Validation Types and Rules
php-requestkit
provides a variety of built-in data types with a rich set of validation rules.
-
Type::string()
:required()
: Field is mandatory.optional()
: Field is optional.length(min, max)
: String length constraints.trim()
: Trim whitespace from both ends.lowercase()
: Convert to lowercase.uppercase()
: Convert to uppercase.email()
: Validate email format.allowed(...values)
: Value must be one of the allowed values.removeSpaces()
: Remove all spaces.padLeft(length, char)
: Pad string to the left with a character.removeChars(...chars)
: Remove specific characters.strict()
: Strict type validation (only accepts strings).
-
Type::int()
:required()
,optional()
,strict()
: Same as StringType.min(value)
: Minimum value.max(value)
: Maximum value.
-
Type::bool()
:required()
,optional()
,strict()
: Same as StringType.
-
Type::date()
andType::datetime()
:required()
,optional()
: Same as StringType.format(format)
: Specify the date/datetime format (using PHP date formats).
-
Type::numeric()
:required()
,optional()
: Same as StringType. Validates any numeric value (integer or float).
-
Type::item(array $schema)
: For nested objects/items. Defines a schema for a nested object within the main schema. Type::arrayOf(Type $type)
: For collections/arrays. Defines that a field should be an array of items, each validated against the providedType
.
Extending Schemas
You can extend existing schemas to reuse and build upon validation logic.
All versions of php-requestkit with dependencies
ext-json Version *
phpdevcommunity/php-validator Version ^1.0
psr/http-message Version ^1.0 || ^2.0