Skip to content

Commit e14deee

Browse files
committed
Merge branch 'develop'
* develop: (56 commits) specify next release add integer ranges constraints add non empty string constraint use blackbox 6 syntax update links update changelog add failWith() documentation simplify readme use new api in documentation use promoted properties use old api in proof to reduce the PR diff deprecate Has::key()->withFailure() remove duplicated code flag internal classes/methods use provider for points in time to allow for ohter constraints in the future split shape implementation from constructor regroup array constraint inside a single provider for a more expressive api add Constraint::object() and move Constraint::instance() to Constraint::object()->instance() do not expose internal type make Constraint::build() private ...
2 parents 583927d + 03ccd15 commit e14deee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2363
-1393
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
# Changelog
22

3+
## 2.0.0 - 2025-04-12
4+
5+
### Added
6+
7+
- `Innmind\Validation\Constraint::object()`
8+
- `Innmind\Validation\Constraint::failWith()`
9+
- `Innmind\Validation\Constraint::string()->nonEmpty()`
10+
- `Innmind\Validation\Constraint::int()->positive()`
11+
- `Innmind\Validation\Constraint::int()->negative()`
12+
- `Innmind\Validation\Constraint::int()->range()`
13+
- `Innmind\Validation\Constraint\Provider`
14+
15+
### Changed
16+
17+
- `Innmind\Validation\Constraint` is now a `final` class
18+
- `Innmind\Validation\Failure::of()` is now `internal`
19+
- `Innmind\Validation\Failure::under()` is now `internal`
20+
- `Innmind\Validation\KeyPath::root()` is now `internal`
21+
- `Innmind\Validation\KeyPath::under()` is now `internal`
22+
- `Innmind\Validation\Predicate` is now `internal`
23+
24+
### Removed
25+
26+
- `Innmind\Validation\Each`, you must use `Is::list()` instead
27+
- `Innmind\Validation\AndConstraint`, you must use `Constraint::and()` instead
28+
- `Innmind\Validation\OrConstraint`, you must use `Constraint::or()` instead
29+
- `Innmind\Validation\Map`, you must use `Constraint::map()` instead
30+
331
## 1.9.0 - 2025-02-09
432

533
### Added

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2025 Innmind
3+
Copyright (c) 2025-present Innmind
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ composer require innmind/validation
1616

1717
```php
1818
use Innmind\Validation\{
19-
Shape,
2019
Is,
21-
Each,
2220
Failure,
2321
};
2422
use Innmind\Immutable\Sequence;
@@ -41,7 +39,7 @@ $invalid = [
4139
'submit' => true,
4240
];
4341

44-
$validate = Shape::of('id', Is::int())
42+
$validate = Is::shape('id', Is::int())
4543
->with('username', Is::string())
4644
->with(
4745
'addresses',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"require-dev": {
2828
"innmind/static-analysis": "^1.2.1",
29-
"innmind/black-box": "~5.5",
29+
"innmind/black-box": "~6.1",
3030
"innmind/coding-standard": "~2.0"
3131
}
3232
}

docs/constraints/associative-arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Here's the constraint to validate the HTTP response <https://packagist.org/packa
55
```php
66
use Innmind\Validation\Is;
77

8-
$validate = Is::associativeArray(
8+
$validate = Is::array()->associative(
99
Is::string(),
1010
Is::shape('repository', Is::string())
1111
->with('abandoned', Is::bool()->or(Is::string())),

docs/constraints/custom.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Custom
22

3-
At some point you'll want to use your own constraints for a custom business logic. Instead of implementing the whole `Constraint` interface you can do:
3+
At some point you'll want to use your own constraints for a custom business logic. You can do:
44

55
```php
66
use Innmind\Validation\{
7-
Of,
7+
Constraint,
88
Failure,
99
};
1010
use Innmind\Immutable\Validation;
1111

12-
$validate = Of::callable(static function(mixed $input) {
12+
$validate = Constraint::of(static function(mixed $input) {
1313
if (/* your validation here */) {
1414
return Validation::success($input);
1515
}
@@ -24,12 +24,12 @@ For example if you know the input has to be a `string` you can do:
2424

2525
```php hl_lines="7"
2626
use Innmind\Validation\{
27-
Of,
27+
Constraint,
2828
Failure,
2929
};
3030
use Innmind\Immutable\Validation;
3131

32-
$validate = Is::string()->and(Of::callable(static function(string $input) {
32+
$validate = Is::string()->and(Constraint::of(static function(string $input) {
3333
if (/* your validation here */) {
3434
return Validation::success($input);
3535
}

docs/constraints/dates.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
This will transform `string`s into `PointInTime`s from the [`innmind/time-continuum` package](https://github.com/Innmind/TimeContinuum/).
44

55
```php
6-
use Innmind\Validation\PointInTime;
6+
use Innmind\Validation\Constraint;
77
use Innmind\TimeContinuum\{
88
Clock,
99
Format,
1010
};
1111

12-
$validate = PointInTime::ofFormat(
13-
Clock::live(),
12+
$validate = Constraint::pointInTime(Clock::live())->format(
1413
Format::iso8601(),
1514
);
1615
```

docs/constraints/index.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Each `Constraint` have the following methods:
77
- `->or()`
88
- `->map()`
99
- `->asPredicate()`
10+
- `->failWith()`
1011

1112
## `->__invoke()`
1213

@@ -39,13 +40,13 @@ Let's take the example of making sure a `string` is shorter than `255` character
3940
```php hl_lines="3-4 6 9-14"
4041
use Innmind\Validation\{
4142
Is,
42-
Of,
43+
Constraint,
4344
Failure,
4445
};
4546
use Innmind\Immutable\Validation;
4647

4748
function(mixed $input): string {
48-
$validate = Is::string()->and(Of::callable(
49+
$validate = Is::string()->and(Constraint::of(
4950
static fn(string $value) => match (true) {
5051
\strlen($value) < 255 => Validation::success($value),
5152
default => Validation::fail(Failure::of('String is too long')),
@@ -120,6 +121,18 @@ Sequence::of(1, 'a', null, 'b', new \stdClass, 'c')
120121
??? note
121122
There's no need to apply transformations on your constraints when used as predicates as the outputed value is not used.
122123

124+
## `->failWith()`
125+
126+
This method allows to change the failure message.
127+
128+
```php
129+
use Innmind\Validation\Is;
130+
131+
$password = Is::string()->failWith('The password is required');
132+
133+
$password($someInput);
134+
```
135+
123136
## Handling failures
124137

125138
In the examples above we've thrown exceptions in case of errors but you have access to all the failures messages and where they happened.

docs/constraints/objects.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Objects
22

3-
You can check the input is an object of a given instance like this:
3+
You can check the input is an object like this:
4+
5+
```php
6+
use Innmind\Validation\Constraint;
7+
8+
$validate = Constraint::object();
9+
```
10+
11+
And if you want to make sure it is an instance of some class:
412

513
```php
614
use Innmind\Validation\Instance;

docs/constraints/primitives.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,44 @@
77
$validate = Is::string();
88
```
99

10+
=== "`non-empty-string`"
11+
```php
12+
use Innmind\Validation\Is;
13+
14+
$validate = Is::string()->nonEmpty();
15+
```
16+
1017
=== "`int`"
1118
```php
1219
use Innmind\Validation\Is;
1320

1421
$validate = Is::int();
1522
```
1623

24+
=== "`int<1, max>`"
25+
```php
26+
use Innmind\Validation\Is;
27+
28+
$validate = Is::int()->positive();
29+
```
30+
31+
=== "`int<min, -1>`"
32+
```php
33+
use Innmind\Validation\Is;
34+
35+
$validate = Is::int()->negative();
36+
```
37+
38+
=== "`int` range"
39+
```php
40+
use Innmind\Validation\Is;
41+
42+
$min = 0;
43+
$max = 100;
44+
45+
$validate = Is::int()->range($min, $max);
46+
```
47+
1748
=== "`float`"
1849
```php
1950
use Innmind\Validation\Is;
@@ -45,7 +76,7 @@
4576
By default the error message will be `Value is not of type {primitive}`. You can change it via:
4677

4778
```php
48-
$validate = Is::string()->withFailure('Some error message');
79+
$validate = Is::string()->failWith('Some error message');
4980
```
5081

5182
## Lists
@@ -61,7 +92,7 @@ use Innmind\Validation\Is;
6192
$validate = Is::list();
6293
```
6394

64-
You can also validate that each value in the list is of a given type. Here's how to validate a list os `string`s:
95+
You can also validate that each value in the list is of a given type. Here's how to validate a list of `string`s:
6596

6697
```php
6798
use Innmind\Validation\Is;

0 commit comments

Comments
 (0)