VOOZH about

URL: https://opis.io/json-schema/2.x/php-format.html

⇱ Creating Opis JSON Schema formats | Opis JSON Schema


Custom Formats

Create a new JSON schema format

Edit on GitHub

A format can be defined in two ways:

  • as a callable, with the following signature:
    function (mixed $value): bool
    
  • as class implementing Opis\JsonSchema\Format interface. The validate method has the following signature:
    public function validate(mixed $value): bool
    

Using formats

You should register your formats before you validate any schema. When you register a format you must specify:

  • JSON data type (number, integer, string, array, object)
  • name: the name you will use in your schemas
  • the format
<?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).

Copyright © Zindex Software. All rights reserved
👁 Image
On this page