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 constrained types like positive integers, non-empty strings, and numeric strings for stricter validation rules.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\{type_non_empty_string, type_numeric_string, type_positive_integer};

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

echo 'Is 5 positive int? ' . (type_positive_integer()->isValid(5) ? 'yes' : 'no') . "\n";
echo 'Is 0 positive int? ' . (type_positive_integer()->isValid(0) ? 'yes' : 'no') . "\n";
echo 'Is -3 positive int? ' . (type_positive_integer()->isValid(-3) ? 'yes' : 'no') . "\n";
echo 'Is "hello" non-empty? ' . (type_non_empty_string()->isValid('hello') ? 'yes' : 'no') . "\n";
echo 'Is "" non-empty? ' . (type_non_empty_string()->isValid('') ? 'yes' : 'no') . "\n";
echo 'Is "123" numeric string? ' . (type_numeric_string()->isValid('123') ? 'yes' : 'no') . "\n";
echo 'Is "abc" numeric string? ' . (type_numeric_string()->isValid('abc') ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top