Download the PHP package joipolloi/json-validation-bundle without Composer

On this page you can find all versions of the php package joipolloi/json-validation-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-validation-bundle

JSON Validation Bundle

Build Status

A Symfony bundle that provides an annotation to validate JSON passed to a controller action against a schema.

Usage

When creating a controller method that accepts JSON as input (e.g. a POST method), put the @ValidateJson annotation on your action and point to the schema to validate against.

Now any time the action is called, the passed JSON will be validated against the schema. If there are no validation errors, the action will execute as normal. If there are errors then a 400 (bad request) response will be returned.

Installation

Use composer: composer require joipolloi/json-validation-bundle

Open AppKernel.php in your Symfony project:

Configuration

The only configuration option is whether to enable the application/problem+json event listener. This is described in detail below, it defaults to off, but can be enabled with the following configuration in your config.yml:

Details

Behind the scenes the bundle registers an event listener on the kernel.controller event that will validate the request content (i.e. $request->getContent();) against a JSON schema using the justinrainbow/json-schema library.

If there is an issue locating the JSON schema, decoding the JSON, decoding the JSON schema or validating against the JSON, a JsonValidationException (which extends BadRequestHttpException) is thrown with an error message.

Options

Getting the valid JSON

In order to save time and processing, you can get the validated JSON as an object by getting the validJson attribute on a request, or by putting $validJson as an argument to your action:

If you want the decoded JSON as an associative array or use the Symfony form component, type hint $validJson as an array:

This does incur a slight performance overhead versus getting an object as the JSON needs to be decoded twice: once to validate against the JSON schema and again as an associative array. If your JSON is large but only a single level deep then you may get better performance by just casting to an array:

Specifying the HTTP methods to validate upon

If your controller action listens on multiple HTTP methods (e.g. PUT and POST) and you only want to validate the JSON on one of them, you can use the methods parameter to the annotation:

Allowing for empty as a valid value

If for some reason you want to allow empty content to also be valid, use the emptyIsValid parameter to the annotation:

Note that only empty request content will be classed as valid; if empty but syntactically valid JSON is passed, this will still be validated against the schema (i.e. "{}" will not be counted as empty).

application/problem+json responses

An exception listener is included within the bundle that can send an application/problem+json response as detailed in RFC 7807. The listener is turned off by default to allow for your own application to handle the exception but can be turned on with configuration in your config.yml file:

If the listener is disabled, a 400 bad request exception is thrown and caught as per your application. If turned on and there is a problem decoding or validating the JSON, a response might look like:

The "errors" key will be an array of at least one error. Each error will be an object with at least a "message" key, but may additionally have "constraint", "pointer" and "property" keys with useful information.

While errors within this array should be safe to send back to the client, there may be some information leakage with regards paths - either to the schema or referenced files. If in doubt, disable the listener and roll your own to have more control.


All versions of json-validation-bundle with dependencies

PHP Build Version
Package Version
Requires symfony/config Version ^3.2
symfony/http-foundation Version ^3.2
justinrainbow/json-schema Version ^5.2
sensio/framework-extra-bundle Version >=3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package joipolloi/json-validation-bundle contains the following files

Loading the files please wait ....