Skip to content

Commit

Permalink
add Is::shape()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Mar 5, 2024
1 parent c44c60c commit aa821e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- `Innmind\Validation\Is::shape()` as a shortcut to `Is::array()->and(Shape::of())`

### Fixed

- `OrConstraint::and()` was applying an _or_ constraint
Expand Down
24 changes: 24 additions & 0 deletions proofs/is.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,28 @@ static function($assert, $array, $other) {
);
},
);

yield proof(
'Is::shape()',
given(
Set\Strings::madeOf(Set\Chars::alphanumerical())->atLeast(1),
Set\Type::any(),
Set\Integers::any(),
),
static function($assert, $key, $value, $int) {
$assert->null(
Is::shape($key, Is::int())($value)->match(
static fn($value) => $value,
static fn() => null,
),
);
$assert->same(
[$key => $int],
Is::shape($key, Is::int())([$key => $int])->match(
static fn($value) => $value,
static fn() => null,
),
);
},
);
};
12 changes: 12 additions & 0 deletions src/Is.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ public static function list(): self
return new self(\array_is_list(...), 'list');
}

/**
* @psalm-pure
*
* @param non-empty-string $key
*
* @return Constraint<mixed, non-empty-array<non-empty-string, mixed>>
*/
public static function shape(string $key, Constraint $constraint): Constraint
{
return self::array()->and(Shape::of($key, $constraint));
}

public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
Expand Down

0 comments on commit aa821e3

Please sign in to comment.