Skip to content

Commit cae3a17

Browse files
committed
exclusiveMinimum and exclusiveMaximums are boolean now
1 parent c199343 commit cae3a17

8 files changed

Lines changed: 119 additions & 191 deletions

File tree

doc/ErrorHandling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Each schema type uses a consistent error code prefix:
267267
| Schema | Prefix | Example Codes |
268268
|--------|--------|---------------|
269269
| string | `string.` | `string.type`, `string.minLength`, `string.email` |
270-
| int | `int.` | `int.type`, `int.exclusiveMinimum`, `int.positive` |
270+
| int | `int.` | `int.type`, `int.minimum`, `int.positive` |
271271
| float | `float.` | `float.type`, `float.minimum`, `float.negative` |
272272
| bool | `bool.` | `bool.type` |
273273
| const | `const.` | `const.type` |

doc/Schema/FloatSchema.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ $data = $schema->parse(4.2); // Returns: 4.2
2020

2121
```php
2222
$schema->minimum(5.0); // Greater than or equal to 5.0
23-
$schema->exclusiveMinimum(5.0); // Greater than 5.0
24-
$schema->exclusiveMaximum(10.0); // Less than 10.0
23+
$schema->minimum(5.0, true); // Greater than 5.0
24+
$schema->maximum(10.0, true); // Less than 10.0
2525
$schema->maximum(10.0); // Less than or equal to 10.0
2626
```
2727

@@ -82,8 +82,6 @@ $coordinatesSchema->parse(['lat' => 47.1, 'lng' => 8.2]);
8282
| Code | Description |
8383
|------|-------------|
8484
| `float.type` | Value is not a float |
85-
| `float.minimum` | Value is not greater than or equal to threshold (used by `minimum` and `nonNegative()`) |
86-
| `float.exclusiveMinimum` | Value is not greater than threshold (used by `exclusiveMinimum()` and `positive()`) |
87-
| `float.exclusiveMaximum` | Value is not less than threshold (used by `exclusiveMaximum()` and `negative()`) |
88-
| `float.maximum` | Value is not less than or equal to threshold (used by `maximum()` and `nonPositive()`) |
85+
| `float.minimum` | Value is not greater than or equal to threshold |
86+
| `float.maximum` | Value is not less than or equal to threshold |
8987
| `float.int` | Cannot convert float to int without precision loss (for `toInt()`) |

doc/Schema/IntSchema.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ $data = $schema->parse(1337); // Returns: 1337
2020

2121
```php
2222
$schema->minimum(5); // Greater than or equal to 5
23-
$schema->exclusiveMinimum(5); // Greater than 5
24-
$schema->exclusiveMaximum(10); // Less than 10
23+
$schema->minimum(5, true); // Greater than 5
24+
$schema->maximum(10, true); // Less than 10
2525
$schema->maximum(10); // Less than or equal to 10
2626
```
2727

@@ -90,7 +90,5 @@ $date = $timestampSchema->parse(1705744500);
9090
| Code | Description |
9191
|------|-------------|
9292
| `int.type` | Value is not an integer |
93-
| `int.minimum` | Value is not greater than or equal to threshold (used by `minimum` and `nonNegative()`) |
94-
| `int.exclusiveMinimum` | Value is not greater than threshold (used by `exclusiveMinimum()` and `positive()`) |
95-
| `int.exclusiveMaximum` | Value is not less than threshold (used by `exclusiveMaximum()` and `negative()`) |
96-
| `int.maximum` | Value is not less than or equal to threshold (used by `maximum()` and `nonPositive()`) |
93+
| `int.minimum` | Value is not greater than or equal to threshold |
94+
| `int.maximum` | Value is not less than or equal to threshold |

src/Schema/FloatSchema.php

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
1313
public const string ERROR_TYPE_TEMPLATE = 'Type should be "float", {{given}} given';
1414

1515
public const string ERROR_MINIMUM_CODE = 'float.minimum';
16-
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be greater than or equal {{minimum}}, {{given}} given';
17-
18-
public const string ERROR_EXCLUSIVE_MINIMUM_CODE = 'float.exclusiveMinimum';
19-
public const string ERROR_EXCLUSIVE_MINIMUM_TEMPLATE = 'Value should be greater than {{exclusiveMinimum}}, {{given}} given';
20-
21-
public const string ERROR_EXCLUSIVE_MAXIMUM_CODE = 'float.exclusiveMaximum';
22-
public const string ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE = 'Value should be lesser than {{exclusiveMaximum}}, {{given}} given';
16+
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}} {{exclusiveMinimum}}, {{given}} given';
2317

2418
public const string ERROR_MAXIMUM_CODE = 'float.maximum';
25-
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be lesser than or equal {{maximum}}, {{given}} given';
19+
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}} {{exclusiveMaximum}}, {{given}} given';
2620

2721
public const string ERROR_GTE_CODE = 'float.gte';
2822
public const string ERROR_GTE_TEMPLATE = 'Value should be greater than or equal {{gte}}, {{given}} given';
@@ -39,80 +33,46 @@ final class FloatSchema extends AbstractSchemaInnerParse implements SchemaInterf
3933
public const string ERROR_INT_CODE = 'float.int';
4034
public const string ERROR_INT_TEMPLATE = 'Cannot convert {{given}} to int';
4135

42-
public function minimum(float $minimum): static
36+
public function minimum(float $minimum, bool $exclusiveMinimum = false): static
4337
{
44-
return $this->postParse(static function (float $float) use ($minimum) {
45-
if ($float >= $minimum) {
38+
return $this->postParse(static function (float $float) use ($minimum, $exclusiveMinimum) {
39+
if ((!$exclusiveMinimum && $float >= $minimum) || ($exclusiveMinimum && $float > $minimum)) {
4640
return $float;
4741
}
4842

4943
throw new ErrorsException(
5044
new Error(
5145
self::ERROR_MINIMUM_CODE,
5246
self::ERROR_MINIMUM_TEMPLATE,
53-
['minimum' => $minimum, 'given' => $float]
54-
)
55-
);
56-
});
57-
}
58-
59-
public function exclusiveMinimum(float $exclusiveMinimum): static
60-
{
61-
return $this->postParse(static function (float $float) use ($exclusiveMinimum) {
62-
if ($float > $exclusiveMinimum) {
63-
return $float;
64-
}
65-
66-
throw new ErrorsException(
67-
new Error(
68-
self::ERROR_EXCLUSIVE_MINIMUM_CODE,
69-
self::ERROR_EXCLUSIVE_MINIMUM_TEMPLATE,
70-
['exclusiveMinimum' => $exclusiveMinimum, 'given' => $float]
71-
)
72-
);
73-
});
74-
}
75-
76-
public function exclusiveMaximum(float $exclusiveMaximum): static
77-
{
78-
return $this->postParse(static function (float $float) use ($exclusiveMaximum) {
79-
if ($float < $exclusiveMaximum) {
80-
return $float;
81-
}
82-
83-
throw new ErrorsException(
84-
new Error(
85-
self::ERROR_EXCLUSIVE_MAXIMUM_CODE,
86-
self::ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE,
87-
['exclusiveMaximum' => $exclusiveMaximum, 'given' => $float]
47+
['minimum' => $minimum, 'exclusiveMinimum' => $exclusiveMinimum, 'given' => $float]
8848
)
8949
);
9050
});
9151
}
9252

93-
public function maximum(float $maximum): static
53+
public function maximum(float $maximum, bool $exclusiveMaximum = false): static
9454
{
95-
return $this->postParse(static function (float $float) use ($maximum) {
96-
if ($float <= $maximum) {
55+
return $this->postParse(static function (float $float) use ($maximum, $exclusiveMaximum) {
56+
if ((!$exclusiveMaximum && $float <= $maximum) || ($exclusiveMaximum && $float < $maximum)) {
9757
return $float;
9858
}
9959

10060
throw new ErrorsException(
10161
new Error(
10262
self::ERROR_MAXIMUM_CODE,
10363
self::ERROR_MAXIMUM_TEMPLATE,
104-
['maximum' => $maximum, 'given' => $float]
64+
['maximum' => $maximum, 'exclusiveMaximum' => $exclusiveMaximum, 'given' => $float]
10565
)
10666
);
10767
});
10868
}
10969

11070
/**
111-
* @deprecated use minimum
71+
* @deprecated Use minimum($gte) instead
11272
*/
11373
public function gte(float $gte): static
11474
{
115-
@trigger_error('Use minimum instead', E_USER_DEPRECATED);
75+
@trigger_error('Use minimum($gte) instead', E_USER_DEPRECATED);
11676

11777
return $this->postParse(static function (float $float) use ($gte) {
11878
if ($float >= $gte) {
@@ -130,11 +90,11 @@ public function gte(float $gte): static
13090
}
13191

13292
/**
133-
* @deprecated use exclusiveMinimum
93+
* @deprecated Use minimum($gt, true) instead
13494
*/
13595
public function gt(float $gt): static
13696
{
137-
@trigger_error('Use exclusiveMinimum instead', E_USER_DEPRECATED);
97+
@trigger_error('Use minimum($gt, true) instead', E_USER_DEPRECATED);
13898

13999
return $this->postParse(static function (float $float) use ($gt) {
140100
if ($float > $gt) {
@@ -152,11 +112,11 @@ public function gt(float $gt): static
152112
}
153113

154114
/**
155-
* @deprecated use exclusiveMaximum
115+
* @deprecated Use maximum($lt, true) instead
156116
*/
157117
public function lt(float $lt): static
158118
{
159-
@trigger_error('Use exclusiveMaximum instead', E_USER_DEPRECATED);
119+
@trigger_error('Use maximum($lt, true) instead', E_USER_DEPRECATED);
160120

161121
return $this->postParse(static function (float $float) use ($lt) {
162122
if ($float < $lt) {
@@ -174,11 +134,11 @@ public function lt(float $lt): static
174134
}
175135

176136
/**
177-
* @deprecated use maximum
137+
* @deprecated Use maximum($lte) instead
178138
*/
179139
public function lte(float $lte): static
180140
{
181-
@trigger_error('Use maximum instead', E_USER_DEPRECATED);
141+
@trigger_error('Use maximum($lte) instead', E_USER_DEPRECATED);
182142

183143
return $this->postParse(static function (float $float) use ($lte) {
184144
if ($float <= $lte) {

src/Schema/IntSchema.php

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ final class IntSchema extends AbstractSchemaInnerParse implements SchemaInterfac
1313
public const string ERROR_TYPE_TEMPLATE = 'Type should be "int", {{given}} given';
1414

1515
public const string ERROR_MINIMUM_CODE = 'int.minimum';
16-
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be greater than or equal {{minimum}}, {{given}} given';
17-
18-
public const string ERROR_EXCLUSIVE_MINIMUM_CODE = 'int.exclusiveMinimum';
19-
public const string ERROR_EXCLUSIVE_MINIMUM_TEMPLATE = 'Value should be greater than {{exclusiveMinimum}}, {{given}} given';
20-
21-
public const string ERROR_EXCLUSIVE_MAXIMUM_CODE = 'int.exclusiveMaximum';
22-
public const string ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE = 'Value should be lesser than {{exclusiveMaximum}}, {{given}} given';
16+
public const string ERROR_MINIMUM_TEMPLATE = 'Value should be minimum {{minimum}} {{exclusiveMinimum}}, {{given}} given';
2317

2418
public const string ERROR_MAXIMUM_CODE = 'int.maximum';
25-
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be lesser than or equal {{maximum}}, {{given}} given';
19+
public const string ERROR_MAXIMUM_TEMPLATE = 'Value should be maximum {{maximum}} {{exclusiveMaximum}}, {{given}} given';
2620

2721
public const string ERROR_GTE_CODE = 'int.gte';
2822
public const string ERROR_GTE_TEMPLATE = 'Value should be greater than or equal {{gte}}, {{given}} given';
@@ -36,80 +30,46 @@ final class IntSchema extends AbstractSchemaInnerParse implements SchemaInterfac
3630
public const string ERROR_LTE_CODE = 'int.lte';
3731
public const string ERROR_LTE_TEMPLATE = 'Value should be lesser than or equal {{lte}}, {{given}} given';
3832

39-
public function minimum(int $minimum): static
33+
public function minimum(int $minimum, bool $exclusiveMinimum = false): static
4034
{
41-
return $this->postParse(static function (int $int) use ($minimum) {
42-
if ($int >= $minimum) {
35+
return $this->postParse(static function (int $int) use ($minimum, $exclusiveMinimum) {
36+
if ((!$exclusiveMinimum && $int >= $minimum) || ($exclusiveMinimum && $int > $minimum)) {
4337
return $int;
4438
}
4539

4640
throw new ErrorsException(
4741
new Error(
4842
self::ERROR_MINIMUM_CODE,
4943
self::ERROR_MINIMUM_TEMPLATE,
50-
['minimum' => $minimum, 'given' => $int]
51-
)
52-
);
53-
});
54-
}
55-
56-
public function exclusiveMinimum(int $exclusiveMinimum): static
57-
{
58-
return $this->postParse(static function (int $int) use ($exclusiveMinimum) {
59-
if ($int > $exclusiveMinimum) {
60-
return $int;
61-
}
62-
63-
throw new ErrorsException(
64-
new Error(
65-
self::ERROR_EXCLUSIVE_MINIMUM_CODE,
66-
self::ERROR_EXCLUSIVE_MINIMUM_TEMPLATE,
67-
['exclusiveMinimum' => $exclusiveMinimum, 'given' => $int]
68-
)
69-
);
70-
});
71-
}
72-
73-
public function exclusiveMaximum(int $exclusiveMaximum): static
74-
{
75-
return $this->postParse(static function (int $int) use ($exclusiveMaximum) {
76-
if ($int < $exclusiveMaximum) {
77-
return $int;
78-
}
79-
80-
throw new ErrorsException(
81-
new Error(
82-
self::ERROR_EXCLUSIVE_MAXIMUM_CODE,
83-
self::ERROR_EXCLUSIVE_MAXIMUM_TEMPLATE,
84-
['exclusiveMaximum' => $exclusiveMaximum, 'given' => $int]
44+
['minimum' => $minimum, 'exclusiveMinimum' => $exclusiveMinimum, 'given' => $int]
8545
)
8646
);
8747
});
8848
}
8949

90-
public function maximum(int $maximum): static
50+
public function maximum(int $maximum, bool $exclusiveMaximum = false): static
9151
{
92-
return $this->postParse(static function (int $int) use ($maximum) {
93-
if ($int <= $maximum) {
52+
return $this->postParse(static function (int $int) use ($maximum, $exclusiveMaximum) {
53+
if ((!$exclusiveMaximum && $int <= $maximum) || ($exclusiveMaximum && $int < $maximum)) {
9454
return $int;
9555
}
9656

9757
throw new ErrorsException(
9858
new Error(
9959
self::ERROR_MAXIMUM_CODE,
10060
self::ERROR_MAXIMUM_TEMPLATE,
101-
['maximum' => $maximum, 'given' => $int]
61+
['maximum' => $maximum, 'exclusiveMaximum' => $exclusiveMaximum, 'given' => $int]
10262
)
10363
);
10464
});
10565
}
10666

10767
/**
108-
* @deprecated use minimum
68+
* @deprecated Use minimum($gte) instead
10969
*/
11070
public function gte(int $gte): static
11171
{
112-
@trigger_error('Use minimum instead', E_USER_DEPRECATED);
72+
@trigger_error('Use minimum($gte) instead', E_USER_DEPRECATED);
11373

11474
return $this->postParse(static function (int $int) use ($gte) {
11575
if ($int >= $gte) {
@@ -127,11 +87,11 @@ public function gte(int $gte): static
12787
}
12888

12989
/**
130-
* @deprecated use exclusiveMinimum
90+
* @deprecated Use minimum($gt, true) instead
13191
*/
13292
public function gt(int $gt): static
13393
{
134-
@trigger_error('Use exclusiveMinimum instead', E_USER_DEPRECATED);
94+
@trigger_error('Use minimum($gt, true) instead', E_USER_DEPRECATED);
13595

13696
return $this->postParse(static function (int $int) use ($gt) {
13797
if ($int > $gt) {
@@ -149,11 +109,11 @@ public function gt(int $gt): static
149109
}
150110

151111
/**
152-
* @deprecated use exclusiveMaximum
112+
* @deprecated Use maximum($lt, true) instead
153113
*/
154114
public function lt(int $lt): static
155115
{
156-
@trigger_error('Use exclusiveMaximum instead', E_USER_DEPRECATED);
116+
@trigger_error('Use maximum($lt, true) instead', E_USER_DEPRECATED);
157117

158118
return $this->postParse(static function (int $int) use ($lt) {
159119
if ($int < $lt) {
@@ -171,11 +131,11 @@ public function lt(int $lt): static
171131
}
172132

173133
/**
174-
* @deprecated use maximum
134+
* @deprecated Use maximum($lte) instead
175135
*/
176136
public function lte(int $lte): static
177137
{
178-
@trigger_error('Use maximum instead', E_USER_DEPRECATED);
138+
@trigger_error('Use maximum($lte) instead', E_USER_DEPRECATED);
179139

180140
return $this->postParse(static function (int $int) use ($lte) {
181141
if ($int <= $lte) {

0 commit comments

Comments
 (0)