PHP code example of sentinelphp / schema

1. Go to this page and download the library: Download sentinelphp/schema library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sentinelphp / schema example snippets


use SentinelPHP\Schema\Generator;
use SentinelPHP\Schema\Config\GeneratorConfig;

$generator = new Generator();

$data = [
    'id' => 123,
    'name' => 'John Doe',
    'email' => '[email protected]',
    'created_at' => '2024-01-15T10:30:00Z',
];

$schema = $generator->generate($data);
// Returns JSON Schema with inferred types and formats

use SentinelPHP\Schema\Merger;

$merger = new Merger();

$schema1 = ['type' => 'object', 'properties' => ['id' => ['type' => 'integer']]];
$schema2 = ['type' => 'object', 'properties' => ['name' => ['type' => 'string']]];

$merged = $merger->merge($schema1, $schema2);
// Combines properties, widens types, intersects 

use SentinelPHP\Schema\Validator;

$validator = new Validator();

$schema = [
    'type' => 'object',
    'properties' => [
        'email' => ['type' => 'string', 'format' => 'email'],
    ],
    '   }
}

$result = $validator->validatePartial($partialData, $schema);

use SentinelPHP\Schema\Config\GeneratorConfig;

$config = new GeneratorConfig(
    strictMode: true,           // Require all observed fields
    nullableFields: false,      // Don't allow null by default
    additionalProperties: false // Disallow extra properties
);

$generator = new Generator();
$schema = $generator->generate($data, $config);