Skip to content

Commit 7d1ff00

Browse files
committed
add Is::just()
1 parent db014d9 commit 7d1ff00

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `Shape::rename()` to rename a key in the output array
88
- `Shape::default()` to specify a default value when an optional key is not set
9+
- `Is::just()`
910

1011
## 1.5.0 - 2024-11-10
1112

docs/constraints/maybe.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Maybe monad
2+
3+
If a previous contraint outputs a [`Maybe`](https://innmind.org/Immutable/structures/maybe/) and you want to access the inner value you can do:
4+
5+
```php
6+
use Innmind\Validation\Is;
7+
use Innmind\Immutable\Maybe;
8+
9+
$validate = Is::int()
10+
->or(Is::null())
11+
->map(Maybe::of(...))
12+
->and(Is::just());
13+
```
14+
15+
In this example the input can be an `int` or `null` but it will fail the validation in case the value is `null` because `Maybe::of(...)` will move the `null` as a `Nothing` and we say we want a `Just`.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ nav:
1111
- Array shapes: constraints/array-shapes.md
1212
- Dates: constraints/dates.md
1313
- Objects: constraints/objects.md
14+
- Maybe monad: constraints/maybe.md
1415
- Custom: constraints/custom.md
1516

1617
theme:

proofs/is.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
declare(strict_types = 1);
33

44
use Innmind\Validation\Is;
5-
use Innmind\Immutable\Str;
5+
use Innmind\Immutable\{
6+
Str,
7+
Maybe,
8+
};
69
use Innmind\BlackBox\Set;
710

811
return static function() {
@@ -615,4 +618,29 @@ static function($assert, $keys, $values, $integer, $string, $random) {
615618
);
616619
},
617620
);
621+
622+
yield proof(
623+
'Is::just()',
624+
given(Set\Integers::any()),
625+
static function($assert, $value) {
626+
$assert->same(
627+
$value,
628+
Is::int()
629+
->map(Maybe::just(...))
630+
->and(Is::just())($value)->match(
631+
static fn($value) => $value,
632+
static fn() => null,
633+
),
634+
);
635+
636+
$assert->false(
637+
Is::null()
638+
->map(Maybe::of(...))
639+
->and(Is::just())(null)->match(
640+
static fn($value) => $value,
641+
static fn() => false,
642+
),
643+
);
644+
},
645+
);
618646
};

src/Is.php

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Innmind\Immutable\{
77
Validation,
8+
Maybe,
89
Predicate as PredicateInterface,
910
};
1011

@@ -162,6 +163,25 @@ public static function associativeArray(Constraint $key, Constraint $value): Ass
162163
return AssociativeArray::of($key, $value);
163164
}
164165

166+
/**
167+
* @psalm-pure
168+
* @template V
169+
*
170+
* @param ?non-empty-string $message
171+
*
172+
* @return Constraint<Maybe<V>, V>
173+
*/
174+
public static function just(?string $message = null): Constraint
175+
{
176+
/** @psalm-suppress MixedArgumentTypeCoercion */
177+
return Of::callable(static fn(Maybe $value) => $value->match(
178+
Validation::success(...),
179+
static fn() => Validation::fail(Failure::of(
180+
$message ?? 'No value was provided',
181+
)),
182+
));
183+
}
184+
165185
/**
166186
* @param non-empty-string $message
167187
*

0 commit comments

Comments
 (0)