Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions content/1.getting-started/5.strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ $faker = new Faker();
$faker->regex('/^01$/')->postalCode(); // Postal code that begins with '01'
$faker->regex('/.com$/')->email(); // Email that ends with '.com'
```

## Valid

The valid strategy allows you to filter generated values using a custom validation callable. The callable must accept one parameter (the generated value) and return a boolean indicating whether the value is valid or not.

```php
use Xefi\Faker\Faker;

$faker = new Faker();

$faker->valid(function ($number) {return $number % 2 === 0; })->numberBetween(1, 100); // Even numbers
$faker->valid(fn ($number) => $number % 2 !== 0)->numberBetween(1, 100); // Odd numbers
$faker->valid(fn ($email) => str_ends_with($email, '.org'))->email(); // Emails that ends by '.org'
```