PHP code example of riley19280 / fluent-json-schema

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

    

riley19280 / fluent-json-schema example snippets


use FluentJsonSchema\FluentSchema;

$schema = FluentSchema::make()
    ->type()->object()
    ->property('name', FluentSchema::make()
        ->type()->string()
    )
    ->return()
    ->compile();
/* Results in
{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
}
*/

FluentSchema::make()
    ->type()->object()
    ->additionalProperties(FluentSchema::make()->false())

$schema = FluentSchema::make()->schema('schema')->id('id')->compile();
// Will be
// { "$schema": "schema", "$id": "id" }
$schema = FluentSchema::make()->id('id')->schema('schema')->compile();
// Will be
// { "$id": "id", "$schema": "schema" }

$isValid = FluentSchema::make()
    ->type()->object()
    ->property('name', FluentSchema::make()
        ->type()->string()
    )
    ->return()
    ->validate((object)[
        'name' => 'validated!',
    ])
    ->isValid();