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 optional (nullable) types. Check if values are either the base type or null for fields that may be missing.

Documentation

Code

<?php

declare(strict_types=1);

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

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

echo 'Is "hello" valid ?string? ' . (type_optional(type_string())->isValid('hello') ? 'yes' : 'no') . "\n";
echo 'Is null valid ?string? ' . (type_optional(type_string())->isValid(null) ? 'yes' : 'no') . "\n";
echo 'Is 123 valid ?string? ' . (type_optional(type_string())->isValid(123) ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top