diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f46e02..5b872ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # CHANGELOG +## [1.0.0] +provides for a given value a json schema instance of +* array +* boolen +* number +* object +* string + ## [0.0.2] * added flux-publish-utils diff --git a/README.md b/README.md index 20561ac..471d4e9 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,66 @@ https://json-schema.org/specification.html The following example application demonstrates the usage: https://github.com/flux-caps/todo-app +## Usage + +account.yaml +``` +title: account +type: object +aggregateRootNames: + - account +properties: + personId: + type: number + firstname: + type: string + lastname: + type: string + email: + type: string + type: + type: string + lastChanged: + type: string +``` + +getAndPrintSchemaInstance.php + +``` +$schema = yaml_parse(file_get_contents('account.yaml')); + +$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('Emmett', $schema['properties']['firstname']); +print_r($schemaInstance); + +$schemaInstance = fluxJsonSchemaInstance\getSchemaInstance('123', $schema['properties']['personId']); +print_r($schemaInstance); +``` + +outputs +``` +Array +( + [value] => Emmett + [describedBy] => {"type":"string"} +) +Array +( + [value] => 123 + [describedBy] => {"type":"number"} +) +``` + ## Contributing :purple_heart: + Please ... + 1. ... register an account at https://git.fluxlabs.ch 2. ... create pull requests :fire: ## Adjustment suggestions / bug reporting :feet: + Please ... + 1. ... register an account at https://git.fluxlabs.ch 2. ... ask us for a Service Level Agreement: support@fluxlabs.ch :kissing_heart: 3. ... read and create issues diff --git a/composer.json b/composer.json index 7d70f13..d89f81e 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "flux-eco/json-schema-instance", "description": "Makes a json schema instance for the transmitted values and schema", - "version": "0.0.2", + "version": "1.0.0", "type": "flux-app", "keywords": [ "flux-eco", @@ -23,25 +23,19 @@ }, "require": { "php": ">=8.0", - "ext-curl": "*", "ext-json": "*", - "ext-yaml": "*", - "composer-runtime-api": ">=2.1", - "psr/http-factory": ">=1.0", - "psr/http-message": ">=1.0", - "psr/http-server-handler": ">=1.0", - "psr/http-server-middleware": ">=1.0" - }, - "require-dev": { - "phpunit/phpunit": ">=9.5", - "composer/composer": ">=2.0", - "swoole/ide-helper": ">=4.6", - "symfony/console": ">=5.2" + "ext-yaml": "*" }, "autoload": { + "files": [ + "fn/getSchemaInstance.php" + ], "psr-4": { "FluxEco\\JsonSchemaInstance\\": [ "src/" + ], + "fluxJsonSchemaInstance\\": [ + "fn/" ] } }, diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..2a32dfc --- /dev/null +++ b/composer.lock @@ -0,0 +1,22 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "ed15a6c9b490c9007025e8b075680b72", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=8.0", + "ext-json": "*", + "ext-yaml": "*" + }, + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/examples/account.yaml b/examples/account.yaml new file mode 100644 index 0000000..21909b4 --- /dev/null +++ b/examples/account.yaml @@ -0,0 +1,19 @@ +--- +title: account +type: object +aggregateRootNames: + - account +properties: + personId: + type: number + firstname: + type: string + lastname: + type: string + email: + type: string + type: + type: string + lastChanged: + type: string +... \ No newline at end of file diff --git a/examples/getAndPrintSchemaInstance.php b/examples/getAndPrintSchemaInstance.php new file mode 100644 index 0000000..00652d2 --- /dev/null +++ b/examples/getAndPrintSchemaInstance.php @@ -0,0 +1,12 @@ +getSchemaInstance($value, $jsonSchema); +} \ No newline at end of file diff --git a/src/Adapters/Api/JsonSchemaInstanceApi.php b/src/Adapters/Api/JsonSchemaInstanceApi.php deleted file mode 100644 index b16b07e..0000000 --- a/src/Adapters/Api/JsonSchemaInstanceApi.php +++ /dev/null @@ -1,28 +0,0 @@ -service = $service; - } - - public static function new(): self - { - $service = Ports\SchemaInstanceService::new(); - return new self($service); - } - - final public function provideSchemaInstance(string $value, array $jsonSchema): array - { - $schemaInstance = $this->service->provideSchemaInstance($value, $jsonSchema); - return SchemaInstance::fromDomain($schemaInstance)->toArray(); - } -} \ No newline at end of file diff --git a/src/Adapters/Api/SchemaInstance.php b/src/Adapters/Api/SchemaInstance.php deleted file mode 100644 index 8f8991a..0000000 --- a/src/Adapters/Api/SchemaInstance.php +++ /dev/null @@ -1,25 +0,0 @@ -schemaInstance = $schemaInstance; - } - - public static function fromDomain(Domain\Models\SchemaInstance $schemaInstance): self - { - return new self($schemaInstance); - } - - public function toArray(): array { - return $this->schemaInstance->toArray(); - } -} \ No newline at end of file diff --git a/src/Api.php b/src/Api.php new file mode 100644 index 0000000..288ee0a --- /dev/null +++ b/src/Api.php @@ -0,0 +1,26 @@ +service = $service; + } + + public static function new() : self + { + $service = Core\Ports\SchemaInstanceService::new(); + return new self($service); + } + + final public function getSchemaInstance(string $value, array $jsonSchema) : array + { + $schemaInstance = $this->service->getSchemaInstance($value, $jsonSchema); + return SchemaInstance::fromDomain($schemaInstance)->toArray(); + } +} \ No newline at end of file diff --git a/src/Core/Application/Commands/ProvideArraySchemaInstanceHandler.php b/src/Core/Application/Commands/ProvideArraySchemaInstanceHandler.php index 370124f..66281d7 100644 --- a/src/Core/Application/Commands/ProvideArraySchemaInstanceHandler.php +++ b/src/Core/Application/Commands/ProvideArraySchemaInstanceHandler.php @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str //ToDo special string validation //@see https://json-schema.org/draft/2020-12/json-schema-validation.html - return Domain\Models\StringSchemaInstance::new($value); + return Domain\Models\StringSchemaInstance::new($value, json_encode($schema)); } } diff --git a/src/Core/Application/Commands/ProvideBooleanSchemaInstanceHandler.php b/src/Core/Application/Commands/ProvideBooleanSchemaInstanceHandler.php index 3c29266..8e74cb3 100644 --- a/src/Core/Application/Commands/ProvideBooleanSchemaInstanceHandler.php +++ b/src/Core/Application/Commands/ProvideBooleanSchemaInstanceHandler.php @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str //ToDo special string validation //@see https://json-schema.org/draft/2020-12/json-schema-validation.html - return Domain\Models\StringSchemaInstance::new($value); + return Domain\Models\StringSchemaInstance::new($value, json_encode($schema)); } } diff --git a/src/Core/Application/Commands/ProvideNumberSchemaInstanceHandler.php b/src/Core/Application/Commands/ProvideNumberSchemaInstanceHandler.php index aa688e2..7daaecc 100644 --- a/src/Core/Application/Commands/ProvideNumberSchemaInstanceHandler.php +++ b/src/Core/Application/Commands/ProvideNumberSchemaInstanceHandler.php @@ -18,12 +18,10 @@ public static function new(): self public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\NumberSchemaInstance { + $schema = $command->getSchema(); $value = $command->getValue(); - //ToDo special string validation - //@see https://json-schema.org/draft/2020-12/json-schema-validation.html - - return Domain\Models\NumberSchemaInstance::new($value); + return Domain\Models\NumberSchemaInstance::new($value, json_encode($schema)); } } diff --git a/src/Core/Application/Commands/ProvideObjectSchemaInstanceHandler.php b/src/Core/Application/Commands/ProvideObjectSchemaInstanceHandler.php index da960b9..9ab9c70 100644 --- a/src/Core/Application/Commands/ProvideObjectSchemaInstanceHandler.php +++ b/src/Core/Application/Commands/ProvideObjectSchemaInstanceHandler.php @@ -61,7 +61,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Obj } - return Domain\Models\ObjectSchemaInstance::new($properties); + return Domain\Models\ObjectSchemaInstance::new($properties, json_encode($subSchema)); } private function process(ProvideSchemaInstanceCommand $command, ProvideSchemaInterfaceHandler $handler): Domain\Models\SchemaInstance diff --git a/src/Core/Application/Commands/ProvideStringSchemaInstanceHandler.php b/src/Core/Application/Commands/ProvideStringSchemaInstanceHandler.php index cf4b2a0..60a7d26 100644 --- a/src/Core/Application/Commands/ProvideStringSchemaInstanceHandler.php +++ b/src/Core/Application/Commands/ProvideStringSchemaInstanceHandler.php @@ -24,7 +24,7 @@ public function handle(ProvideSchemaInstanceCommand $command): Domain\Models\Str //ToDo special string validation //@see https://json-schema.org/draft/2020-12/json-schema-validation.html - return Domain\Models\StringSchemaInstance::new($value); + return Domain\Models\StringSchemaInstance::new($value, json_encode($schema)); } } diff --git a/src/Core/Ports/SchemaInstanceService.php b/src/Core/Ports/SchemaInstanceService.php index db49cfe..adf3b1d 100644 --- a/src/Core/Ports/SchemaInstanceService.php +++ b/src/Core/Ports/SchemaInstanceService.php @@ -31,7 +31,7 @@ public function __construct(array $handlerMapping) $this->handlerMapping = $handlerMapping; } - public static function new(): self + public static function new() : self { $handlerMapping = [ 'boolean' => Commands\ProvideNumberSchemaInstanceHandler::new(), @@ -43,9 +43,10 @@ public static function new(): self return new self($handlerMapping); } - - final public function provideSchemaInstance(string $value, array $schema): Domain\Models\ObjectSchemaInstance|Domain\Models\SchemaInstance - { + final public function getSchemaInstance( + string $value, + array $schema + ) : Domain\Models\ObjectSchemaInstance|Domain\Models\SchemaInstance { $process = Processes\ProvideSchemaInstanceProcess::new(); $command = Commands\ProvideSchemaInstanceCommand::new($value, $schema, $this->handlerMapping); diff --git a/src/SchemaInstance.php b/src/SchemaInstance.php new file mode 100644 index 0000000..373363d --- /dev/null +++ b/src/SchemaInstance.php @@ -0,0 +1,23 @@ +schemaInstance = $schemaInstance; + } + + public static function fromDomain(Core\Domain\Models\SchemaInstance $schemaInstance) : self + { + return new self($schemaInstance); + } + + public function toArray() : array + { + return $this->schemaInstance->toArray(); + } +} \ No newline at end of file