PHP code example of dentelis / php-json-structure-validator

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

    

dentelis / php-json-structure-validator example snippets


//object example
$user = (new ObjectType())
    ->addProperty('name', (new StringType())->assertNotEmpty())
    ->addProperty('email', (new StringType())->assertEmail());
    
$data = json_decode('{"name":"user", "email":"[email protected]"}');
try {
    $user->validate($data);
} catch (ValidationException $e) {
    //do smth
    
}

//array of objects
$users = (new ArrayType())
    ->assertNotEmpty()
    ->assertType($user);

$data = json_decode('[{"name":"user", "email":"[email protected]"},{"name":"user", "email":"[email protected]"}]');
try {
    $users->validate($data);
} catch (ValidationException $e) {
    //do smth

}