Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created June 2, 2020 17:30
Show Gist options
  • Save PJZ9n/0d5761c6e078834bededefd6de3035af to your computer and use it in GitHub Desktop.
Save PJZ9n/0d5761c6e078834bededefd6de3035af to your computer and use it in GitHub Desktop.
Particle/Validatorを使用したバリデーションのサンプル
<?php
declare(strict_types=1);
use Particle\Validator\Validator;
$input = [
"enabled" => true,
"items" => [
[
"id" => 400,
"meta" => 50,
"count" => 15,
],
],
];
$validator = new Validator();
$validator->required("enabled")->bool();
$validator->required("items")->each(function (Validator $itemValidator): void {
$itemValidator->required("id")->integer();
$itemValidator->required("meta")->integer();
$itemValidator->required("count")->integer();
});
$result = $validator->validate($input);
var_dump($result->isValid());
var_dump($result->getValues());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment