PHP code example of nayzo / json-schema-validator-bundle

1. Go to this page and download the library: Download nayzo/json-schema-validator-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nayzo / json-schema-validator-bundle example snippets




use Nayzo\JsonSchemaValidatorBundle\Configurator\ValidationSubscriberSupportInterface;

final class MyAccountValidationSupport implements ValidationSubscriberSupportInterface
{
    public function __construct(private RequestStack $requestStack)
    {
    }
    
    public function support(): bool
    {
        // some code logic here... example:
        return '/v1/my-account' === $this->requestStack->getMainRequest()->getPathInfo();
    }

    public static function getResourceName(): string
    {
        return 'my_account';  // this value represents the resource name.
    }
}



    // EventSubscriber
    
    use Nayzo\JsonSchemaValidatorBundle\Validator\ValidatorFactory;
    
    public function __construct(private JsonSchemaValidatorContractInterface $myAccountContract)
    {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER => 'onKernelController',
            KernelEvents::RESPONSE => 'onKernelResponse',
        ];
    }
    
    public function onKernelController(ControllerEvent $event): void
    {       
        if ($event->getController() instanceof ErrorController) {
            return;
        }

        $request = $event->getRequest();
        $path = $this->myAccountContract->getContractFile();  // '/some/path/my-contract.yaml'
        
        $validatorFactory = new ValidatorFactory();
        $validator = $validatorFactory->build($path);
        $validator->validate($request, $request->getPathInfo(), $request->getMethod());
    }

    public function onKernelResponse(ResponseEvent $event): void
    {
        $request = $event->getRequest();
        $response = $event->getResponse();
        $path = $this->myAccountContract->getContractFile();  // '/some/path/my-contract.yaml'
        
        $validatorFactory = new ValidatorFactory();
        $validator = $validatorFactory->build($path);
        $validator->validate($response, $request->getPathInfo(), $request->getMethod());
    }



use Nayzo\JsonSchemaValidatorBundle\Contract\JsonSchemaValidatorContractInterface;

    public function __construct(
        private JsonSchemaValidatorContractInterface $myAccountContract, // represent "my_account" in "resources" configuration
        private JsonSchemaValidatorContractInterface $mySecondAccountContract, // represent "my_second_account" in "resources" configuration
    ) {
    }
    
    public function something(): array
    {
        $this->myAccountContract->getContractFile();        // '/some/path/my-contract.yaml'
        $this->mySecondAccountContract->getContractFile();  // '/some/path/my-second-contract.yaml'
    }

// Note: the "Contract" suffix must be added to the contract variable to be loaded correctly.