![]() |
VOOZH | about |
Create a new JSON schema format
A format can be defined in two ways:
function (mixed $value): bool
Opis\JsonSchema\Format interface. The validate method has the following signature:
public function validate(mixed $value): bool
You should register your formats before you validate any schema. When you register a format you must specify:
<?php
use Opis\JsonSchema\Validator;
use Opis\JsonSchema\Resolvers\FormatResolver;
$validator = new Validator();
/** @var FormatResolver $formats */
$formats = $validator->parser()->getFormatResolver();
$isPrime = function (int $value): bool {
if ($value < 2) {
return false;
}
if ($value == 2) {
return true;
}
$max = floor(sqrt($value)) + 1;
for ($i = 3; $i < $max; $i += 2) {
if ($value % $i == 0) {
return false;
}
}
return true;
};
// Register our prime number format
$formats->registerCallable("integer", "prime", $isPrime);
Here is an example that uses our prime number format
{
"type": "integer",
"format": "prime"
}
This schema validates 5, but does not validate 9 (3 * 3).
Create a new JSON schema filter
Create a new JSON schema media (MIME) type