polus / adr-json-schema-input
Helper to create input validation based on json schema
1.0.0
2019-01-28 19:09 UTC
Requires
- php: >=7.2.0
- ext-json: *
- opis/json-schema: ^1.0
- psr/http-message: ^1.0
- psr/log: ^1.0
This package is auto-updated.
Last update: 2024-10-29 05:28:56 UTC
README
Helper class to make it easier to use JsonSchema's to validate request input
Install
composer require polus/adr-json-schema-input
Usage
SchemaMap
To use the abstract input class you need to implement a SchemaMap
That will
contains information about the schemas and a way to load them.
use Polus\JsonSchemaInput\SchemaMapInterface; class SchemaMap implements SchemaMapInterface { public const SCHEMA_NS = 'https://schema_ns/schemas/'; public const TEST_SCHEMA = 'subDirectory/test.json'; private const ALLOWED_SCHEMAS = [ self::TEST_SCHEMA, ]; public function getDefaultNamespace(): string { return self::SCHEMA_NS; } /** * @return string[] */ public function getSchemaPaths(): array { return [ 'path to where schemas are stored', ]; } public function exists(string $schema): bool { return in_array($schema, self::ALLOWED_SCHEMAS, true); } public function get(string $schema): string { if ($this->exists($schema)) { return self::SCHEMA_NS . $schema; } return ''; } public function load(string $schema): ?array { foreach ($this->getSchemaPaths() as $path) { if (file_exists($path.'/'.$schema)) { return json_decode($path.'/'.$schema, true); } } return null } }
Input
use Psr\Http\Message\ServerRequestInterface; use Polus\JsonSchemaInput\Input\AbstractJsonSchemaInput; class TestInput extends AbstractJsonSchemaInput { public function __invoke(ServerRequestInterface $request) { $body = $this->validate($request, SchemaMap::TEST_SCHEMA); return $body; } }
If the validate fails it will throw DomainException with the JsonSchema error message