Skip to content

Commit

Permalink
Merge pull request #6 from Innmind/is-just
Browse files Browse the repository at this point in the history
Add `Is::just()`
  • Loading branch information
Baptouuuu authored Nov 11, 2024
2 parents db014d9 + 7d1ff00 commit b21a5b5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

## 1.5.0 - 2024-11-10

Expand Down
15 changes: 15 additions & 0 deletions docs/constraints/maybe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Maybe monad

If a previous contraint outputs a [`Maybe`](https://innmind.org/Immutable/structures/maybe/) and you want to access the inner value you can do:

```php
use Innmind\Validation\Is;
use Innmind\Immutable\Maybe;

$validate = Is::int()
->or(Is::null())
->map(Maybe::of(...))
->and(Is::just());
```

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`.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ nav:
- Array shapes: constraints/array-shapes.md
- Dates: constraints/dates.md
- Objects: constraints/objects.md
- Maybe monad: constraints/maybe.md
- Custom: constraints/custom.md

theme:
Expand Down
30 changes: 29 additions & 1 deletion proofs/is.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
declare(strict_types = 1);

use Innmind\Validation\Is;
use Innmind\Immutable\Str;
use Innmind\Immutable\{
Str,
Maybe,
};
use Innmind\BlackBox\Set;

return static function() {
Expand Down Expand Up @@ -615,4 +618,29 @@ static function($assert, $keys, $values, $integer, $string, $random) {
);
},
);

yield proof(
'Is::just()',
given(Set\Integers::any()),
static function($assert, $value) {
$assert->same(
$value,
Is::int()
->map(Maybe::just(...))
->and(Is::just())($value)->match(
static fn($value) => $value,
static fn() => null,
),
);

$assert->false(
Is::null()
->map(Maybe::of(...))
->and(Is::just())(null)->match(
static fn($value) => $value,
static fn() => false,
),
);
},
);
};
20 changes: 20 additions & 0 deletions src/Is.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\Immutable\{
Validation,
Maybe,
Predicate as PredicateInterface,
};

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

/**
* @psalm-pure
* @template V
*
* @param ?non-empty-string $message
*
* @return Constraint<Maybe<V>, V>
*/
public static function just(?string $message = null): Constraint
{
/** @psalm-suppress MixedArgumentTypeCoercion */
return Of::callable(static fn(Maybe $value) => $value->match(
Validation::success(...),
static fn() => Validation::fail(Failure::of(
$message ?? 'No value was provided',
)),
));
}

/**
* @param non-empty-string $message
*
Expand Down

0 comments on commit b21a5b5

Please sign in to comment.