JsonType matches JSON structures against templates. You can specify the type of fields in JSON or add additional validation rules.

JsonType is used by REST module in seeResponseMatchesJsonType and dontSeeResponseMatchesJsonType methods.

Usage example:

$jsonType = new JsonType(['name' => 'davert', 'id' => 1]);
  'name' => 'string:!empty',
  'id' => 'integer:>0|string:>0',
]); // => true

  'id' => 'string',
]); // => `id: 1` is not of type string

Class JsonType @package Codeception\Util


public __construct($jsonArray)

Creates instance of JsonType Pass an array or \Codeception\Util\JsonArray with data. If non-associative array is passed - the very first element of it will be used for matching.

  • param $jsonArray array \Codeception\Util\JsonArray

See source


public static addCustomFilter($name, callable $callable)

Adds custom filter to JsonType list. You should specify a name and parameters of a filter.


JsonType::addCustomFilter('slug', function($value) {
    return strpos(' ', $value) !== false;
// => use it as 'string:slug'

// add custom function to matcher with `len($val)` syntax
// parameter matching patterns should be valid regex and start with `/` char
JsonType::addCustomFilter('/len\((.*?)\)/', function($value, $len) {
  return strlen($value) == $len;
// use it as 'string:len(5)'
  • param $name
  • param callable $callable

See source


public static cleanCustomFilters()

Removes all custom filters

See source


public matches(array $jsonType)

Checks data against passed JsonType. If matching fails function returns a string with a message describing failure. On success returns true.

  • param array $jsonType
  • return bool string

See source

© 2011 Michael Bodnarchuk and contributors
Licensed under the MIT License.