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 typed maps with specific key and value types. Check dictionary-like structures have consistent types.

Documentation

Code

<?php

declare(strict_types=1);

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

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

echo 'Is ["a"=>1,"b"=>2] valid? ' . (type_map(type_string(), type_integer())->isValid(['a' => 1, 'b' => 2]) ? 'yes' : 'no') . "\n";
echo 'Is ["a"=>1,"b"=>"x"] valid? ' . (type_map(type_string(), type_integer())->isValid(['a' => 1, 'b' => 'x']) ? 'yes' : 'no') . "\n";
echo 'Is [1=>100,2=>200] valid? ' . (type_map(type_string(), type_integer())->isValid([1 => 100, 2 => 200]) ? 'yes' : 'no') . "\n";
echo 'Is [] valid? ' . (type_map(type_string(), type_integer())->isValid([]) ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top