programster/json-schema-creator

A PHP package to make it easier create JSON schemas.

1.2.0 2021-07-08 15:41 UTC

This package is auto-updated.

Last update: 2024-10-08 23:00:04 UTC


README

JSON Schema Creator This is a simple PHP package to make it quick and easy to generate a JSON schema that can then be used for validation and documentation.

This tool was built primarily with the help of the documentation for JSON schemas at opis.io/json-schema.

Install

composer require programster/json-schema-creator

Example

<?php
require_once(__DIR__ . '/../vendor/autoload.php');

$name = new \Programster\JsonSchema\Types\StringType(name: "product_name", minLength: 3);
$barcode = new \Programster\JsonSchema\Types\StringType(name: "barcode", minLength: 12);
$description = new \Programster\JsonSchema\Types\StringType(name: "description", minLength: 12);

$requiredProperties = array(
    $name,
    $barcode,
);

$nonRequiredProprties = array(
    $description
);

$object = new Programster\JsonSchema\Types\ObjectType(
    "Product",
    new Programster\JsonSchema\PropertyCollection(...$requiredProperties, ...$nonRequiredProprties),
    new Programster\JsonSchema\RequiredPropertiesCollection(...$requiredProperties)
);

$schema = new \Programster\JsonSchema\Schema($object, "Product Schema", "A product definition");
print($schema) . PHP_EOL;

That would generate the following schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "Product Schema",
    "title": "A product definition",
    "name": "Product",
    "type": "object",
    "properties": {
        "product_name": {
            "type": "string",
            "minLength": 3
        },
        "barcode": {
            "type": "string",
            "minLength": 12
        },
        "description": {
            "type": "string",
            "minLength": 12
        }
    },
    "required": [
        "product_name",
        "barcode"
    ]
}

Testing

You can check that your generated schema is valid by using jsonschemalint.com