wunderwerkio / http-api-utils
Set of useful HTTP API utilities. Provides json schema validation and error handling.
0.1.1
2023-09-29 09:16 UTC
Requires
- justinrainbow/json-schema: ^5.2
- symfony/http-foundation: ^4 || ^5 || ^6
- wunderwerkio/jsonapi-error: ^0.1.2
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
README
This package provides useful utilities for use with HTTP-APIs of any kind.
For response generation the symfony/http-foundation
package is being used.
Install
Install this package via composer:
composer require wunderwerkio/http-api-utils
Features
HttpApiValidationTrait
The HttpApiValidationTrait
provides methods to validate a data structure against a [JSON Schema)(https://json-schema.org/).
The validation itself is done with the justinrainbow/json-schema
package.
Define the schema:
$schema = [ "type" => "object", "properties" => [ "name" => [ "type" => "string", ], "age" => [ "type" => "integer", ], ], "required" => ["name"], ];
Depending on your input data, you can use different validation methods to suit your needs:
validateDataStructure
Accepts a reference to an object. This is meant to be used with results fromjson_decode
.validateArray
Accepts an array as the data to validate.validateJsonString
Accepts a JSON encoded string.
Example (with the schema from above):
$validData = (object) [ 'name' => 'Max', 'age' => 42, ]; $result = $this->validateDataStructure($validData, $schema); $result->isValid(); // -> TRUE // With invalid data. $invalidData = (object) [ 'age' => '42', ]; $result = $this->validateDataStructure($invalidData, $schema); $result->isValid(); // -> FALSE $result->getErrors(); // -> Array of validation errors. $result->getResponse(); // -> JsonApiErrorResponse with the validation errors.
For more information, check out the JSON Schema package: https://github.com/justinrainbow/json-schema.