ptyhard / json-schema-bundle
this bundles is json schema validation support.
0.0.4
2020-07-12 08:51 UTC
Requires
- php: ^7.2
- ext-json: *
- doctrine/common: ~2.7
- justinrainbow/json-schema: ^5.2
- psr/simple-cache: ^1.0
- symfony/cache: ~4.4|^5.0
- symfony/config: ~4.4|^5.0
- symfony/console: ~4.4|^5.0
- symfony/dependency-injection: ~4.4|^5.0
- symfony/event-dispatcher: ~4.4|^5.0
- symfony/filesystem: ~4.4|^5.0
- symfony/http-foundation: ~4.4|^5.0
- symfony/http-kernel: ~4.4|^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.5
README
JsonSchema Validate For Symfony Bundle.
Installation
$ composer req ptyhard/json-schema-bundle "dev-master"
Add config/bundles.php
<?php return [ ... Ptyhard\JsonSchemaBundle\JsonSchemaBundle::class => ['all' => true] ];
Introduce bundle configuration to your config file
# config/packages/ptyhard_json_schema.yml ptyhard_json_schema: ~ use_jms_serializer: true # default true json_file_directory: ~ # default null json_write_directory: # default null
Usage
Create Schema php class.
<?php // src/JsonSchema/User.php namespace App\JsonSchema; use Ptyhard\JsonSchemaBundle\Annotations\SchemaClass; use Ptyhard\JsonSchemaBundle\Annotations\Property; /** * @SchemaClass(required={"id","name"}) */ class User { /** * @Property\NumberProperty("id") * * @var int */ private $id; /** * @Property\StringProperty("name") * * @var string */ private $name; }
Create controller class.
<?php namespace App\Controller; use App\JsonSchema\User; use Polidog\SimpleApiBundle\Annotations\Api; use Symfony\Component\Routing\Annotation\Route; /** * @Route("/") */ class TopController { /** * @Route("/request/check",methods={"POST"}) * @Api(statusCode=200) * * @param User $user * @return User */ public function requestCheck(User $user) :User { return []; } /** * @Route("/response/check",methods={"GET"}) * @Api(statusCode=200) * * @return User */ public function responseCheck() :User { return new User(); } }
Generate object to json schema file.
If you need json schema file, can use generate command.
$ bin/console json-schema:generate:file