Download the PHP package usox/json-schema-api without Composer
On this page you can find all versions of the php package usox/json-schema-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download usox/json-schema-api
More information about usox/json-schema-api
Files in usox/json-schema-api
Package json-schema-api
Short Description Json schema powered api creation for php
License MIT
Informations about the package json-schema-api
JsonSchemaApi
This library provides a simple way to create a json api using json-schema to validate the request. You can leverage most of the input validation tasks (variables types, length/content-constraints, lists containing just certain items, etc.) to the json schema validator and work with the data right away.
Json-Schema
Every method needs a corresponding schema-file which describes, how the request data should look alike.
You can find a simple example in the example/schema
-folder.
Every request has also to follow a basic schema (see dist/request.json
) which contains informations about the method.
Validation
Every request and the generated response will be validated against the provided schema.
Requirements
This library requires existing psr7 and psr17 implementations.
Install
Usage
Just use the factory method to create the endpoint. The factory automatically searches for a existing psr17 stream factory implementation, but you can also provide a factory instance when calling the method.
Get your method-provider ready (see below) and call the factory
-method on the endpoint to retrieve a working instance:
The serve method requires a psr request and returns a psr response.
Optional: PSR15 Middleware
The endpoint class implements the PSR15 RequestHandlerInterface
.
MethodProvider
The MethodProvider
is the source for your api methods - this could be a simple array, a DI-Container, etc. The class has to implement Usox\JsonSchemApi\Contract\MethodProviderInterface
.
API-Method
The lookup
-method in the MethodProvider must return an instance of Usox\JsonSchemaApi\Contract\ApiMethodInterface
.
Every api method handler must define at least those two methods:
- The
handle
method which processes the request and returns the result - The
getSchemaFile
method which returns the path to the schema file which is used to validate the request
Example
You can find a working example in the example
-folder.
Just cd to the example-folder and fire up the the php internal webserver php -S localhost:8888
.
Now you can send POST
-Requests to the api like this using curl.
Error-Handling
Basically there are three types of errors. All of them get logged.
ApiExceptions
If a handler throws an exception which extends the ApiException
exception class, the api will
return a Bad Request (400)
response including an error message (the exception message) and an error code for reference
within a json response.
InternalException
Internal errors, like non-existing schema files, invalid schemas and such, will return a Internal Server Error (500)
response and create a log entry (if a logger is provided).
In addition, optionally available context information within the exception will be logged, too.
Throwable
Any Throwable which are thrown within an api handler, will be catched, logged and return a Internal Server Error (500)
response.
All versions of json-schema-api with dependencies
ext-json Version *
opis/json-schema Version ^2.1
php-http/discovery Version ^1.13
psr/http-factory Version ^1.0
psr/http-message Version ^1||^2
psr/http-server-handler Version ^1.0
psr/log Version ^1.1||^3
ramsey/uuid Version ^4.1
teapot/status-code Version ^2