Custom Content encodings
Create a new JSON schema content encoding
A content encoding can be defined in two ways:
- as a callable, with the following signature:
function (string $value, string $type): ?string
- as class implementing
Opis\JsonSchema\ContentEncoding
interface. Thedecode
method has the following signature:public function decode(string $value, string $type): ?string
So, if you can decode the value you should return a string representing the decoded value,
otherwise you should return null
.
Using content encodings
You should register your content encodings before you validate any schema. If you want, you can also create a default content encoding handler, so you can use a generic lib to decode.
<?php
use Opis\JsonSchema\Validator;
use Opis\JsonSchema\Resolvers\ContentEncodingResolver;
$validator = new Validator();
/** @var ContentEncodingResolver $contentEncodings */
$contentEncodings = $validator->parser()->getContentEncodingResolver();
// Register a content encoding
$contentEncodings->registerCallable('my-encoding', function (string $content): ?string {
// return a string representing the decoded $content
// or null if you cannot decode
return null;
});
// Register the default handler
$contentEncodings->setDefaultHandler(function (string $content, string $encoding): ?string {
// return a string representing the decoded $content which is encoded as $encoding
// or null if you cannot decode
return null;
});