Skip to content
Search
Examples

Types

Description

Type validation checks if a value matches the expected type without throwing exceptions. The isValid() method returns true if the value conforms to the type, false otherwise. This is useful for conditional logic, filtering data, or performing pre-flight checks before processing.

Unlike assertions, validation never throws - it's designed for control flow decisions rather than enforcing contracts.


Validate array structures with isValid(). Check for required keys, correct types, and nested structures safely.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\{type_boolean, type_integer, type_string, type_structure};

require __DIR__ . '/vendor/autoload.php';

$userType = type_structure(['id' => type_integer(), 'name' => type_string(), 'active' => type_boolean()]);

echo 'Valid user? ' . ($userType->isValid(['id' => 1, 'name' => 'John', 'active' => true]) ? 'yes' : 'no') . "\n";
echo 'Missing field? ' . ($userType->isValid(['id' => 1, 'name' => 'John']) ? 'yes' : 'no') . "\n";
echo 'Wrong type? ' . ($userType->isValid(['id' => 'one', 'name' => 'John', 'active' => true]) ? 'yes' : 'no') . "\n";
echo 'Extra field? ' . ($userType->isValid(['id' => 1, 'name' => 'John', 'active' => true, 'email' => 'j@x.com']) ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top