PHP code example of spacetab-io / body-validator

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

    

spacetab-io / body-validator example snippets



use Amp\Loop;
use HarmonyIO\Validation\Rule\Combinator\All;
use HarmonyIO\Validation\Rule\Email\RfcEmailAddress;
use HarmonyIO\Validation\Rule\Network\Url\Url;
use HarmonyIO\Validation\Rule\Text\AlphaNumeric;
use HarmonyIO\Validation\Rule\Text\LengthRange;
use Spacetab\BodyValidator\BodyValidator;
use Spacetab\BodyValidator\BodyValidatorInterface;

Loop::run(static function () {
    $body = [
        'username' => '__roquie',
        'password' => '1',
        'contacts' => [
            [
                'email' => '1mail@@example.com',
                'github' => 'https:/github.com/roquie',
            ],
            [
                'email' => '2mail@@example.com',
                'github' => 'https:/github.com/roquie',
            ],
        ],
    ];

    $validator = new BodyValidator($body, new class implements BodyValidatorInterface {
        public function validate(): iterable {
            yield 'username' => new All(new LengthRange(3, 15), new AlphaNumeric());
            yield 'password' => new LengthRange(12, 255);
            yield 'contacts.*.email' => new RfcEmailAddress();
            yield 'contacts.*.github' => new Url();
        }
    });

    /** @var \Spacetab\BodyValidator\ResultSet $result */
    $result = yield $validator->verify();

    $result->isValid();
    $result->getErrors();
});