Skip to content

Commit d93e735

Browse files
authored
Merge pull request #34 from xefi/valid-strategy
✨ Valid strategy
2 parents ce11c96 + 345a82b commit d93e735

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

content/1.getting-started/5.strategies.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ $faker = new Faker();
4343
$faker->regex('/^01$/')->postalCode(); // Postal code that begins with '01'
4444
$faker->regex('/.com$/')->email(); // Email that ends with '.com'
4545
```
46+
47+
## Valid
48+
49+
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.
50+
51+
```php
52+
use Xefi\Faker\Faker;
53+
54+
$faker = new Faker();
55+
56+
$faker->valid(function ($number) {return $number % 2 === 0; })->numberBetween(1, 100); // Even numbers
57+
$faker->valid(fn ($number) => $number % 2 !== 0)->numberBetween(1, 100); // Odd numbers
58+
$faker->valid(fn ($email) => str_ends_with($email, '.org'))->email(); // Emails that ends by '.org'
59+
```

0 commit comments

Comments
 (0)