PHPackages                             mrsuh/json-validation-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. mrsuh/json-validation-bundle

ActiveSymfony-bundle[Validation &amp; Sanitization](/categories/validation)

mrsuh/json-validation-bundle
============================

This bundle provides a way to validate JSON in request/response against a schema

4.1.1(3y ago)177.5k↓23.1%3[3 issues](https://github.com/mrsuh/json-validation-bundle/issues)MITPHPPHP &gt;=8.0

Since May 9Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mrsuh/json-validation-bundle)[ Packagist](https://packagist.org/packages/mrsuh/json-validation-bundle)[ RSS](/packages/mrsuh-json-validation-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (23)Used By (0)

JSON Validation Bundle
======================

[](#json-validation-bundle)

[![](https://github.com/mrsuh/json-validation-bundle/actions/workflows/tests.yml/badge.svg)](https://github.com/mrsuh/json-validation-bundle/actions/workflows/tests.yml/badge.svg)[![](https://camo.githubusercontent.com/b0d281a2f967eb8fba4b7287289a7366424b5629e21fdeb442c96eccd0889c78/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a736f6e2d76616c69646174696f6e2d62756e646c652e737667)](https://camo.githubusercontent.com/b0d281a2f967eb8fba4b7287289a7366424b5629e21fdeb442c96eccd0889c78/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a736f6e2d76616c69646174696f6e2d62756e646c652e737667)[![](https://camo.githubusercontent.com/543635b1fd08f487a352284c9f6d71a2e4be3ccba97e2bce6b2d36a776bb2bd8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d727375682f6a736f6e2d76616c69646174696f6e2d62756e646c65)](https://camo.githubusercontent.com/543635b1fd08f487a352284c9f6d71a2e4be3ccba97e2bce6b2d36a776bb2bd8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d727375682f6a736f6e2d76616c69646174696f6e2d62756e646c65)

A Symfony bundle that provides an annotation to validate request/response JSON against a schema.

### Differences from joipolloi/json-validation-bundle

[](#differences-from-joipolloijson-validation-bundle)

- added `response` validation
- supporting Symfony `>=3.4`, `4.*`, `5.*`, `6.*`
- error/warnings logging
- single validator usage

Versions
--------

[](#versions)

- ^3 for Symfony `< 6.*`
- ^4 for Symfony `>= 6.*`

Installation
------------

[](#installation)

```
composer require mrsuh/json-validation-bundle ^4
```

Usage
-----

[](#usage)

Create validation schemes
See [json-schema](http://json-schema.org/) for more details

JsonSchema/Request/myAction.json

```
{
    "description": "Request JSON schema",
    "type": "object",
    "properties": {
        "test": {
            "type": "string",
            "minLength": 1
        }
    },
    "required": [ "test" ]
}
```

JsonSchema/Response/myAction.json

```
{
    "description": "Response JSON schema",
    "type": "object",
    "properties": {
        "test": {
            "type": "string",
            "minLength": 1
        }
    },
    "required": [ "test" ]
}
```

Create controller with annotations `ValidateJsonRequest` and/or `ValidateJsonResponse`
Specify `$validJson` argument if you want get decoded JSON data from the request
Specify the `array` type of the `$validJson` argument if you want get decoded JSON data as `array`
Specify the `object` type of the `$validJson` argument or don't specify type if you want get decoded JSON data as `object`

Controller/MyController.php

```
