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 JSON data from strings or arrays. Check if values can be safely converted to JSON without throwing exceptions.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_json;

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

echo 'Is array valid? ' . (type_json()->isValid(['name' => 'John']) ? 'yes' : 'no') . "\n";
echo 'Is JSON string valid? ' . (type_json()->isValid('{"name":"John"}') ? 'yes' : 'no') . "\n";
echo 'Is plain string valid? ' . (type_json()->isValid('hello') ? 'yes' : 'no') . "\n";
echo 'Is invalid JSON valid? ' . (type_json()->isValid('{invalid}') ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top