PHP code example of cortexphp / json-schema

1. Go to this page and download the library: Download cortexphp/json-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/ */

    

cortexphp / json-schema example snippets


use Cortex\JsonSchema\Schema;
use Cortex\JsonSchema\Enums\SchemaFormat;

// Create a schema
$schema = Schema::object('user')
    ->description('User schema')
    ->properties(
        Schema::string('name')
            ->minLength(2)
            ->maxLength(100)
            ->ings')
            ->additionalProperties(false)
            ->properties(
                Schema::string('theme')
                    ->enum(['light', 'dark']),
            ),
    );

$data = [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'age' => 30,
    'active' => true,
    'settings' => [
        'theme' => 'light',
    ],
];

if ($schema->isValid($data)) {
    echo "Valid!";
} else {
    try {
        $schema->validate($data);
    } catch (\Cortex\JsonSchema\Exceptions\SchemaException $e) {
        echo $e->getMessage();
    }
}

// Convert to array
$schema->toArray();

// Convert to JSON string
echo $schema->toJson(JSON_PRETTY_PRINT);