PHP code example of webdevcave / schema-validator

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

    

webdevcave / schema-validator example snippets


use Webdevcave\SchemaValidator\Validator;

$validator = Validator::string()
    ->min(3)
    ->max(50);

// Validate the data
$isValid = $validator->validate('Carlos');

if ($isValid) {
    echo "Data is valid!";
} else {
    echo "Data is invalid!";
}

use Webdevcave\SchemaValidator\Validator;

$validator = Validator::array([
    'name' => Validator::string()
        ->min(3)
        ->max(50),
    'email' => Validator::string()
        ->pattern('/^\w+@(\w+|\.)+$/'),
    'address' => Validator::array([
        'street' => Validator::string()->max(200),
        'city' => Validator::string()->max(50),
        'postal_code' => Validator::string()
            ->min(1)
            ->max(15),
    ])
]);

// Data to be validated
$data = [
    'name' => 'Alice Johnson',
    'email' => '[email protected]',
    'address' => [
        'street' => '123 Maple St',
        'city' => 'Somewhere',
        'postal_code' => '12345'
    ]
];

// Validate the data
$isValid = $validator->validate($data);

if ($isValid) {
    echo "Data is valid!";
} else {
    echo "Data is invalid!";
}

use Webdevcave\SchemaValidator\Validator;

// Data to be validated
$data = [
    'name' => 'John Doe',
    'age' => 'thirty'
];

// Define the validation schema
$validator = Validator::array([
    'name'  => Validator::string(),
    'age'   => Validator::numeric()
        ->integer('Only integer numbers are allowed')
        ->positive('Age field must be positive')
]);

// Validate the data
if (!$validator->validate($data)) {
    // Get and print the validation errors
    print_r($validator->errorMessages());
}
bash
composer