Skip to content

Commit cf86a85

Browse files
committed
Streamline validators.md index
Makes it so the index looks more like a cheatsheet, condensing information instead of making long lists that require lots of scrolling to explore. Additionally, the happy path for each validator was also added, providing a quick reference use for comparison. The direct markdown links were replaced by titled markdown references, offering mouse-over tooltips over links that display the validator one-line description.
1 parent 68ed5d2 commit cf86a85

9 files changed

Lines changed: 426 additions & 458 deletions

File tree

docs/validators.md

Lines changed: 346 additions & 439 deletions
Large diffs are not rendered by default.

docs/validators/Callback.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ SPDX-License-Identifier: MIT
1111
Validates the input using the return of a given callable.
1212

1313
```php
14-
v::callback(
15-
fn (int $input): bool => $input + ($input / 2) == 15,
16-
)->assert(10);
17-
// Validation passes successfully
14+
v::callback(fn (int $input): bool => $input % 5,)->assert(10);
15+
// → 10 must be valid
1816
```
1917

2018
## Templates

docs/validators/Circuit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ SPDX-License-Identifier: MIT
1010

1111
Validates the input against a series of validators until the first fails.
1212

13+
```php
14+
v::circuit(v::intVal(), v::floatVal())->assert(15);
15+
// Validation passes successfully
16+
```
17+
1318
This validator can be handy for getting the least error messages possible from a chain.
1419

1520
This validator can be helpful in combinations with [Lazy](Lazy.md). An excellent example is when you want to validate a

docs/validators/Cnpj.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ SPDX-License-Identifier: MIT
1010
Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
1111
Ignores non-digit chars, so use `->digit()` if needed.
1212

13+
```php
14+
v::cpf()->assert('00394460005887');
15+
// → "00394460005887" must be a valid CPF number
16+
```
17+
1318
## Templates
1419

1520
### `Cnpj::TEMPLATE_STANDARD`

docs/validators/GreaterThanOrEqual.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ SPDX-License-Identifier: MIT
1010
Validates whether the input is greater than or equal to a value.
1111

1212
```php
13-
v::intVal()->greaterThanOrEqual(10)->assert(9);
14-
// → 9 must be greater than or equal to 10
15-
1613
v::intVal()->greaterThanOrEqual(10)->assert(10);
1714
// Validation passes successfully
1815

16+
v::intVal()->greaterThanOrEqual(10)->assert(9);
17+
// → 9 must be greater than or equal to 10
18+
1919
v::intVal()->greaterThanOrEqual(10)->assert(11);
2020
// Validation passes successfully
2121
```

docs/validators/Odd.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ SPDX-License-Identifier: MIT
1010
Validates whether the input is an odd number or not.
1111

1212
```php
13-
v::odd()->assert(0);
14-
// → 0 must be an odd number
15-
1613
v::odd()->assert(3);
1714
// Validation passes successfully
15+
16+
v::odd()->assert(0);
17+
// → 0 must be an odd number
1818
```
1919

2020
Using `intVal()` before `odd()` is a best practice.

docs/validators/Uuid.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Validates whether the input is a valid UUID. It also supports validation of
1212
specific versions 1 to 8.
1313

1414
```php
15-
v::uuid()->assert('Hello World!');
16-
// → "Hello World!" must be a valid UUID
17-
1815
v::uuid()->assert('eb3115e5-bd16-4939-ab12-2b95745a30f3');
1916
// Validation passes successfully
2017

18+
v::uuid()->assert('Hello World!');
19+
// → "Hello World!" must be a valid UUID
20+
2121
v::uuid()->assert('eb3115e5bd164939ab122b95745a30f3');
2222
// Validation passes successfully
2323

src-dev/Markdown/Content.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use function implode;
2626
use function max;
2727
use function preg_match;
28+
use function preg_replace;
2829
use function reset;
2930
use function rtrim;
3031
use function sprintf;
@@ -111,6 +112,14 @@ public function anchorListItem(string $title, string $href): void
111112
$this->listItem(sprintf('[%s](%s)', $title, $href));
112113
}
113114

115+
public function reference(string $title, string $href, string|null $description = null): void
116+
{
117+
$this->lines[] = match ($description) {
118+
null => sprintf('[%s]: %s', $title, $href),
119+
default => sprintf('[%s]: %s "%s"', $title, $href, $description),
120+
};
121+
}
122+
114123
public function extractSpdx(): self
115124
{
116125
$start = 0;
@@ -130,6 +139,11 @@ public function extractSpdx(): self
130139
return new self(array_slice($this->lines, $start, $end + 2));
131140
}
132141

142+
public static function stripRefs(string $text): string
143+
{
144+
return preg_replace('/\[(.+?)\](?:\[\]|\(.+?\))/', '$1', $text) ?? $text;
145+
}
146+
133147
public function withSection(Content $content): self
134148
{
135149
$firstLine = reset($content->lines);

src-dev/Markdown/Linters/ValidatorIndexLinter.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
use function array_keys;
1919
use function dirname;
20+
use function implode;
2021
use function ksort;
22+
use function preg_match;
2123
use function sort;
2224
use function sprintf;
2325
use function str_ends_with;
@@ -38,15 +40,19 @@ public function lint(File $file): File
3840
$validators = $this->getSortedListOfValidators();
3941

4042
$validatorsByCategory = [];
43+
$validatorsDescriptions = [];
44+
$validatorsExamples = [];
4145
foreach ($validators as $validator) {
42-
foreach ($this->getCategoriesByValidator($validator) as $category) {
46+
$validatorInfo = $this->getValidatorInfo($validator);
47+
foreach ($validatorInfo['categories'] as $category) {
4348
$validatorsByCategory[$category] ??= [];
4449
$validatorsByCategory[$category][] = $validator;
50+
$validatorsDescriptions[$validator] = $validatorInfo['description'];
51+
$validatorsExamples[$validator] = $validatorInfo['example'];
4552
}
4653
}
4754

4855
ksort($validatorsByCategory);
49-
$validatorsByCategory['Alphabetically'] = $validators;
5056

5157
$categories = array_keys($validatorsByCategory);
5258

@@ -55,16 +61,33 @@ public function lint(File $file): File
5561
$content->emptyLine();
5662

5763
foreach ($categories as $category) {
58-
$content->h2($category);
5964
$categoryValidators = $validatorsByCategory[$category];
6065
sort($categoryValidators);
66+
$refs = [];
6167
foreach ($categoryValidators as $categoryValidator) {
62-
$content->anchorListItem($categoryValidator, sprintf('validators/%s.md', $categoryValidator));
68+
$refs[] = sprintf('[%s][]', $categoryValidator);
6369
}
6470

71+
$content->paragraph(sprintf('**%s**: %s', $category, implode(' - ', $refs)));
72+
6573
$content->emptyLine();
6674
}
6775

76+
$content->h2('Alphabetically');
77+
foreach ($validators as $validator) {
78+
$content->listItem(sprintf('[%s][] - `%s`', $validator, $validatorsExamples[$validator]));
79+
}
80+
81+
$content->emptyLine();
82+
83+
foreach (array_keys($validatorsDescriptions) as $validator) {
84+
$content->reference(
85+
$validator,
86+
sprintf('validators/%s.md', $validator),
87+
$validatorsDescriptions[$validator],
88+
);
89+
}
90+
6891
return $file->withContent($content);
6992
}
7093

@@ -88,10 +111,22 @@ private function getSortedListOfValidators(): array
88111
}
89112

90113
/** @return array<string> */
91-
private function getCategoriesByValidator(string $validator): array
114+
private function getValidatorInfo(string $validator): array
92115
{
93116
$content = Content::from(sprintf('%s/validators/%s.md', dirname(__DIR__, 2) . '/../docs', $validator));
94117
$section = $content->getSection('## Categorization');
118+
$description = '';
119+
$example = '';
120+
foreach ($content->getSection(sprintf('# %s', $validator)) as $line) {
121+
if ($description === '' && preg_match('/^[A-Z]/', $line) === 1) {
122+
$description = $line;
123+
}
124+
125+
if (preg_match('/^v::/', $line) === 1) {
126+
$example = $line;
127+
break;
128+
}
129+
}
95130

96131
$categories = [];
97132
foreach ($section as $line) {
@@ -102,6 +137,10 @@ private function getCategoriesByValidator(string $validator): array
102137
$categories[] = trim(substr($line, 1));
103138
}
104139

105-
return $categories;
140+
return [
141+
'categories' => $categories,
142+
'description' => Content::stripRefs($description),
143+
'example' => $example,
144+
];
106145
}
107146
}

0 commit comments

Comments
 (0)