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\SchemaFactory;
use Cortex\JsonSchema\Enums\SchemaFormat;
// Create a basic user schema using the SchemaFactory
$schema = SchemaFactory::object('user')
->description('User schema')
->properties(
SchemaFactory::string('name')
->minLength(2)
->maxLength(100)
->nalProperties(false)
->properties(
SchemaFactory::string('theme')
->enum(['light', 'dark']),
),
);
$schema = (new ObjectSchema('user'))
->description('User schema')
->properties(
(new StringSchema('name'))
->minLength(2)
->maxLength(100)
->
(new BooleanSchema('active'))
->default(true),
(new ObjectSchema('settings'))
->additionalProperties(false)
->properties(
(new StringSchema('theme'))
->enum(['light', 'dark']),
),
);
// Convert to array
$schema->toArray();
// Convert to JSON string
$schema->toJson();
$schema->toJson(JSON_PRETTY_PRINT);
$data = [
'name' => 'John Doe',
'email' => 'foo', // invalid email
'age' => 16,
'active' => true,
'settings' => [
'theme' => 'dark',
],
];
// Validate data against the schema
try {
$schema->validate($data);
} catch (SchemaException $e) {
echo $e->getMessage(); // "The data must match the 'email' format"
}
// Or just get a boolean
$schema->isValid($data); // false
use Cortex\JsonSchema\SchemaFactory;
$schema = SchemaFactory::string('name')
->minLength(2)
->maxLength(100)
->pattern('^[A-Za-z]+$')
->readOnly();
use Cortex\JsonSchema\SchemaFactory;
// Array must contain at least one number between 10 and 20
$schema = SchemaFactory::array('numbers')
->contains(
SchemaFactory::number()
->minimum(10)
->maximum(20)
)
->minContains(2) // must contain at least 2 such numbers
->maxContains(3); // must contain at most 3 such numbers
$schema->isValid([15, 12, 18]); // true (contains 3 numbers between 10-20)
$schema->isValid([15, 5, 25]); // false (only contains 1 number between 10-20)
$schema->isValid([15, 12, 18, 19]); // false (contains 4 numbers between 10-20)
use Cortex\JsonSchema\SchemaFactory;
$schema = SchemaFactory::array('coordinates')
->items(
SchemaFactory::number()->description('latitude'),
SchemaFactory::number()->description('longitude'),
);
$schema->isValid([51.5074, -0.1278]); // true (valid lat/long)
$schema->isValid(['invalid', -0.1278]); // false (first item must be number)
use Cortex\JsonSchema\SchemaFactory;
use Cortex\JsonSchema\Enums\SchemaFormat;
$schema = SchemaFactory::object('user')
->properties(
SchemaFactory::string('name')->')->enum(['light', 'dark'])
),
)
->additionalProperties(false);
use Cortex\JsonSchema\SchemaFactory;
$schema = SchemaFactory::object('config')
// Validate property names against a pattern
->propertyNames(
SchemaFactory::string()->pattern('^[a-zA-Z]+$')
)
// Control number of properties
->minProperties(1)
->maxProperties(10)
->additionalProperties(false);
// Property names must be alphabetic
$schema->isValid(['123' => 'value']); // false
$schema->isValid(['validKey' => 'value']); // true
use Cortex\JsonSchema\SchemaFactory;
$schema = SchemaFactory::object('config')
// Add a single pattern property
->patternProperty('^prefix_',
SchemaFactory::string()->minLength(5)
)
// Add multiple pattern properties
->patternProperties([
'^[A-Z][a-z]+$' => SchemaFactory::string(), // CamelCase properties
'^\d+$' => SchemaFactory::number(), // Numeric properties
]);
// Valid data
$schema->isValid([
'prefix_hello' => 'world123', // Matches ^prefix_ and meets minLength
'Name' => 'John', // Matches ^[A-Z][a-z]+$
'123' => 42, // Matches ^\d+$
]); // true
// Invalid data
$schema->isValid([
'prefix_hi' => 'hi', // Too short for minLength
'invalid' => 'no pattern', // Doesn't match any pattern
'123' => 'not a number', // Wrong type for pattern
]); // false
use Cortex\JsonSchema\SchemaFactory;
use Cortex\JsonSchema\Enums\SchemaType;
$schema = SchemaFactory::union([SchemaType::String, SchemaType::Integer], 'id')
->description('ID can be either a string or an integer')
->enum(['abc123', 'def456', 1, 2, 3])
->nullable();
$schema->isValid('abc123'); // true
$schema->isValid(1); // true
$schema->isValid(null); // true (because it's nullable)
$schema->isValid(true); // false (not a string or integer)
$schema->isValid('invalid'); // false (not in enum)
use Cortex\JsonSchema\Exceptions\SchemaException;
try {
$schema->validate($data);
} catch (SchemaException $e) {
echo $e->getMessage(); // "The data must match the 'email' format"
}
use Cortex\JsonSchema\SchemaFactory;
/**
* This is the description of the closure
*
* @param string $name The name of the user
* @param array $meta The meta data of the user
* @param ?int $age The age of the user
*/
$closure = function (string $name, array $meta, ?int $age = null): void {};
// Build the schema from the closure
$schema = SchemaFactory::fromClosure($closure);
// Convert to JSON Schema
$schema->toJson();
use Cortex\JsonSchema\SchemaFactory;
/**
* This is the description of the class
*/
class User
{
/**
* @var string The name of the user
*/
public string $name;
/**
* @var ?int The age of the user
*/
public ?int $age = null;
/**
* @var float The height of the user in meters
*/
public float $height = 1.7;
}
// Build the schema from the class
$schema = SchemaFactory::fromClass(User::class);
// Convert to JSON Schema
$schema->toJson();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.