Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d3dec57

Browse files
authored
Merge pull request #52 from programmatordev/YAPV-20-create-length-rule
Create Length rule
2 parents 96d4ef4 + 87ba533 commit d3dec57

28 files changed

+511
-53
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"php": ">=8.1",
1616
"egulias/email-validator": "^4.0",
1717
"symfony/intl": "^6.3",
18-
"symfony/polyfill-ctype": "^1.27"
18+
"symfony/polyfill-ctype": "^1.27",
19+
"symfony/polyfill-intl-grapheme": "^1.29"
1920
},
2021
"require-dev": {
2122
"phpunit/phpunit": "^10.0",

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
## String Rules
1717

1818
- [Email](03-rules_email.md)
19+
- [Length](03-rules_length.md)
1920
- [URL](03-rules_url.md)
2021

2122
## Comparison Rules

docs/03-rules_choice.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ For example, if `max` is 2, the input array must have at most 2 values.
7878

7979
### `message`
8080

81-
type: `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
81+
type: `?string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
8282

8383
Message that will be shown if input value is not a valid choice.
8484

@@ -92,7 +92,7 @@ The following parameters are available:
9292

9393
### `multipleMessage`
9494

95-
type: `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
95+
type: `?string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
9696

9797
Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.
9898

@@ -106,7 +106,7 @@ The following parameters are available:
106106

107107
### `minMessage`
108108

109-
type: `string` default: `The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.`
109+
type: `?string` default: `The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.`
110110

111111
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `min`.
112112

@@ -123,7 +123,7 @@ The following parameters are available:
123123

124124
### `maxMessage`
125125

126-
type: `string` default: `The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.`
126+
type: `?string` default: `The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.`
127127

128128
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `max`.
129129

docs/03-rules_count.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Validator::count(max: 1)->validate(['a', 'b', 'c']); // false
2525

2626
// min and max value
2727
Validator::count(min: 2, max: 4)->validate(['a', 'b', 'c']); // true
28-
2928
// exact value
3029
Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
3130
```
@@ -34,10 +33,10 @@ Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
3433
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
3534
3635
> [!NOTE]
37-
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
36+
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
3837
3938
> [!NOTE]
40-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
39+
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
4140
4241
## Options
4342

@@ -47,16 +46,12 @@ type: `?int` default: `null`
4746

4847
It defines the minimum number of elements required.
4948

50-
For example, if `min` is 2, the input value must have at least 2 elements.
51-
5249
### `max`
5350

5451
type: `?int` default: `null`
5552

5653
It defines the maximum number of elements required.
5754

58-
For example, if `max` is 2, the input value must have at most 2 elements.
59-
6055
### `minMessage`
6156

6257
type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.`

docs/03-rules_country.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Available options:
4242

4343
### `message`
4444

45-
type: `string` default: `The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.`
45+
type: `?string` default: `The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.`
4646

4747
Message that will be shown if the input value is not a valid country code.
4848

docs/03-rules_each-key.md

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

3-
Validates every key of an `array` or object implementing `\Traversable` with a given set of rules.
3+
Validates every key of an `array`, or object implementing `\Traversable`, with a given set of rules.
44

55
```php
66
EachKey(
@@ -12,8 +12,13 @@ EachKey(
1212
## Basic Usage
1313

1414
```php
15-
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 'green' => '#0f0']); // true
16-
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 1 => '#0f0']); // false
15+
Validator::eachKey(
16+
Validator::notBlank()->type('string')
17+
)->validate(['red' => '#f00', 'green' => '#0f0']); // true
18+
19+
Validator::eachKey(
20+
Validator::notBlank()->type('string')
21+
)->validate(['red' => '#f00', 1 => '#0f0']); // false
1722
```
1823

1924
> [!NOTE]
@@ -29,13 +34,15 @@ Validator that will validate each key of an `array` or object implementing `\Tra
2934

3035
### `message`
3136

32-
type: `string` default: `Invalid key: {{ message }}`
37+
type: `?string` default: `Invalid key: {{ message }}`
3338

3439
Message that will be shown if at least one input value key is invalid according to the given `validator`.
3540

3641
```php
37-
Validator::eachKey(Validator::notBlank())->assert(['red' => '#f00', 1 => '#0f0'], 'color');
3842
// Throws: Invalid key: The color key value should be of type "string", 1 given.
43+
Validator::eachKey(
44+
Validator::type('string')
45+
)->assert(['red' => '#f00', 1 => '#0f0'], 'color');
3946
```
4047

4148
The following parameters are available:

docs/03-rules_each-value.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ EachValue(
1212
## Basic Usage
1313

1414
```php
15-
Validator::eachValue(Validator::notBlank()->greaterThan(1)->lessThan(10))->validate([4, 5, 6]); // true
16-
Validator::eachValue(Validator::notBlank()->greaterThan(1)->lessThan(10))->validate([4, 5, 20]); // false
15+
Validator::eachValue(
16+
Validator::notBlank()->greaterThan(1)->lessThan(10)
17+
)->validate([4, 5, 6]); // true
18+
19+
Validator::eachValue(
20+
Validator::notBlank()->greaterThan(1)->lessThan(10)
21+
)->validate([4, 5, 20]); // false
1722
```
1823

1924
> [!NOTE]
@@ -29,13 +34,15 @@ Validator that will validate each element of an `array` or object implementing `
2934

3035
### `message`
3136

32-
type: `string` default: `At key "{{ key }}": {{ message }}`
37+
type: `?string` default: `At key "{{ key }}": {{ message }}`
3338

3439
Message that will be shown if at least one input value element is invalid according to the given `validator`.
3540

3641
```php
37-
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'color');
3842
// Throws: At key 2: The color value should not be blank, "" given.
43+
Validator::eachValue(
44+
Validator::notBlank()
45+
)->assert(['red', 'green', ''], 'color');
3946
```
4047

4148
The following parameters are available:

docs/03-rules_email.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Validator::email(normalizer: fn($value) => trim($value))->validate('test@example
5656

5757
### `message`
5858

59-
type `string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
59+
type: `?string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
6060

6161
Message that will be shown if the input value is not a valid email address.
6262

docs/03-rules_greater-than-or-equal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.`
47+
type: `?string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not greater than or equal to the constraint value.
5050

docs/03-rules_greater-than.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.`
47+
type: `?string` default: `The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not greater than the constraint value.
5050

0 commit comments

Comments
 (0)