myaza-software / schema-validator
Check mapping array to object
Installs: 58
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8
- doctrine/annotations: ^1.0
- phpdocumentor/reflection: ^4.0
- phpdocumentor/reflection-docblock: ^5.2
- symfony/dependency-injection: ^4.3.3 || ^5.0
- symfony/framework-bundle: ^4.3.3 || ^5.0
- symfony/http-kernel: ^4.3.3 || ^5.0
- symfony/property-access: ^4.3.3 || ^5.0
- symfony/property-info: ^4.3.3 || ^5.0
- symfony/serializer: ^4.3.3 || ^5.0
- symfony/validator: ^4.3.3 || ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- infection/infection: ^0.23.0
- matthiasnoback/symfony-dependency-injection-test: ^4.2
- myclabs/php-enum: ^1.8
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.16.0
- psalm/plugin-symfony: ^2.3
- ramsey/uuid: ^4.1
- roave/infection-static-analysis-plugin: ^1.8
- symfony/uid: ^5.3
- vimeo/psalm: ^4.7
This package is auto-updated.
Last update: 2025-02-27 17:56:14 UTC
README
Schema Validator
Description
Check mapping array to object
Requirements
- php >= 8.0
- symfony >= 4.0
- symfony/serializer >= 4.0
Installation
The package could be installed with composer:
composer install myaza-software/schema-validator
Supported type:
- String,Float,Int,Array
- Union type
- Array of object/string
- Uuid symfony/ramsey
- Enum MyCLabs
- DateTimeInterface
Example
<?php declare(strict_types=1); namespace App; use App\DTO\RefundWebhook; use SchemaValidator\Schema; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; final class ValidateDtoCommand extends Command { protected static $defaultName = 'validate:dto'; public function __construct( private ValidatorInterface $validator, private SerializerInterface $serializer, ){ parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output) { $errors = $this->validator->validate([ 'type' => 123123, 'uuid' => '', 'price' => [ 'vault' => [ 'value' => 1, 'names' => [ ['value' => 1, 'createdAt' => ''] ] ],'value' => 1] ], new Schema([ 'type' => RefundWebhook::class, 'strictTypes' => true ])); $output->write($this->serializer->serialize($errors,'json')); return self::SUCCESS; } }
Output:
{ "type": "https:\/\/symfony.com\/errors\/validation", "title": "Validation Failed", "detail": "price.vault.value: This value should be of type string.\nprice.vault.names[0].value: This value should be of type string.\nprice.vault.names[0].createdAt: This is not a valid date.\nprice.value: This value should be of type float.\nuuid: This value should not be blank.\ndate: This is not a valid date.\ntype: This value should be of type string.", "violations": [ { "propertyPath": "price.vault.value", "title": "This value should be of type string.", "parameters": { "{{ value }}": "1", "{{ type }}": "string" }, "type": "urn:uuid:ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "price.vault.names[0].value", "title": "This value should be of type string.", "parameters": { "{{ value }}": "1", "{{ type }}": "string" }, "type": "urn:uuid:ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "price.vault.names[0].createdAt", "title": "This is not a valid date.", "parameters": [], "type": "urn:uuid:780e721d-ce3c-42f3-854d-f990ebffdc4f" }, { "propertyPath": "price.value", "title": "This value should be of type float.", "parameters": { "{{ value }}": "1", "{{ type }}": "float" }, "type": "urn:uuid:ba785a8c-82cb-4283-967c-3cf342181b40" }, { "propertyPath": "uuid", "title": "This value should not be blank.", "parameters": { "{{ value }}": "\"\"" }, "type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3" }, { "propertyPath": "date", "title": "This is not a valid date.", "parameters": [], "type": "urn:uuid:780e721d-ce3c-42f3-854d-f990ebffdc4f" }, { "propertyPath": "type", "title": "This value should be of type string.", "parameters": { "{{ value }}": "123123", "{{ type }}": "string" }, "type": "urn:uuid:ba785a8c-82cb-4283-967c-3cf342181b40" } ] }